May i ask how to convert an array to JSON format,
I have tried to used the following method, but fail to do so, any idea?
Here's my current result:
["B型肝炎疫苗下一個注射期为2017-06-30 ","96709394"]
["疫苗名稱4下一個注射期为2017-06-30 ","96709394"]
What i want :
{"B型肝炎疫苗下一個注射期为2017-06-30 ","96709394"},
{"疫苗名稱4下一個注射期为2017-06-30 ","96709394"}
Thank you
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['patientid'] . '</td>';
echo '<td>' . $row['vaccineid'] . '</td>';
echo '<td>' . $row['vaccinename1'] . '</td>';
echo '<td>' . $row['totalnoofinjection'] . '</td>';
echo '<td>' . $row['nthinjection'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['nextdate'] . '</td>';
echo '<td>' . $row['skip'] . '</td>';
echo '<td>' . $row['traditionalmessage'] ."<br />";
echo '<td>' . $row['traditionalmessage'] . '</td>';
echo '<td>' . $row['telMobile'] . '</td>';
$data_array = [$row['traditionalmessage'],$row['telMobile']];
foreach($data_array as $key => $value){
$new_data_array[urlencode($key)] = urlencode($value);
}
$data_json_url = json_encode($new_data_array);
$data_json = urldecode($data_json_url);
echo $data_json;
Related
Hi I am having a problem displaying the 24 H volume name in coinmarketcap api it roduces an error when i call it but other values are working perfectly it may bebecause the 24h volume starts with a number. I want to know how will i get it. Thank you. Here is my current code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<th>Name</th>
<th>Symbol</th>
<th>Rank</th>
</tr>
<?php
ini_set('max_execution_time', 300);
// $tick = file_get_contents('https://api.coinmarketcap.com/v1/ticker/bitcoin/');
$url ='https://api.coinmarketcap.com/v1/ticker/?start=0&limit=1000';// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data); // decode the JSON feed
// echo $characters[0]->name;
foreach ($characters as $character) {
echo '<tr>';
echo '<td>' . $character->name . '</td>';
echo '<td>' . $character->symbol . '</td>';
echo '<td>' . $character->rank . '</td>';
echo '<td>' . $character->price_usd . '</td>';
echo '<td>' . $character->price_btc . '</td>';
echo '<td>' . $character->total_24h_volume_usd . '</td>';
echo '<td>' . $character->market_cap_usd . '</td>';
echo '<td>' . $character->available_supply . '</td>';
echo '<td>' . $character->total_supply . '</td>';
echo '<td>' . $character->max_supply . '</td>';
echo '<td>' . $character->percent_change_1h . '</td>';
echo '<td>' . $character->percent_change_24h . '</td>';
echo '<td>' . $character->percent_change_7d . '</td>';
echo '<td>' . $character->last_updated . '</td>';
echo '</tr>';
}
?>
</tbody>sdsdf
</table>
</body>
</html>
In order to access fields named in that fashoin you need to wrap them with {''}. So you need to write $character->{'24h_volume_usd'}.
I have a code where I echo a table:
<?php
// connect to the database
include('core/base.php');
// get results from database
$result = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM uitslag ORDER BY ID ASC")
or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Nummer</th><th>Naam</th><th>Telefoon</th><th>Binnen</th> <th>Adres</th> <th>Postcode</th> <th>Wijk</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysqli_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo '<td>' . $row['Telefoon'] . '</td>';
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
echo '<td>Aanpassen</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
The row 'Telefoon' either echos 1 or 0. How can I echo a checkbox which is checked when 1 instead of echoing the actual number?
Define input element of type checkbox.Like this.
$telefoon = ($row['Telefoon']==1)?'checked':'';
echo '<td>' . $row['ID'] . '</td>';
echo '<td>' . $row['Naam'] . '</td>';
echo "<td><input type='checkbox' $telefoon></td>";
echo '<td>' . $row['Binnen'] . '</td>';
echo '<td>' . $row['Adres'] . '</td>';
echo '<td>' . $row['Postcode'] . '</td>';
echo '<td>' . $row['Wijk'] . '</td>';
<?php
if($row['Telefoon']==1){
echo "<td><input type='checkbox' checked></td>";
}
else {
echo "<td><input type='checkbox' ></td>";
}
?>
<!doctype html>
<?php
?>
<html>
<head>
<title>Midterm Review</title>
</head>
<body>
<h3>Tools Not Currently in Stock</h3>
<?php
$conn = mysqli_connect(This part works );
mysqli_select_db(this part works);
$query = "SELECT * FROM `midterm` WHERE stock='0'";
$result = mysqli_query($conn, $query);
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach ($result as $row) {
$row = mysqli_fetch_array($result);
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
echo '</table>';
};
mysqli_close($conn);
?>
<!--<form method="post" action="midterm_confirmation.php">
<label>Part Number: </label><input type="text" name="partNumber" /><br />
<label>Description: </label><input type="text" name="description" /></br />
<label>Stock: </label><input type="number" name="stock" /><br />
<label>Price: </label><input type="text" name ="price" /></br />
<label>Received Date: </label><input type="text" name="receivedDate" /></br />
<input type="Submit" value="Add to Stock">
</form> -->
</body>
</html>
Basically my end result is that I am getting one table row, instead the two I am getting. Any suggestions? The table I have populates everything but is only running once, instead of posting for each line I have where stock is equal to zero.
See the snippet below. P.S. Don't mix templates and database layer, it's a bad smell...
<?php
// establish connection to $conn variable
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>';
mysqli_close($conn);
?>
The problem is in closing of </table> It should be kept outside the loop.
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
// echo '</table>';// this is wrong.
};
echo '</table>';// this is correct. closing table inside loop is wrong, do it outside the loop.
You can simply use a while loop to do it like below:
<?php
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
while ($row = mysqli_fetch_assoc($query)) {
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
Or if you want to use a foreach then write an extra function for it:
<?php
function mysql_fetch_all($result) {
$rows = array();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
// establish connection to $conn
$query = mysqli_query($conn, "SELECT * FROM `midterm` WHERE stock='0'");
echo '<table>';
echo '<tr><th>ID</th><th>Part Number</th><th>Description</th><th>Stock</th><th>Price</th><th>Received</th></tr>';
foreach (mysql_fetch_all($result) as $row){
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['part_number'] . '</td>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['stock'] . '</td>';
echo '<td>' . $row['price'] . '</td>';
echo '<td>' . $row['received_date'] . '</td>';
echo '</tr>';
}
echo '</table>'; // this should be outside the loop
mysqli_close($conn);
?>
how can i do if this row is empty then it will showing another row
suppose name row is blank
echo '<td>' . $row['name'] . '</td>';
so if name row is blank then it will show echo '<td>' . $row['companyname'] . '</td>'; row
please help me how can i fix this
thanks
echo "<tr>";
echo '<td>' . $row['buildingname'] . '</td>';
echo '<td>' . $row['area'] . '</td>';
echo '<td>' . $row['city'] . '</td>';
echo '<td>' . $row['flatno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['mobileno'] . '</td>';
echo '<td>' . $row['agreementdates'] . '</td>';
echo '<td>' . $row['agreementdatee'] . '</td>';
echo "</tr>";
Try
if(empty( $row['name'])){
$tbl = '<td>' . $row['companyname'] . '</td>';
}else{
$tbl = '<td>' . $row['name'] . '</td>';
}
echo $tbl;
try this i think this is working.
echo '<td>' . empty($row['name']) ? $row['companyname'] : $row['name'] . '</td>';
try this:
echo "<tr>";
if(!empty($row['buildingname'])) {
echo '<td>' . $row['buildingname'] . '</td>';
}
if(!empty($row['area'])) {
echo '<td>' . $row['area'] . '</td>';
}
if(!empty($row['city'])) {
echo '<td>' . $row['city'] . '</td>';
}
if(!empty($row['flatno'])) {
echo '<td>' . $row['flatno'] . '</td>';
}
if(!empty($row['name'])) {
echo '<td>' . $row['name'] . '</td>';
}
if(!empty($row['mobileno'])) {
echo '<td>' . $row['mobileno'] . '</td>';
}
if(!empty($row['agreementdates'])) {
echo '<td>' . $row['agreementdates'] . '</td>';
}
echo "</tr>";
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I keep getting the error with the echo statement. Is it the quotes that are wrong? Should i add quotes around the $row statement
<?php
while($row = mysql_fetch_array($search))
{
echo "<tr>"//the line below
'<td>' . $row['DepartmentOwner'] . '</td>';
'<td>' . $row['CurrentAssignee'] . '</td>';
'<td>' . $row['Location'] . '</td>';
'<td>' . $row['LaptopSerialNumber'] . '</td>';
'<td>' . $row['SupportedOS'] . '</td>';
'<td>' . $row['DeviceName'] . '</td>';
'<td>' . $row['IMEISerialNumber'] . '</td>';
'<td>' . $row['UUID'] . '</td>';
'<td>' . $row['ReturnDate']'</td>';
"</tr>";
}
echo "</table>"; ?>
In this line
'<td>' . $row['ReturnDate']'</td>'
It should be
'<td>' . $row['ReturnDate'] . '</td>'
You are missing a string concatenator .
while($row = mysql_fetch_array($search)){
echo "<tr>";
echo '<td>' . $row['DepartmentOwner'] . '</td>';
echo '<td>' . $row['CurrentAssignee'] . '</td>';
echo '<td>' . $row['Location'] . '</td>';
echo '<td>' . $row['LaptopSerialNumber'] . '</td>';
echo '<td>' . $row['SupportedOS'] . '</td>';
echo '<td>' . $row['DeviceName'] . '</td>';
echo '<td>' . $row['IMEISerialNumber'] . '</td>';
echo '<td>' . $row['UUID'] . '</td>';
echo '<td>' . $row['ReturnDate'] . '</td>';
echo "</tr>";
}
echo "</table>"; ?>
If you close your echo you need again write echo!
<?php
while($row = mysql_fetch_array($search))
{
echo "<tr>";//the line below
echo '<td>' . $row['DepartmentOwner'] . '</td>';
echo '<td>' . $row['CurrentAssignee'] . '</td>';
echo '<td>' . $row['Location'] . '</td>';
echo '<td>' . $row['LaptopSerialNumber'] . '</td>';
echo '<td>' . $row['SupportedOS'] . '</td>';
echo '<td>' . $row['DeviceName'] . '</td>';
echo '<td>' . $row['IMEISerialNumber'] . '</td>';
echo '<td>' . $row['UUID'] . '</td>';
echo '<td>' . $row['ReturnDate'].'</td>';
echo "</tr>";
}
echo "</table>"; ?>
You're incorrectly concatenating the string passed to echo, you can avoid this (and make the code much cleaner IMO) by using PHP's alternate syntax.
<?php while($row = mysql_fetch_array($search)): ?>
<tr>
<td><?php echo $row['DepartmentOwner']; ?></td>
<td><?php echo $row['CurrentAssignee']; ?></td>
<td><?php echo $row['Location']; ?></td>
...
...
...
</tr>
<?php endwhile; ?>
Anthony.