I need a help on these, where I want to display the query result with multiple table as the assets have different attributes, it will be ORDER BY category. Let's say, Category = Laptop will list all the laptop details, TV will have its own table with its features & so on. All of this will be on the same page but breakdown by tables. How can I achieve this? Here's the part where I suppose the problem lies. Any help is highly appreciated!
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($category == "2 - TV")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "3 - DESK")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "4 - TELEPHONE")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}
The big problem I see in your code is its repetition, it completely breaks the DRY principle. Also, you have your categories hard coded in your code, and stored in your DB. I modified the script, so that it should create now a generic table header whenever a new category is found in the resultset.
Please try this (instead of all your code) and see if it works for you:
$categ = '';
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category != $categ)
{
$categ = $category;
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
} //while
} //if
This code assumes that your results are comming with ORDER BY category
What about doing it like this? :
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}
Related
I'm trying to create one table that will loop through all the records from my database but it seems to be create individual tables for each record instead.
$query = 'select * from images';
$result = mysqli_query($connection,$query);
$row_count = mysqli_num_rows($result);
for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';
foreach($row as $next) {
{
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
echo '</section>';
}
Ideally, you would loop around the tr
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
foreach($row as $next) {
{
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
// all the other columns
echo "</tr>";
}
echo "</table>";
I have the following PHP/MySql:
$sql = "SELECT * from tblDigest";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I would like to group the results by 'diagram', so that I can display them as an accordion. Is there any easy way to do this?
Failing that, how can I get the value for the first row of the table (so I can have a $diagNo variable and compare $row["diagram"] against that to find out when it has changed.
Sort your results by diagram and watch when its value changes:
$sql = "SELECT * from tblDigest ORDER BY diagram";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr>
<th>Diagram</th>
<th>Headcode</th>
<th>Date</th>
<th>Dep Time</th>
<th>Origin</th>
<th>Destination</th>
<th>Arr Time</th>
<th>Booked Traction</th>
<th>Actual Traction</th>
<th>Type</th>
</tr>";
// output data of each row
$oldDiagram = '';
while($row = $result->fetch_assoc()) {
if($oldDiagram == '')
{
// this is the first diagram - create the first accordeon
$oldDiagram = $row['diagram'];
}
elseif($oldDiagram != $row['diagram'])
{
// a new group starts right now - create new accordeon
$oldDiagram = $row['diagram'];
}
echo "<tr>";
echo "<td>" . $row["diagram"] . "</td>";
echo "<td>" . $row["headcode"] . "</td>";
echo "<td>" . $row["depDate"] . "</td>";
echo "<td>" . $row["depTime"] . "</td>";
echo "<td>" . $row["origin"] . "</td>";
echo "<td>" . $row["destination"] . "</td>";
echo "<td>" . $row["arrTime"] . "</td>";
echo "<td>" . $row["bookedTraction"] . "</td>";
echo "<td>" . $row["actualTraction"] . "</td>";
echo "<td>" . $row["type"] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Here is some short Code of table.I am using echo in table.
The data is not printing as first three fields.
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td>";
}
echo "</table>";
Table Output Image
You have missed tr in every dynamic data. change your code as below:
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Date</th>
<th>Reference</th>
</tr>" ;
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->id . "</td>";
echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->ref . "</td></tr>";
}
echo " <tr>
<th>First Name</th>
<th>Father Name</th>
<th>Phone</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->name . "</td>";
echo "<td>" . $row->fname . "</td>";
echo "<td>" . $row->cell . "</td></tr>";
}
echo "<tr>
<th>District</th>
<th>Address</th>
<th>Gender</th>
</tr>";
while ($row = $result->fetch_object())
{
echo "<tr><td>" . $row->district . "</td>";
echo "<td>" . $row->address . "</td>";
echo "<td>" . $row->gender . "</td></tr>";
}
echo "</table>";
I was hoping to get anyone's opinion on why this certain variable won't print out. Variable $sum from the $qsl_ query is not printing along with the other table row data.
<?php
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
}
echo "</table>";
?>
This is my current output where the points column won't produce:
I apologize if my code appears inefficient, I am a struggling student fanatic of PHP. I appreciate direct answers; however, learning is my key here. Thanks in advance!
$sql = "SELECT users.*, sum(history.points) as points FROM users left join history on users.id=history.userid group by users.id";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
echo "<tr>";
echo "<td>" . $record['id'] . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $record['points'] . "</td>";
}
echo "</table>";
It seems that I might have had my code jungled, thank you #abhi and #msfoster for your healthy inputs.
<?php
$sql = "SELECT * FROM users";
$myData = mysql_query($sql);
echo "<table id=\"table\" class=\"table table-striped\">
<thead>
<tr>
<th>ID</th>
<th>Email</th>
<th>First Name</th>
<th>Last Name</th>
<th>Model</th>
<th>Year</th>
<th>Plate Number</th>
<th>City</th>
<th>Country</th>
<th>Points</th>
</tr></thead>";
while($record = mysql_fetch_array($myData)){
$id = $record['id'];
$sql_ = "SELECT SUM(`points`) AS value_sum FROM `history` WHERE `userid` = '$id'";
$result = mysql_query($sql_);
#echo mysql_error();
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $record['email'] . "</td>";
echo "<td>" . $record['firstName'] . "</td>";
echo "<td>" . $record['lastName'] . "</td>";
echo "<td>" . $record['model'] . "</td>";
echo "<td>" . $record['Year'] . "</td>";
echo "<td>" . $record['plateNumber'] . "</td>";
echo "<td>" . $record['city'] . "</td>";
echo "<td>" . $record['country'] . "</td>";
echo "<td>" . $sum . "</td>";
echo "</tr>";
}
echo "</table>";
?>
My output is finally:
query is correct but as you shown
var_dump($row) = "array (size=1) 'value_sum' => null "
so it have 'null' because of that its showing blank;
but for this you can use join also .
$row = mysql_fetch_assoc($result); returns an array of "rows". Since you want the first row you need to access it $sum = $row[0]['value_sum'];
Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";