I am trying to store information from my database about a customers booked flights into a HTML table.
Here is the HTML code:
<h2> Your Flights: </h2>
<table>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<tr>
<td> <?php echo $row['flight_number'];?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; }?> </td>
</tr>
</table>
Currently it is repeating the table headers over again for each record from my database. I tried moving my while loop after my table headers however that just made the information go everywhere.
<h2> Your Flights: </h2>
<table>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<?php while($row = $result->fetch_assoc()){ ?>
<tr>
<td> <?php echo $row['flight_number'];?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; ?> </td>
</tr>
<?php }?>
</table>
The while loop have to surround the <tr> including the data:
<h2> Your Flights:</h2>
<table>
<tr>
<th> flight number </th>
<th> flight status </th>
<th> flight destination </th>
<th> booking date </th>
<th> flight date </th>
</tr>
<?php while($row = $result->fetch_assoc()) { ?><!-- start of while loop -->
<tr>
<td> <?php echo $row['flight_number']; ?> </td>
<td> <?php echo $row['status']; ?> </td>
<td> <?php echo $row['to_airport']; ?> </td>
<td> <?php echo $row['booking_datetime']; ?> </td>
<td> <?php echo $row['flight_datetime']; ?> </td>
</tr>
<?php } ?><!-- end of while loop -->
</table>
You current code displays the header for each line because the while starts before the header. Your table is broken because you end the while loop inside the last column (after last value). So the closing tag of the last column and the row itself is missing.
Related
I have a table which Displays the book list from mysql Database and search form where user can search books.
I"m looking forward to show the book list as per title and author when user search for by input value of title and author and display " NO Books by name or author" as echo when there is no any record.
My code is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
Go Back
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
My Search form is
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<form action = "DisplayBooks.php" method="get">
<br>
<center>Enter the title of the book to be searched :
<input type="text" name="search" size="48">
<br></br>
<input type="submit" value="submit">
<input type="reset" value="Reset">
</center>
<br>
</form>
</body>
</html>
But it sucessfully displays list of books but when there is no any record ..it don't launch echo.
ps. How can I add link button such that it shows Back to search results to navigate user to Searchform and user can go back to previous form.
You have duplicated your check for "no results":
if(mysqli_num_rows($result)>0)if(mysqli_num_rows($result)>0)
Remove one.
Try This:
<!DOCTYPE HTML>
<html>
<body bgcolor="87ceeb">
<center><h2>Central Department of Physics</h2></center>
<br>
<?php
include("DBConnection.php");
$search = isset($_REQUEST["search"]) ? $_REQUEST["search"] : '';
$query = "select ISBN,Title,Author,Edition,Publication from book_info where author like '%$search%' or title like '%$search%'";
//search with a book name in the table book_info
$result = mysqli_query($db,$query);
?>
<table border="2" align="center" cellpadding="5" cellspacing="5">
<tr>
<th> ISBN </th>
<th> Title </th>
<th> Author </th>
<th> Edition </th>
<th> Publication </th>
</tr>
<?php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["ISBN"];?> </td>
<td><?php echo $row["Title"];?> </td>
<td><?php echo $row["Author"];?> </td>
<td><?php echo $row["Edition"];?> </td>
<td>Click Me</td>
</tr>
<?php
}
}
else{ ?>
<tr>
<td colspan="5">
<center>No books found in the library by the name $search </center>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>
<br>
This question already has answers here:
table header is repeating from a for loop in php
(2 answers)
Closed 4 years ago.
im trying to fetch data from mysqli and display it on the table. everything is find expect for the table heading where if there's 5 data, it will repeat the header 5 times for each data.
Any idea where im doing wrong? I'm suspecting on the if loop on my php code.
Below is the code.
PHP Code:
<div class="table-responsive">
<?php
//get rows query
$query = $db->query("SELECT * FROM register");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<table class="table">
<thead class=" text-primary">
<th>
Name
</th>
<th>
Age
</th>
<th>
Gender
</th>
<th>
Passport Number
</th>
<th>
Nationality
</th>
<th>
Citizen
</th>
<th>
Address
</th>
<th>
Caught Place
</th>
<th>
Current Status
</th>
<th>
Based At
</th>
<th>
Phone Number
</th>
<th>
Family Phone Number
</th>
<th>
Company Phone Number
</th>
</thead>
<tbody>
<tr>
<td>
<?php echo $row["Name"]; ?>
</td>
<td>
<?php echo $row["Age"]; ?>
</td>
<td>
<?php echo $row["Gender"]; ?>
</td>
<td>
<?php echo $row["PassportNumber"]; ?>
</td>
<td>
<?php echo $row["Nationality"]; ?>
</td>
<td>
<?php echo $row["Citizen"]; ?>
</td>
<td>
<?php echo $row["Address"]; ?>
</td>
<td>
<?php echo $row["CaughtPlace"]; ?>
</td>
<td>
<?php echo $row["CurrentStatus"]; ?>
</td>
<td>
<?php echo $row["BasedAt"]; ?>
</td>
<td>
<?php echo $row["PhoneNumber"]; ?>
</td>
<td>
<?php echo $row["FamilyPhoneNumber"]; ?>
</td>
<td>
<?php echo $row["CompanyPhoneNumber"]; ?>
</td>
</tr>
</tbody>
</table>
<?php } }else{ ?>
<p>Data(s) not found</p>
<?php } ?>
</div>
Table output:
When you said "I'm suspecting on the if loop on my php code", you were correct. Just move the table header, start of the body & end of the body out of the loop, leaving only the table rows within ...
// Moved this from inside the loop
<table class="table">
<thead class=" text-primary">
<th>
Name
</th>
<th>
Age
</th>
<th>
Gender
</th>
<th>
Passport Number
</th>
<th>
Nationality
</th>
<th>
Citizen
</th>
<th>
Address
</th>
<th>
Caught Place
</th>
<th>
Current Status
</th>
<th>
Based At
</th>
<th>
Phone Number
</th>
<th>
Family Phone Number
</th>
<th>
Company Phone Number
</th>
</thead>
<div class="table-responsive">
<?php
//get rows query
$query = $db->query("SELECT * FROM register");
if($query->num_rows > 0){
while($row = $query->fetch_assoc()){
?>
<tbody>
<tr>
<td>
<?php echo $row["Name"]; ?>
</td>
<td>
<?php echo $row["Age"]; ?>
</td>
<td>
<?php echo $row["Gender"]; ?>
</td>
<td>
<?php echo $row["PassportNumber"]; ?>
</td>
<td>
<?php echo $row["Nationality"]; ?>
</td>
<td>
<?php echo $row["Citizen"]; ?>
</td>
<td>
<?php echo $row["Address"]; ?>
</td>
<td>
<?php echo $row["CaughtPlace"]; ?>
</td>
<td>
<?php echo $row["CurrentStatus"]; ?>
</td>
<td>
<?php echo $row["BasedAt"]; ?>
</td>
<td>
<?php echo $row["PhoneNumber"]; ?>
</td>
<td>
<?php echo $row["FamilyPhoneNumber"]; ?>
</td>
<td>
<?php echo $row["CompanyPhoneNumber"]; ?>
</td>
</tr>
</tbody>
</table>
<tbody>
<?php } }else{ ?>
<p>Data(s) not found</p>
<?php } ?>
// Moved this from inside the loop
</tbody>
</table>
</div>
if(isset($_POST["Search"]))
{
$resz=mysqli_query($link,"select*from tblschedule where Destination =
'$_POST[Destination]' and Origin = '$_POST[Origin]' and Date =
'$_POST[Date]'");
Code for search
$count=mysqli_num_rows($resz);
if($count>=1){
if there's a row to search then display
echo "</br></br>
<div class=container>
<div class=table-responsive>
<table class = table>
<tr>
<th class=col-xs-2> TripCode </th>
<th class=col-xs-2> Origin </th>
<th class=col-xs-2> Destination </th>
<th class=col-xs-2> Date </th>
<th class=col-xs-2> Time </th>
<th class=col-xs-2> Action </th>
</tr>";
while($row = mysqli_fetch_array($resz))
{?>
<tr>
<td> <?php echo $row['TripCode']; ?> </td>
<td> <?php echo $row['Origin']; ?> </td>
<td> <?php echo $row['Destination']; ?> </td>
<td> <?php echo $row['Date']; ?> </td>
<td> <?php echo $row['Time']; ?> </td>
<td> <?php echo "<a href=webbook.php?
id=".$row['TripCode'].">Book</a><br />"; ?></td>
</tr>
</table>
</div>
</div>
<?php }} else{ echo "<center>No results were found.</center>"; }}
?>
Code is successfully getting the rows I want but only one row has a table and the others don't have. I want each row to have a table of their own.
seems like your <table> and the two <div> tag is within the while loop. remove it and put outside of the loop.
Not sure if this is the best way to do this.. But..
I've got a PHP file calling an API and doing a foreach loop to get all the data I want. Previously I was calling this from my html file and putting it into a table, the problem was that it was all appearing as technically the same row and that made formatting hard.
This is what I used to have:
<div class="bgimg2 w3-container">
<table class="w3-table w3-medium w3-text-white">
<tr class="w3-blue-gray">
<th> <h5> Name </h5> </th>
<th> <h5> Size </h5> </th>
<th> <h5> % </h5> </th>
</tr>
<tr>
<th> <h4> <div id=queue> </h4> </th>
<th> <h4> <div id=queueSize> </h4> </th>
<th> <h4> <div id=queuePercent> </h4> </th>
</tr>
</thead>
</table>
</div>
I want to have each item appearing as it's own row, so I'm trying to do a php foreach loop directly in the table.
This is what I've got currently for the table:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row){
?>
<tr>
<td> <?php $row['filename'] ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
It's not working and the file isn't even loading. Not sure where I've gone wrong.
Any help would be greatly appreciated.
Seems you missed a detail in not echoing out your variable. I would also suggest, and perhaps introduce you to alternative PHP syntax which is good for when you want to tightly integrate a few php blocks into html as you are doing here:
<table class="w3-table w3-medium w3-text-white">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php
require_once ('/api/sabapiQueue.php');
foreach ($apiResultDataShift as $row):
?>
<tr>
<td><?= $row['filename'] ?></td>
</tr>
<?php
endforeach;
?>
</tbody>
</table>
Alternatively, as per the echo manual page you could just change this line:
<td><?php echo $row['filename']; ?></td>
I need to display data from a database in a table, and I was wondering if you can create 2 separate tables based on a variable.
I have code to display it all in one table:
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["name"] ?> </td>
<td> <?php echo $row["event"] ?> </td>
</tr>
<?php } ?>
</table>
So each person has an option of 2 fundraisers. Is there a way to create 2 separate tables based on the event variable? I know I could create another table in the database, but I want to keep all the data in one area.
I only know basic PHP and any help is appreciated.
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["name"] ?> </td>
</tr>
<?php } ?>
</table>
<table>
<tr>
<th> Name </th>
<th> Event </th>
</tr>
<?php
while($row = $result->fetch_assoc())
{?>
<tr>
<td> <?php echo $row["event"] ?> </td>
</tr>
<?php } ?>
</table>