I have this problem in displaying the result in td inside table. I want to display all rows coming from the database.
I have code but it results only one row.
<table class="table table-striped table-bordered table-hover" id="dataTables">
<thead>
<tr>
<th>ID</th>
<th>Role</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost", "root", "", "trackingsys");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * from role" ;
$result = mysqli_query($con,$query);
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['RoleName'] . "</td>";
echo "<td>" . $row['RoleDesc'] . "</td>";
echo '</tr>';
}
mysqli_close($con); ?>
</tbody>
</table>
I edited this code and put the ending tag </td>. But still it results only one row.
Related
I want to generate Bootstrap 5 table .table-striped using data from MySQL, table: [users]
The table in plain HTML looks fine, but as soon as I generate it using PHP all styling disappears
PHP Code:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
echo "<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['rank'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
echo " </tbody>";
}
echo "</table>";
mysqli_close($link);
?>
and I have <div class='table-responsive'> before <?php and </div> after ?>
I would be glad if you could explain me where I am making a mistake and I would like the table to have the same styling as in pure HTML
EDIT:
rendered HTML
<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th>#</th>
<th>Username</th>
<th>Rank</th>
<th>Created at</th>
</tr>
</thead><tbody><tr><td>39</td><td></td><td>julia</td><td>2021-12-20 00:42:14</td></tr> </tbody><tbody><tr><td>40</td><td>test</td><td>user</td><td>2021-12-20 02:35:37</td></tr> </tbody><tbody><tr><td>41</td><td>testt</td><td>user</td><td>2021-12-20 17:22:43</td></tr> </tbody></table></div>
You are building a new tbody for every row. You should move the tbody creation outside the while loop. You also only need 1 echo, rather than echoing every line. I would do:
<?php
$result = mysqli_query($link,"SELECT * FROM users ORDER BY id ASC");
$output = "<div class='table-responsive'>
<table class='table table-striped'>
<thead>
<tr>
<th style='background-color:#ad8c70'>#</th>
<th style='background-color:#ad8c70'>Username</th>
<th style='background-color:#ad8c70'>Rank</th>
<th style='background-color:#ad8c70'>Created at</th>
</tr>
</thead>
<tbdoy>";
while($row = mysqli_fetch_array($result))
{
$output .= "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['username'] . "</td>
<td>" . $row['rank'] . "</td>
<td>" . $row['created_at'] . "</td>
</tr>";
}
mysqli_close($link);
$output .= '</tbody></table>';
echo $output;
?>
After writing a code to echo the result of my mysql to my database table, now am trying to get the selected row to be on my update page as as as show some information from the table and as well as being able to update the table at the same time
Am getting confused with all the online help i have actually read in other to make this work, but i believe i wrote the right code but might just be missing something in which i believe some on can help me with.
here is index.php that display the table from my database
<?php
//include auth.php file on all secure pages
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php require_once('header.php')?>
<div class="container content">
<table id="myTable" class="table table-striped" >
<thead>
<tr>
<th>ConsignmentNo</th>
<th>Origin</th>
<th>Destination</th>
<th>PickupDate</th>
<th>Status</th>
<th >Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($con,"SELECT * FROM consignment");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['consignmentno'] . "</td>";
echo "<td>" . $row['shipmentorigin'] . "</td>";
echo "<td>" . $row['shipmentdestination'] . "</td>";
echo "<td>" . $row['shipmentpickupdate'] . "</td>";
echo "<td>" . $row['shipmentstatus'] . "</td>";
echo "<td><a name='consignmentno' href='update.php?id=".$row['consignmentno']."'>Edit</a></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
</div>
</div>
<?php require_once('footer.php')?>
And here is the
update.phppage that process my request
<?php
require("../db.php");
$track = $_GET['consignmentno'];
$sql = "SELECT * FROM `consignment` WHERE consignmentno='$track'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$consignmentno = $row['consignmentno'];
$shippername = $row['shippername'];
$shipperphone = $row['shipperphone'];
$shipperaddress = $row['shipperaddress'];
$shipperemail = $row['shipperemail'];
$receivername = $row['receivername'];
$receiverphone = $row['receiverphone'];
$receiveraddress = $row['receiveraddress'];
$receiveremail = $row['receiveremail'];
$shipmenttype = $row['shipmenttype'];
$shipmentweight = $row['shipmentweight'];
$shipmentcourier = $row['shipmentcourier'];
$shipmentpackage = $row['shipmentpackage'];
$shipmentmode = $row['shipmentmode'];
$shipmentproduct = $row['shipmentproduct'];
$shipmentquantity = $row['shipmentquantity'];
$shipmentfrieght = $row['shipmentfrieght'];
$shipmentcarrier = $row['shipmentcarrier'];
$departeddate = $row['departeddate'];
$shipmentorigin = $row['shipmentorigin'];
$shipmentdestination = $row['shipmentdestination'];
$shipmentpickupdate = $row['shipmentpickupdate'];
$shipmentstatus = $row['shipmentstatus'];
$shipmentexpected = $row['shipmentexpected'];
$comment = $row['comment'];
}
} else {
echo "NO DETAILS FOR USER";
}
$con->close();
?>
<?php require_once('header.php')?>
<div class="container content">
<script type="text/javascript">
$(document).ready(function(){
txt=$("#UpdateStatus").val();
if(txt=='3')
{
$("#receive").slideDown("slow");
$("#UpdateReceivedBy").removeAttr("disabled");
}
else
{
$("#receive").slideUp("slow");
$('#UpdateReceivedBy').attr('disabled', 'true');
}
$("#UpdateStatus").change(function(){
txt=$("#UpdateStatus").val();
if(txt=='3')
{
$("#receive").slideDown("slow");
$("#UpdateReceivedBy").removeAttr("disabled");
}
else
{
$("#receive").slideUp("slow");
$('#UpdateReceivedBy').attr('disabled', 'true');
}
});
});
</script>
<h2 class="col-md-offset-5">Update Shipment</h2>
<div class="row">
<div class="table-responsive col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><h4><?php echo $consignmentno ; ?></h4>
</th>
<th>
<h2>On Hold</h2>
</th>
</tr>
</thead>
but it is not echoing the $consignmentno cause if it can do that then every other things becomes easier
and here is an image that shows that my index page is working fine
So please help me check were i got it all wrong. Thanks
You must replace update.php?id= with update.php?consignmentno= on line 35 it will resolve your issue
Or use this code in index.php
<?php
//include auth.php file on all secure pages
require("../db.php");
session_start();
if(!isset($_SESSION["username"])){
header("Location: login");
exit(); }
?>
<?php require_once('header.php')?>
<div class="container content">
<table id="myTable" class="table table-striped" >
<thead>
<tr>
<th>ConsignmentNo</th>
<th>Origin</th>
<th>Destination</th>
<th>PickupDate</th>
<th>Status</th>
<th >Actions</th>
</tr>
</thead>
<tbody>
<?php
$result = mysqli_query($con,"SELECT * FROM consignment");
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['consignmentno'] . "</td>";
echo "<td>" . $row['shipmentorigin'] . "</td>";
echo "<td>" . $row['shipmentdestination'] . "</td>";
echo "<td>" . $row['shipmentpickupdate'] . "</td>";
echo "<td>" . $row['shipmentstatus'] . "</td>";
echo "<td><a name='consignmentno' href='update.php?consignmentno=".$row['consignmentno']."'>Edit</a></td>";
echo "</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
</div>
</div>
<?php require_once('footer.php')?>
I am new to PHP coding and don't understand where I have gone wrong, I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields, keeps coming up with Notice: Undefined index: First, Notice: Undefined index: last etc. How do I define my column fields?
Edited: here are links to pictures of my database and the error codes I'm receiving: http://imgur.com/E5uohjr
http://imgur.com/BGDn9SQ
Here is my code:
</head>
$con = mysqli_connect('localhost', 'root', '', 'form_database') or die("Can not connect: " . mysqli_error());
$result = mysqli_query($con,"SELECT first, last, phone, class FROM form_submissions");
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
while($row = mysqli_fetch_assoc($results)){
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
check if you do have results before displaying, them.
<?php
// Create connection
$con = mysqli_connect('localhost', 'root', '', 'form_database');
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM form_submissions";
$result = mysqli_query($con, $sql);
//check if you get any results
if (mysqli_num_rows($result) > 0) {
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No results found";
}
mysqli_close($con);
I am pulling stuff from a DB and populating it into a drop-down select. When the user selects the query, I want it to be displayed in a table (which it is now). But the problem is with the format. I want the headers to be displayed just on the top of the table rather than for every single row. What is wrong with my code? Any ideas/suggestions? Thanks in advance.
Resulting page of code:
Desired format (table headings just on top):
<?php
session_start();
if(!isset($_SESSION['EmployeeID'])){
$URL="Logon.php";
header ("Location: $URL");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Orders Page</title>
</head>
<body>
<h1>Orders Information Page</h1>
<form action="Orders.php" method="post">
<?php
require "Information.php";
try{
$database="Northwind";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
} catch(PDOException $e){
$conn->setArribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Could not open database.";
}
$lname= $_POST['txtFirstName'];
$extension = $_POST['txtLastName'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['OrderID']=$_POST['selOrder'];
} //end if is a post
echo "<select name='selOrder'>";
$sql = $conn->prepare("SELECT * FROM orders WHERE EmployeeID=? ORDER BY CustomerID ASC");
if ($sql->execute(array($_SESSION['EmployeeID']))) {
while ($row = $sql->fetch()) {
echo "\n\t<option value='" . $row['OrderID'] . "'";
if(isset($_SESSION['OrderID']) && $_SESSION['OrderID']== $row['OrderID']){
echo " selected='selected' ";
}
echo ">";
echo "Cust:" . $row['CustomerID'] . " ordered on " . $row['OrderDate'];
echo "</option>";
}
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Submit" />
<?php
if(isset($_SESSION['OrderID'])){
echo "<h3>Order Details</h3><p>";
$sql = $conn->prepare("SELECT * FROM orders, orderdetails, products WHERE orders.OrderID=orderdetails.OrderID AND products.ProductID=orderdetails.ProductID AND orders.OrderID=? ");
if ($sql->execute(array($_SESSION['OrderID']))) {
while ($row = $sql->fetch()) {
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
{
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
echo "</p>";
}
$conn =null;
?>
</form>
</body>
</html>
Maybe I understand the problem very poorly, or do not understand at all, but is
...
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
...
not what you are looking for? Eg displaying the <th>'s only once?
This is not very difficult, you just made a little mistake.
Get the
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
part out of the while loop, just before the while loop, and it will work like how you want it to.
Hope this helps.
move
"<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
(and while you're at it the echo "</table>";)
out of the while loop:
echo "<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
note that you had two extra curly braces that weren't doing anything.
i want to fill two different html tables inside php while loop .My current code populate table 1 when name is not empty($row[name]) . However , how i can declare table2 so it only populate table2 when name is empty ? could any one show me how i can declare 2 different tables and fill both them depending if name value from db is empty or not ?
//table declration
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
foreach ($lines as $line) {
//print_r( $line );
$line = rtrim($line);
$result = mysqli_query($con,"SELECT ID,name,imgUrl,imgPURL FROM testdb WHERE imgUrl like '%$line'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
//echo $result;
if(mysqli_num_rows($result)>0)
{
if(mysqli_num_rows($result)>1)
{
echo "<br>Duplicate in DB:".$line."<br>";
};
while($row = mysqli_fetch_array($result))
{
$totalRows++;
if (!empty($row[name]))
{
echo "<tr>";
echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['imgPURL'] . "</td>";
echo "<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else
{
$emptyNameCounter++;
//fill table2 if name is empty
};
}; // end of while loop
}
else
{
echo "<br>Not Found:".$line."<br>";
};
};// end of for each line loop
echo "</table>";
One other way is append your data in varables for both table one and two and after the loop put the data in tables
here is a demo how you can do that
$table1='';
$table2='';
while($row = mysqli_fetch_array($result))
{
$totalRows++;
if (!empty($row[name]))
{
$table1.="<tr>";
$table1.="<td>" . $row['ID'] ."(".$totalRows. ")</td>";
$table1.="<td>" . $row['name'] . "</td>";
$table1.="<td>" . $row['imgPURL'] . "</td>";
$table1.="<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else
{
$emptyNameCounter++;
$table2.="data to show in table 2";
//fill table2 if name is empty
}
} // end of while loop
if(!empty($table1)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table1;
echo "</table>";
}
if(!empty($table2)){
echo "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
echo $table2;
echo "</table>";
}
The easiest way is to collect the data in an array first, then loop over that array twice.
There are other variations on that theme, but basically they're all about splitting the reading from the database and the rendering into two distinct steps.
Basically something like:
while($row = mysqli_fetch_array($result)) $rows[] = $result;
foreach( $rows as $row ) {
// Render table 1
}
foreach( $rows as $row ) {
// Render table 2
}
Rather than echo the tables out, store each table to a variable. Once the loop is finished, echo out the variables. Example
$tableOne = "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
$tableTwo = "<table border='1'>
<tr>
<th>ID</th>
<th>name</th>
<th>page URL</th>
<th>img URL</th>
</tr>";
//Code and Query here
while($row = mysqli_fetch_array($result)) {
$totalRows++;
if (!empty($row[name])) {
$tableOne .= "<tr>";
$tableOne .= "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
$tableOne .= "<td>" . $row['name'] . "</td>";
$tableOne .= "<td>" . $row['imgPURL'] . "</td>";
$tableOne .= "<td>" . $row['imgUrl'] . "</td>"; echo "</tr>";
}
else {
$emptyNameCounter++;
//store in $tableTwo if name is empty
}
}
//close tables, echo out variables