Display php data in a html table using while loop - php

Each time i echo out information from the database using while loop, the first results are displayed in only one row while the rest of the results are printed outside the table as raw text.
<table class="table table-striped">
<thead>
<tr class="bg-info">
<th>Id</th>
<th>Customer</th>
<th>Order</th>
<th>Title</th>
<th>Quantity</th>
<th>Total</th>
<th>Paid</th>
<th>Created Time</th>
<th>Updated Time</th>
<th>Update Order</th>
<th>Delete Order</th>
</tr>
</thead>
<tbody>
<?php
if($sql->num_rows > 0){
while($data = $sql->fetch_array()):
?>
<tr>
<td><?php echo $database->escape_value($data['custom_id']); ?></td>
<td><?php echo $database->escape_value($data['order_id']); ?></td>
<td><?php echo $database->escape_value($data['title']); ?></td>
<td><?php echo $database->escape_value($data['quantity']); ?></td>
<td><?php echo $database->escape_value($data['total']); ?></td>
<td><?php echo $database->escape_value($data['paid']); ?></td>
<td><?php echo $database->escape_value($data['created_at']); ?></td>
<td><?php echo $database->escape_value($data['updated_at']); ?></td>
<td>Update Order</td>
<td>Delete</td>
</tr>
</tbody>
</table>
<?php endwhile; }else
echo "<div class='btn bg-danger'>search not found</div>";
?>
Guys,only the first result is displayed proparly in the table, i want all the data display inside the table.

You are closing the table inside the loop. It's important that you keep the row only in the while.
Then be careful to close all brackets correctly.
<thead>
<tr class="bg-info">
<th>Id</th>
<th>Customer</th>
<th>Order</th>
<th>Title</th>
<th>Quantity</th>
<th>Total</th>
<th>Paid</th>
<th>Created Time</th>
<th>Updated Time</th>
<th>Update Order</th>
<th>Delete Order</th>
</tr>
</thead>
<tbody>
<?php
if($sql->num_rows > 0){
while($data = $sql->fetch_array()){
?>
<tr>
<td><?php echo $database->escape_value($data['custom_id']); ?></td>
<td><?php echo $database->escape_value($data['order_id']); ?></td>
<td><?php echo $database->escape_value($data['title']); ?></td>
<td><?php echo $database->escape_value($data['quantity']); ?></td>
<td><?php echo $database->escape_value($data['total']); ?></td>
<td><?php echo $database->escape_value($data['paid']); ?></td>
<td><?php echo $database->escape_value($data['created_at']); ?></td>
<td><?php echo $database->escape_value($data['updated_at']); ?></td>
<td>Update Order</td>
<td>Delete</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php
if($sql->num_rows == 0){
echo "<div class='btn bg-danger'>search not found</div>";
}
?>

Related

Php automatic time countdown(timer) as soon as a record is inserted

I have a table for saving details including logging time.Just as soon as a new record is inserted I want the column 'Countdown' to start an automatic time countdown from 30min until it gets to zero(0).How do I go about it?Thanks.
Here is the table php code
`<?php $results = mysqli_query($db, "SELECT
Vehicle_name,Vehicle_make,Vehicle_color,Number_plate,Date,Time FROM
vehicle"); ?>
<div class="table">
<table>
<thead>
<tr>
<th>Vehicle name</th>
<th>Vehicle make</th>
<th>Vehicle color</th>
<th>Reg Number</th>
<th>Date</th>
<th>Time</th>
<th>Countdown</th>
<th colspan="4">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['Vehicle_name']; ?></td>
<td><?php echo $row['Vehicle_make']; ?></td>
<td><?php echo $row['Vehicle_color']; ?></td>
<td><?php echo $row['Number_plate']; ?></td>
<td><?php echo $row['Date']; ?></td>
<td><?php echo $row['Time']; ?></td>
<td><?php echo $row['Time']; ?></td>
<td>
<a href="php_code.php?del=<?php echo $row['Number_plate']; ?>"
class="del_btn">Delete</a>
</td>
</tr>
<?php } ?>
`

show multiple rows for each result value

I have two tables one for products and another for items.
I want to show all items details for each product in one table like this:
I have tried to make a nested while loops but the result not as I want.
<table border="1">
<thead>
<th>Product No.</th>
<th>Product Name</th>
<th>T.Qty</th>
<th>Item No.</th>
<th>Item Name</th>
<th>Qty </th>
</thead>
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
while($GetItems = $res->fetch_assoc()){
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php } ?>
</tr>
</tbody>
</table>
but the items displayed beside each other not below.
You have a problem in your html table code. You should close the tag for each row you have, and, in case it is not the first line for that element, insert 3 cells with no data:
<tbody>
<tr>
<td><?php echo $Product_no ?></td>
<td><?php echo $Product_name?></td>
<td><?php echo $TQty ?></td>
<?php
// my problem is here
$Items= $connect->prepare("Query Statment?");
$Items->execute();
$res = $Items->get_result();
$i=0;
while($GetItems = $res->fetch_assoc()){
if ($i!=0){
echo "<td></td><td></td><td></td>";
}
?>
<td><?php echo GetItems['Item_no'];?></td>
<td><?php echo GetItems['Item_name']; ?></td>
<td><?php echo GetItems['Qty']; ?></td>
<?php
echo "</tr>";
$i++;
} ?>
</tbody>

adding inline CSS in datatable row on condition not working

I tried to highlight records whose certain columns have some values other than NULL. I'm using dataTable plugin.
<table class="table table-striped table-hover" id="checkin-checkout-record-table">
<thead>
<tr>
<th>Employee Name</th>
<th>Check-in-date</th>
<th>Check-in-time</th>
<th>Check-out-date</th>
<th>Check-out-time</th>
<th class="col-lg-2">Late Check-in Remarks</th>
<th class="col-lg-2">Early Check-out Remarks</th>
</tr>
</thead>
<tbody>
<?php
foreach ($checkinCheckoutList as $member): ?>
<tr <?php
if(isset($member['early_checkout_remarks']) ||isset($member['delayed_checkin_remarks']))
{?>
style="background-color:red;"
<?php } ?> >
<td><?php echo $member['fullname'] ?></td>
<td> <?php echo $member['checkin_date']; ?></td>
<td> <?php echo $member['checkin_time']; ?></td>
<td><?php echo $member['checkout_date']; ?></td>
<td><?php echo $member['checkout_time']; ?></td>
<td><?php echo $member['delayed_checkin_remarks']; ?></td>
<td><?php echo $member['early_checkout_remarks']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
But the result is not as expected. Some records are not highlighted. This works well with other normal table. Please help.

Display the rows from query in table

I have this code to get all the data from my products table. It's working great like this:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row['id'];
echo $row['country'];
echo $row['price'];
echo $row['games'];
echo $row['plus'];
echo $row['buylink'];
echo "<br/>";
}
But I want these results to be shown in a table inside the body. I tried something but it's not working.
This is how the table looks :
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
</tbody>
<?php
if ($result->num_rows > 0) {
?>
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php }?>
</tbody>
</table>
<?php
}else {
// if there is no result... Code something here.
}
?>
Youre getting close. You need to have the loop spit out the rows into the html.
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_row()) {
echo "<tr>";
foreach ($row as $item) {
echo "<td>$item</td>";
}
echo"</tr>";
}
?>
</tbody>
also instead of using a associative array and indexing into it (ie. $row['country']), You can use a normal array and a foreach loop to cut down on the code.
LE:
Each html table row is created based on each database's table row. So, you just need to loop through the result set (that while), and for each iteration of the loop, you need to create a new html table row:
<table class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>ID</th>
<th>Country</th>
<th>Price</th>
<th>Games</th>
<th>Plus</th>
<th>Buy Link</th>
</tr>
</thead>
<tbody>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['price']; ?></td>
<td><?php echo $row['games']; ?></td>
<td><?php echo $row['plus']; ?></td>
<td><?php echo $row['buylink']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

How to avoid displaying duplicated values or overwrite duplicated values in php foreach loop in table?

I'm currently working on codeigniter. I want to display a value that is not been duplicated or overwrite a duplicated value from mysql database into the datatable of php foreach loop.
Here is the table code:
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
foreach($EMPLOYEES as $employee){?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php } ?>
</tbody>
</table>
Why not just use -
$EMPLOYEES = array_unique($EMPLOYEES);
Try this
<table class="table table-striped responsive-utilities jambo_table" id="myTable">
<thead>
<tr class="headings">
<th>Employee No.</th>
<th>Username</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $emp='';
foreach($EMPLOYEES as $employee){
if($emp!=$employee->username){ # if username contain duplicate values
?>
<tr>
<td><?php echo $employee->empnum; ?></td>
<td><?php echo $employee->username; ?></td>
<td><?php echo $employee->name; ?> <?php echo $employee->lastname; ?></td>
<td><?php
if ($employee->hasClockOut==1){
echo '<a class="label label-danger">Inactive</a>';
}else {
echo '<a class="label label-success">Active</a>';
}
?></td>
</tr>
<?php }$emp=$employee->username;} ?>
</tbody>
</table>

Categories