I am trying to display all the data in my database in table format. I am using a while loop here. The first row looks good, but the subsequent rows don't display like a table.
<?php
require_once"../connect.php";
$sql = "SELECT * FROM history WHERE LOWER(trackingNo)=LOWER('$track') ORDER BY dates DESC";
$result = $con -> query($sql);
if ($result -> num_rows > 0) {
// output data of each row
echo "<table class='striped ex2 centered'>[The way my output looks right now][1]
< tr > <th class='trcol' > DATE < /th> <th class='trcol'>TIME</th > <th
class='trcol' > LOCATION < /th> <th class='trcol'>STATUS</th > <th
class='trcol' > REMARKS < /th></tr >";
while ($row = $result -> fetch_assoc()) {
$dates = $row['dates']; enter code here
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo" <tr><td>".$dates."</td> <td>".$tim."</td>
< td > ".$location." < /td> <td>".$status."</td >
<td>".$remarks." < /td></tr > </table>";
}
}
else {
echo "0 results";
}
$con -> close();
?>
This is how it currently looks like
The problem is that your closing table tag is inside the while-loop, so no iterations after the first will actually be part of the table. Edit your code to look like this:
while ($row = $result->fetch_assoc()) {
$dates = $row['dates'];
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo "<tr>";
echo "<td>".$dates."</td>";
echo "<td>".$tim."</td>";
echo "<td>".$location."</td>";
echo "<td>".$status."</td>";
echo "<td>".$remarks."</td>";
echo "</tr>";
}
echo "</table>";
You just need to take your </table> outside of the while loop.
So, it would be -
while ($row = $result -> fetch_assoc()) {
$dates = $row['dates'];
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo" <tr>
<td>".$dates."</td> <td>".$tim."</td>
<td> ".$location." </td> <td>".$status."</td >
<td>".$remarks." </td>
</tr>"; // <-- removed table closing
}
echo "</table>"; // <-- close table here
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Why my while loop not working, I also have a while loop on the other PHP page, but there's only one page that doesn't work with PHP's while loop. But it does not contain any errors. Here's my code:
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
if($num <= 0){
echo "<h2>No records found.</h2>";
}
$x=0;
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
You were reading your first result row and incorrectly using that as a count of resulted rows, then ignoring its content.
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$num = mysqli_fetch_array($result);
// this reads the first row of your result set and then of course gets lost
//$num = mysqli_fetch_array($result);
// use mysqli_num_rows instead
if(mysqli_num_rows($result) <= 0){
echo "<h2>No records found.</h2>";
} else {
$x=0;
// now this will get the first row, which you must have been missing before
while($row = mysqli_fetch_assoc($result)){
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
$sqlquery = "SELECT * FROM tbl_accredited";
$result = $con->query($sqlquery);
$x=0;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$x++;
echo '
<tr>
<td>'.$x.'</td>
<td>'.$row['permitno'].'</td>
<td>'.$row['boarding_optr'].'</td>
<td>'.$row['boarding_addr'].'</td>
<td>'.$row['orno'].'</td>
<td>'.$row['boarding_name'].'</td>
</tr>
';
}
}
else {
echo "<h2>No records found.</h2>";
}
I currently have this code set up:
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
$data_exist = true;
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
}
}
And then:
<?php if ($data_exist){?>
<p><?php echo $id ?></p>
<p><?php echo $teacher_set?></p>
<p><?php echo $name?></p>
<p><?php echo $description?></p>
<?php
}?>
However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?
I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.
You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.
You need to print each time you read a row from the database.
about the styles, you can represent it in many ways. In the code below I present it in a table.
<table>
<thead>
<tr>
<th>id</th>
<th>teacher set</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
// you need to print the output now otherwise you will miss the row!
// now printing
echo "
<tr>
<td>".$id."</td>
<td>".$teacher_set."</td>
<td>".$name."</td>
<td>".$description."</td>
</tr>";
}
}
else // no records in the database
{
echo "not found!";
}
?>
</tbody>
</table>
</body>
</html>
I have set up a for and while loop to print through the contents of my database for my menu system. It works fine, however, the places to where each of the table contents is displayed is one below where it should be. See this picture below:
The problem is that the items which are showing under "Main Courses" are supposed to be under the "Starters" section.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $countArray; $i++){
echo "<h1 id='starters' class='head-font text-center head'>$result_array[$i]</h1>";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo
"
<div id='hide'>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo
"<tr>" .
"<td>$name - <small>$description</small></td>" .
"<td class='text-right'>" . "£" . $price . "</td>" .
"</tr>";
}
echo "</tbody>
</table>
</div>";
}
for($j = 1; $j < $numRows+1; $j++){
echo $j;
}
// print_r($result_array[2]);
?>
I'm new to php and I'm stuck. I have a report that allows a supervisor to show the working hours of the operators. I can show all the hours from the db, but I can't make a sum of all the hours. I want to display one more row that shows the total hours. How can I do that?
<?php
if(isset($_SESSION['username'])) {
$username = $_SESSION['username'];
} else {
header('Location: index.php');
die();
}
include('connect.php');
#$oret_e_punes = $_POST['oret_e_punes'];
#$grupi = $_POST['grupi'];
#$data_e_inserimit = $_POST['data_e_inserimit'];
#$data_e_inserimit_2 = $_POST['data_e_inserimit_2'];
$sql = "select grup_name from grupi";
$result = mysqli_query($dbCon, $sql);
if(!$result) {
die("Error");
}
?>
//html form
<?php
if(isset($_POST['insert'])) {
$insert = "select * from ore where grupi='$grupi' and (data between '$data_e_inserimit' and '$data_e_inserimit_2' and ore !=0)";
$result_insert = mysqli_query($dbCon, $insert);
if(!$result_insert) {
die("Error");
}
echo "<table id='table'>
<tr id='main'>
<td>Operatori</td>
<td>Grupi</td>
<td>Oret e punes</td>
<td>Data</td>
</tr>";
while ($row = mysqli_fetch_assoc($result_insert)) {
$id = $row['id'];
echo "<tr id='sub'>
<td>".$row['usr']."</td>
<td>".$row['grupi']."</td>
<td>".$row['ore']."</td> ---->working hours
<td>".$row['data']."</td>
</tr>";
$id++;
}
echo "</table>";
$_SESSION['$id'] = #$id;
}
?>
have a variable for hours before loop starts. Add in it hour of each record. After loop will end you will have your total hours
echo "<table id='table'>
<tr id='main'>
<td>Operatori</td>
<td>Grupi</td>
<td>Oret e punes</td>
<td>Data</td>
</tr>";
$hours = 0;
while ($row = mysqli_fetch_assoc($result_insert)) {
$id = $row['id'];
echo "<tr id='sub'>
<td>".$row['usr']."</td>
<td>".$row['grupi']."</td>
<td>".$row['ore']."</td> ---->working hours
<td>".$row['data']."</td>
</tr>";
$hours += $row['ore'];
$id++;
}
echo "<tr><td colspan='4'>Total</td><td>$hours</td></tr>";
echo "</table>";