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.
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 am creating a simple database that can do basic CRUD operations (create, read, update, delete) using php. I am able to complete the create, and able to see the results if I directly query the mySQL DB in the back end. But, I am having trouble getting the table to display on the webpage. It is instead displaying a "Bad Gateway" error if I attempt to display the database entries.
I tried removing the reference to the table, specifically
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>...
and the web page on front end works fine. Albeit can only see the data if I query the backend.
<?php include('php_code.php'); ?>
...
...
...
<?php $results = mysqli_query($db, "SELECT * FROM info"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['city']; ?></td>
<td>
<a href="test.php?edit=<?php echo $row['id']; ?>"
class="edit_btn" >Edit</a>
</td>
<td>
<a href="php_code.php?del=<?php echo $row['id']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
</table>
<!--in php_code.php-->
//to retrieve records
$select_query = "SELECT * FROM info";
$result = mysqli_query($db, $select_query);
I should be able to see the table with data containing name, address and city. But I am getting a 502 error instead.
Try this
<?php
include('php_code.php');
$results = mysqli_query($db, "SELECT * FROM `info` ");
$return = <<<HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan="2">Action</th>
</tr>
</thead>
<tbody>
HTML;
while ($row = mysqli_fetch_array($results)) {
$return .= <<<HTML
<tr>
<td>{$row['name']}</td>
<td>{$row['address']}</td>
<td>{$row['city']}</td>
<td><a href="test.php?edit={$row['id']}" class="edit_btn" >Edit</a></td>
<td>Delete</td>
</tr>
HTML;
}
$return .= <<<HTML
</tbody>
</table>
HTML;
echo $return;
?>
your php_code.php should really only have the database config...
you are closing the php statements so you cannot retrieve the result of your query. Don't split php parts and just echo html like this
<?php
echo " <table>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th colspan='2'>Action</th>
</tr>
</thead> ";
while ($row = mysqli_fetch_array($results)) {
echo "<tr>
<td>$row['name']</td>
<td>$row['address']</td>
<td> $row['city']</td>
<td>
<a href='test.php?edit=$row['id']'
class='edit_btn' >Edit</a>
</td>
<td>
<a href='php_code.php?del=$row['id']'
class='del_btn'>Delete</a>
</td>
</tr>";
}
echo "</table>";
?>
I am creating a table that fetches data from an SQL database using a PDO method. The data loads fine but the issue I'm having is that the 'for loop' I'm using is multiplying a (table header) after every (table row).
I am wondering what a possible solution the the issue could be.
Any help is appreciated, thanks!
Here is the code:
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
</table>
<?php }
?>
Up at the very top is the connection file that creates a connection to my local database and a statement that brings in the information I want to display from the database like so:
$result = $conn->prepare("SELECT * FROM events");
$result->execute();
Few possible solutions are
i) Put the header part and the table opening and closing tag outside the for loop. This will give you an empty table with headers if there is no data.
ii) Put an if condition and print headers only when i = 0, and put table tags outside the loop. This will give you an empty table with nothing if there is no data.
Edit: Method II (since you are learning)
<table id="eventstable">
<?php
for($i=0; $row = $result->fetch(); $i++){
if($i == 0){
?>
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php }//if statment ends here ?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
I would also suggest since you are learning, use better ways than for loop. Look at the php PDO manuals and see the use of while or foreach. It will help more.
Put in loop only, what should be looped, everything else should be outside of loop.
For example, your code could look like this
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php
}
?>
</table>
try this :
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
foreach($result->fetch() as $row){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
</table>
I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.