How to display search results in one table with PHP & MySQL? - php

I have the code to do the search. But the search results are not in the same table. All search results appear in a different table. How do I make them appear in one table?
Screenshot :
And here's my code :
> search.php
<?php
$query = $_GET['query'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_assoc($raw_results)){
?>
<table width="107%" class="view">
<thead>
<tr>
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
<?php
}
}
else{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
So how to make the search results appear just in one table? Am I wrong to put the table code? Or something else? And one more question, How to add numbers for each result? Thank you in advance for your time and the help.

Move <table> tag outside your while loop and also add a <TR> tag inside the while loop and dont forget to close tag.
For Serial Number you have to introduce another variable as a counter. In the code given below i added $i. Change it if you had already used $i in your code.
I think after changing your code will look like
?>
<table width="107%" class="view">
<thead>
<tr>
<th>SN</th>//new Line
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<?php
$i=1;//new line
while($results = mysql_fetch_assoc($raw_results)){
?>
<tr>
<td><?php echo $i; ?> </td>//new line
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
</tr>
<?php
$i++;//new line
}
?>
</table>
<?php
}
else{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}

Move the <table> tag outside of your while loop.
Should be like this..
echo "<table width="107%" class=/"view/">";
while($results = mysql_fetch_assoc($raw_results)){
?>
<!-- Comment this
<table width="107%" class="view">
-->
<thead>

use this
<?php
$query = $_GET['query'];
$min_length = 1;
if(strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM barang WHERE (`tanggal` LIKE '%".$query."%')") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0)
{
?>
<table width="107%" class="view">
<thead>
<tr>
<th width="180">Tanggal</th>
<th width="150">Barang Masuk</th>
<th width="90">Bijih Keluar</th>
<th width="120">Kantong Hitam Keluar</th>
<th width="120">Kantong Putih Keluar</th>
<th width="90">Stok Bijih</th>
<th width="90">Stok Kantong Hitam</th>
<th width="90">Stok Kantong Putih</th>
<th width="130">Catatan</th>
</tr>
</thead>
<?php
while($results = mysql_fetch_assoc($raw_results))
{
<tr>
<td><?php echo $results['tanggal']; ?></td>
<td><?php echo $results['barang_in']; ?></td>
<td><?php echo $results['bijih_out']; ?></td>
<td><?php echo $results['htm_out']; ?></td>
<td><?php echo $results['pth_out']; ?></td>
<td><?php echo $results['bijih']; ?></td>
<td><?php echo $results['kantong_htm']; ?></td>
<td><?php echo $results['kantong_pth']; ?></td>
<td><?php echo $results['note']; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
else
{ // if there is no matching rows do following
echo "Hasil tidak bisa ditemukan atau tidak ada di dalam database.";
}
}
else
{
echo "Minimum length is ".$min_length;
}
?>

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 } ?>
`

Give data from database in table numbers

I have a foreach in the table. I foreach a row for every row in my database. So my database has got 10 records, I and my table is showing all those records underneath eachother. So far so good.
I want to number them, from 1 to 10, displayed in front of every row.
This is my table:
<table class="table table-striped mt-3">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Team</th>
<th scope="col">Player</th>
<th scope="col">P</th>
<th scope="col">W</th>
<th scope="col">D</th>
<th scope="col">L</th>
<th scope="col">GF</th>
<th scope="col">GA</th>
<th scope="col">GD</th>
<th scope="col">P</th>
</tr>
</thead>
<tbody>
<?php $count = count($table); ?>
<?php foreach($table as $t): ?>
<tr>
<td><?php for($i = 1; $i < $count; $i++;)
{
echo $i; ?>}
</td>
<td><?php echo $t['team']; ?></td>
<td><?php echo $t['speler']; ?></td>
<td><?php echo $t['gw']; ?></td>
<td><?php echo $t['w']; ?></td>
<td><?php echo $t['g']; ?></td>
<td><?php echo $t['v']; ?></td>
<td><?php echo $t['dv']; ?></td>
<td><?php echo $t['dt']; ?></td>
<td><?php echo $t['ds']; ?></td>
<td><?php echo $t['points']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This is my method
public function fifaLeagueTable() {
$getTable = "SELECT * FROM fifa_league ORDER BY points DESC";
$table = $this->model->readAll($getTable);
$count = count($table);
include('app/views/fifaLeagueTable.php');
}
If I var_dump the $count, I receive int(10). So it's counting the amount of rows and I have access to the 10. I am getting a white page, so there might be something wrong in the for loop or something. What did I do wrong?
You just need to create one more variable and its done. Here is updated code :
<table class="table table-striped mt-3">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Team</th>
<th scope="col">Player</th>
<th scope="col">P</th>
<th scope="col">W</th>
<th scope="col">D</th>
<th scope="col">L</th>
<th scope="col">GF</th>
<th scope="col">GA</th>
<th scope="col">GD</th>
<th scope="col">P</th>
</tr>
</thead>
<tbody>
<?php $count = count($table); $num = 1; ?>
<?php foreach($table as $t): ?>
<tr>
<td><?php echo $num; ?>
</td>
<td><?php echo $t['team']; ?></td>
<td><?php echo $t['speler']; ?></td>
<td><?php echo $t['gw']; ?></td>
<td><?php echo $t['w']; ?></td>
<td><?php echo $t['g']; ?></td>
<td><?php echo $t['v']; ?></td>
<td><?php echo $t['dv']; ?></td>
<td><?php echo $t['dt']; ?></td>
<td><?php echo $t['ds']; ?></td>
<td><?php echo $t['points']; ?></td>
</tr>
<?php $num++ ; endforeach; ?>
</tbody>
</table>

Display php data in a html table using while loop

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>";
}
?>

change bootstrap label depend on words from mysql row

I do have some problems with my code.
I would like to change the color on my lables if it goes from one status to another depending on the world from mysql db.
First of all, I have the code:
<table class="table responsive">
<div id="employee_table">
<table class="table">
<tr>
<th width="10%">ARK ID</th>
<th width="20%">User</th>
<th width="45%">Header</th>
<th width="10%">Status</th>
<th width="20%">Priority</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["ark_id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><a href="read.php?id=<?php echo $row['id']; ?>"><?php echo $row["overskrift"]; ?></td>
<td><?php echo $row["prioritet"]; ?></td>
<td><?php echo $row["status"]; ?></td>
</tr>
<?php
}
?>
</table>
Second of all, I'll have Status and priority to change label color like this
If priority is LOW then Green Label
If priority is MEDIUM then Blue
if priority is HIGH then RED
... The same function to the Status Pending .... And so on..
I hope someone could help me :)
Thanks!
You could check the priority on the beginning of the while loop:
<table class="table responsive">
<div id="employee_table">
<table class="table">
<tr>
<th width="10%">ARK ID</th>
<th width="20%">User</th>
<th width="45%">Header</th>
<th width="10%">Status</th>
<th width="20%">Priority</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
if($row["prioritet"] == "LOW") {
$color = '#000000'; // choose color
}
else if($row["prioritet"] == "MEDIUM") {
$color = '#888888'; // choose color
}
else {
$color = '#ffffff'; // choose color
}
?>
<tr>
<td><?php echo $row["ark_id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><a href="read.php?id=<?php echo $row['id']; ?>"><?php echo $row["overskrift"]; ?></td>
<td style="color:<?php echo $color?>"><?php echo $row["prioritet"]; ?></td>
<td style="color:<?php echo $color?>"><?php echo $row["status"]; ?></td>
</tr>
<?php
}
?>
</table>
hope below code help you
<table class="table responsive">
<div id="employee_table">
<table class="table">
<tr>
<th width="10%">ARK ID</th>
<th width="20%">User</th>
<th width="45%">Header</th>
<th width="10%">Status</th>
<th width="20%">Priority</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
if($row["prioritet"] == "LOW") {
$priority_color = '#009933'; // low priority color
}
else if($row["prioritet"] == "MEDIUM") {
$priority_color = '#0099ff'; // Medium priority color
}
else if($row["prioritet"] == "HIGH"){
$priority_color = '#ff0000'; // High priority color
}else{
$priority_color = '#ffffff'; // default color
}
?>
<tr>
<td><?php echo $row["ark_id"]; ?></td>
<td><?php echo $row["name"]; ?></td>
<td><a href="read.php?id=<?php echo $row['id']; ?>"><?php echo $row["overskrift"]; ?></td>
<td><?php echo $row["prioritet"]; ?></td>
<td bgcolor="<?php echo $priority_color; ?>"><?php echo $row["status"]; ?></td>
</tr>
<?php
}
?>
</table>

Not able to display image from database on webpage

I am not able to display my full image on my webpage from database
my codemy image is goint to database
$con= mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT addplace, stayamount, foodamount, airlinesamount, noofdays,noofnights,
SUM(stayamount + foodamount + airlinesamount) AS totalamount,choose
FROM adddetails GROUP BY packageid");
?>
<div class="container">
<CENTER><h2>view packages</h2>
</CENTER>
<table class="table table-bordered">
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>no of days</th>
<th>no of nights</th>
<th>total amount</th>
<th>image</th>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row['addplace']; ?></td>
<td><?php echo $row['stayamount']; ?></td>
<td><?php echo $row['foodamount'] ;?></td>
<td><?php echo $row['airlinesamount'] ;?></td>
<td><?php echo $row['noofdays'] ;?></td>
<td><?php echo $row['noofnights'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
<td><?php echo '<img src=data:image/jpeg;base64,'.base64_encode( $row['choose'] ).'>'; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
please rewrite my code and i want my image of 150*150 size to be fitted on my web page in my table
try using this.
<td><img src='".$row['choose']."'/></td>";

Categories