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>
Related
I am updating a plugin for WordPress using .php files. I have an update that requires a table and pagination. I'm trying to use jQuery for this.
I followed the datatable instructions but I am not seeing any changes applied. What am I doing wrong? I have added the jQuery to use the correct table id. Nothing seems to work doing this.
I am attempting to run through the data and allow the page to paginate. I do not care about having the option for user to select number of items on page at given time.
I am looking for an easy way to paginate a HTML table. Nothing I've tried seems to work
jQuery(document).ready(function() {
jQuery('#engine-search-table').DataTable();
});
<div class="parts-table">
<table id="engine-search-table" class="display">
<thead>
<tr id="header-row">
<th class="table-header">
<h4>Manufacturer</h4>
</th>
<th class="table-header">
<h4>Model</h4>
</th>
<th class="table-header">
<h4>Description</h4>
</th>
<th class="table-header">
<h4>Series</h4>
</th>
<th class="table-header">
<h4>Engine</h4>
</th>
<th class="table-header">
<h4>Years</h4>
</th>
<th class="table-header">
<h4>Begin Serial</h4>
</th>
<th class="table-header">
<h4>Engine Serial</h4>
</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($parts)) {
foreach ($parts as $part) {
$meta = get_post_meta($part->ID); ?>
<tr id="data" class="parts-row">
<td class="part-container-title">
<?php printCFTVal($meta, 'manufacturer');?>
</td>
<td class="part-container-manufacturer">
<a href='<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'model'); ?>
</a>
</td>
<td class="part-container-category">
<?php printCFTVal($meta, 'description'); ?>
</td>
<td class="part-container-excerpt">
<?php echo str_replace('OVERVIEW', '', get_the_excerpt($part)); ?>
</td>
<td class='part-container-button'>
<?php echo ($part->guid); ?>'>
<?php printCFTVal($meta, 'enginemodel'); ?>
</td>
<td>
<?php printCFTVal($meta, 'years');?>
</td>
<td>
<?php printCFTVal($meta, 'beginSerial');?>
</td>
<td>
<?php printCFTVal($meta, 'engineserial');?>
</td>
</tr>
<?php }
} else {
echo '<p>No parts found.</p>';
} ?>
</tbody>
</table>
</div>
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.
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>
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.
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>