I'm using PDO and I'm trying to print the results on a table, but the values don't appear. I need to use the <td> tags.
Here is my complete code with a help of francisco:
<?php
require_once('config.php');
$stmt = $connect->prepare("SELECT id, username, email FROM user");
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
?>
<table id="table">
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>email</th>
</tr>
</thead>
<tbody>
<?php
foreach ($result as $row):
?>
<tr>
<td><?= $row['id'];?></td>
<td><?= $row['username'];?></td>
<td><?= $row['email'];?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
You need to call fetchAll() outside of loop OR call fetch() in each iteration like this:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
// do sth
If you want foreach, just call fetchAll() and do foreach loop over result.
Related
I have the following code to generate a HTML table with the placed orders.
// Select data FROM ORDER & ORDERLINE
$id = $value->ID;
$datenow = date("Y-m-d");
$temp = 0;
$stmt2 = $mysql->prepare("SELECT salesorder.order_id AS soid, salesorder.delivery_date AS sdd, orderline.qty AS olq, food.food_type AS fft
FROM orderline
INNER JOIN salesorder ON salesorder.order_id = orderline.order_id
INNER JOIN food ON food.food_id = orderline.food_id
WHERE salesorder.client_id=? AND orderline.qty!=?
ORDER BY sdd");
$stmt2->bind_param('ss', $id, $temp);
$stmt2->execute();
$result2 = $stmt2->get_result();
// determen if there are orders to display
if (mysqli_num_rows($result2) > 0) {
while ($value2 = $result2->fetch_object()) {
$results[] = $value2;
}
?>
<table class="center table">
<thead>
<tr>
<th>Order Number</th>
<th>Delivery Date</th>
<th>QTY</th>
<th>Food Type</th>
</tr>
</thead>
<tbody>
<b>Delivered orders :</b> (and orders delivered today...)
<?php foreach ( $results as $result ) : ?>
<?php if (($result->sdd)<=$datenow) { ?>
<td><?php echo ($result->soid); ?></td>
<td><?php echo ($result->sdd); ?></td>
<td><?php echo ($result->olq); ?></td>
<td><?php echo ($result->fft); ?></td>
</tr>
<?php } ?>
<?php endforeach;?>
</tbody>
</table>
The problem is that the 'delivery date' and 'order number' ALSO are displayed as many times as there are QTY and food types. i know this has to do with indexes, but however i try, i cant find the right syntax or way to do it. Any ideas?
I have added the data tables as images. Please let me know...
Inside of foreach u need to open <tr> Tag
You Can Loop the array directly mysqli_fetch_assoc returning array, then why you store into $results[] array,
Based on your comments on the other answer by Eibs please could you check if the following gives you what you need. First when you are looping the results we create an associative array grouped by the order number and date and add each item to the items array. Then when creating the table we loop the results array and then the items array. I have used separate rows only because you have a headings row, however, using this same approach you can design your table output a number of different ways.
// determen if there are orders to display
if(mysqli_num_rows($result2) > 0)
{
while($value2 = $result2->fetch_object())
{
$key = $value2->soid.'-'.$value2->sdd;
if(!isset($results[$key]))
{
$results[$key] = array(
'soid' => $value2->soid,
'sdd' => $value2->sdd,
'items' => array(),
);
}
$results[$key]['items'][] = $value2;
}
}
?>
<table class="center table">
<thead>
<tr>
<th>Order Number</th>
<th>Delivery Date</th>
<th>QTY</th>
<th>Food Type</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4"><b>Delivered orders :</b> (and orders delivered today...)</td>
</tr>
<?php foreach($results as $result) : ?>
<?php if($result['sdd'] <= $datenow): ?>
<tr>
<td><?php echo($result['soid']); ?></td>
<td><?php echo($result['sdd']); ?></td>
<td></td>
<td></td>
</tr>
<?php foreach($result['items'] as $item): ?>
<tr>
<td></td>
<td></td>
<td><?php echo($item->olq); ?></td>
<td><?php echo($item->fft); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
I want to print invoice data. but some error only one image print. how to print all data in an invoice I am using PHP 7.2 and loop foreach please help me...
error->Warning: Invalid argument supplied for foreach() in D:\xammp\htdocs\tam\admin\invoice.php on line 133
PHP GET id wise invoice data script
<?php
require 'setting/config.php';
$id=$_GET['id'];
$query="select * from orders where id='$id'";
$galrun=mysqli_query($conn, $query);
$result=mysqli_fetch_assoc($galrun);
$total=$result['total'];
$array[0]=$result['image'];
$array[1]=$result['productName'];
$array[2]=$result['product_qty'];
$array[3]=$result['salseprice'];
$resultdata= implode(",", $array);
?>
HTML invoice
<table class="table">
<thead>
<tr>
<th>Sr. No.</th>
<th>image</th>
<th>Description</th>
<th>Qty</th>
<th>Amount</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
foreach ($resultdata as $item){
?>
<tr>
<td> ? </td>
<td><img src="image/product/<?php echo $item[0] ; ?>" style="width:100px;height:50px;"></td>
<td><?php echo $item[1] ; ?></td>
<td><?php echo $item[2] ; ?></td>
<td><?php echo $item[3] ; ?></td>
<td><?php echo $total ; ?</td>
</tr>
<?php }?>
</tbody>
</table>
You try to loop through $resultdata, but it is a string, because of this part:
$resultdata= implode(",", $array);
PHP implode() returns a string.
I want to make a table based on a result from an SQL query in PHP. I know that if my query returned 2 results I could use
echo '<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<tr>
<td>$row[0]['username']</td>
<td>$row[0]['answer']</td>
</tr>
<tr>
<td>$row[1]['username']</td>
<td>$row[1]['answer']</td>
</tr>
But how do I go about this if I don't know how many results will be returned
EDIT:
I have used a loop as advised but the results appear underneath the table instead of inside. Only the table headers are inside. Any ideas?
echo '<table class="collabtable">
<tr>
<th>User</th>
<th>Answer</th>
</tr>';
foreach($rows as $row){
if (isset($row['collabans'])){
echo '<tr>
<td>'.$row['username'].'</td>
<td>'.$row['collabans'].'</td>
</tr>';
}
echo '</table>';
}
}
You can use loop. See below.
<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<?php foreach ($rows as $row) {
if (isset($row['collabans'])){ ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['answer']; ?></td>
</tr>
<?php }
} ?>
</table>
You need to loop through all results of your query result as below:
if ($result->num_rows() > 0) {
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
<tr>
<td>$row['username']</td>
<td>$row['answer']</td>
</tr>
}
}
Hope it helps you.
Hy!
I want to list all row of my table, but it is not works :(
My function.php:
function getCharacterAdminJails($account_id) {
global $connection;
$stmt = $connection->pdo->prepare("SELECT * FROM adminjails WHERE jailed_accountID = :account_id");
$stmt->bindParam(":account_id", $account_id);
$stmt->execute();
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
return $adminjail_data;
}
My example.php, where I list my rows:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<tr>
<th><?=$adminjail_data["jailed_admin"];?></th>
<th><?=$adminjail_data["jailed_reason"];?></th>
<th><?=$adminjail_data["jailed_ido"];?></th>
<th><?=$adminjail_data["jailed_idopont"];?></th>
</tr>
</table>
How can I list all rows of my table?
I would suggest you change this line of code in your php function from
$adminjail_data = $stmt->fetch(PDO::FETCH_ASSOC);
To this:
$adminjail_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
As fatchAll will return an array of all the results from your query and then in your html page do this:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
This is from PHP.net documentation about fetchAll:
PDOStatement::fetchAll — Returns an array containing all of the result set rows
You can refer to the full php documentation about PDOs fetchAll here: https://secure.php.net/manual/en/pdostatement.fetchall.php
Try the following code to list all the rows by iterating through the $adminjail_data array:
<table class="table table-hover">
<tr>
<th>Admin neve:</th>
<th>Indok:</th>
<th>Perc:</th>
<th>Időpont:</th>
</tr>
<?php foreach($adminjail_data as $row) :?>
<tr>
<th><?=$row["jailed_admin"];?></th>
<th><?=$row["jailed_reason"];?></th>
<th><?=$row["jailed_ido"];?></th>
<th><?=$row["jailed_idopont"];?></th>
</tr>
<?php endforeach; ?>
</table>
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM 'tdata'";
$result=mysqli_query($conn,$query);
while ($row=mysqli_fetch($result)) {
?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
<th></th>
</thead>
</tr>
<tbody>
<?php echo "<tr><td>".$row[full_name]."</td></tr>";
}
?>
</tbody>
</table>
Error# Fatal error: Uncaught Error: Call to undefined function mysqli_fetch() in C:\xampp\htdocs\gsoft\assigment\tabledb.php:9 Stack trace: #0 {main} thrown in C:\xampp\htdocs\gsoft\assigment\tabledb.php on line 9
if you want fetch without index use mysqli_fetch_assoc().
Or
if you want to fetch with index use mysqli_fetch_array()
And i think you are doing wrong here because your table tag is inside while loop and your <thead> too.
i think it should be outside of while loop. and you have syntax error in your query and you have one extra <th>
your full code.
<?php
$host="localhost";
$user="root";
$pwd="";
$db="assigment";
$conn=mysqli_connect($host,$user,$pwd,$db);
$query="SELECT * FROM tdata ";
$result=mysqli_query($conn,$query); ?>
<table class="table">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
<th>Birthday</th>
<th>Gender</th>
<th>Intrests</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php while ($row=mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
<td><?php echo $row['your table field name']; ?></td>
</tr>
<?php }
?>
</tbody>
</table>
use mysqli_fetch_array function instead of mysqli_fetch
Use mysqli_fetch_array instead of mysqli_fetch
Also your table is inside the loop