PHP - Displaying database data in 2 different tables - php

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>

Related

creating a table to store data from SQL database

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.

How to convert foreach() Mysql select into HTML form

I am working on a small course registration system. I have a table called 20182019carryovertbl I select students carry over fron the table and its working well. I need to convert the selected info to an HTML form with submit button( This will go into another table 20192020coursestbl). How d i set name for my inputs
<table>
<thead>
<tr>
<th>Register course</th>
<th>Course Title</th>
<th>Corse Code</th>
<th>Unit</th>
<th>Lecturer</th>
</tr>
</thead>
<tbody>
<?php $sql = "SELECT * from 20182019carryovertbl WHERE matricno = '$matno' ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr>
<td> <input type="checkbox" name="reg1" value="1" class="custom-control-input"></td>
<td>
<?php echo htmlentities($result->coursetitle);?>
</td>
<td>
<?php echo htmlentities($result->coursecode);?>
</td>
<td>
<?php echo htmlentities($result->unit);?>
</td>
<td>
<?php echo htmlentities($result->lecturer);?>
</td>
</tr>
<?php $cnt=$cnt+1; }} ?>
</tbody>
</table>
you can use your unique key from database like Id:
<td>
<input type="checkbox" name="reg<?php echo $result->id;" value="1"class="custom-control-input">
</td>
and it's gonna be unique name

how to remove redudant heading on table [duplicate]

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>

Im looping to display the rows in the database but other rows are not included inside the html table

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.

Including a PHP file then PHP foreach loop in a html table

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>

Categories