I need some help with displaying the results correctly from the MySql query in rows in a HTML table. Can anyone offer some code, some corrections, and why I need to edit the code, and why the corrections where offered so that I can better learn and understand how to use MySql queries and display results so that I can do this next time without any help.
Here’s what the end result needs to be:
Here’s my code so far:
<?php
include 'connection.php'; // includes the connection.php file to connect to the database
// query to database and prepares and executes
$query = "SELECT id, first_name, last_name FROM customers ORDER BY last_name asc";
$result = $db->prepare($query);
$result->execute();
$result->store_result();
$result->bind_result($query);
// count the number of customers
$total_customers = $result->num_rows;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Granger Customers</title>
<link href="assets/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--start container to center design in browser-->
<div class="container">
<div class="row" style="margin-bottom:30px">
<div class="col-xs-12">
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="customers.php">Customers</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="">Add New</a>
</li>
</ul>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered table-striped table-hover">
<p><strong>There are a total of <?PHP echo $total_customers; ?> customers.</strong></p> <!-- output total number of customers -->
<thead>
<tr class="success">
<th class="text-center">#</th>
<th>Customer ID</th>
<th>Last Name</th>
<th>First name</th>
<th class="text-center">Details</th>
</tr>
</thead>
<tbody>
<!--start of row that you need to include for each item returned from the query-->
<?php
while($result = $result->fetch_assoc($query)) {
'<tr>';
echo '<td>'.$row["id"].'</td>';
echo '<td>'.$row["first_name"].'</td>';
echo '<td>'.$row["last_name"].'</td>';
'</tr>';
}
?>
<tr>
<td class="text-center active">1</td>
<td>20597</td>
<td>Aadil</td>
<td>Kareem</td>
<td class="text-center"><a class="btn btn-xs btn-primary" href="details.php?id=20597">Details</a></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!--end container to center design in browser-->
</body>
</html>