display multiple table as query result using ORDER BY in PHP MYSQL

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>";
}
This question and answers originated from www.stackoverflow.com
Question by (6/23/2011 8:54:24 AM)

Answer

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


Find More Answers
Related Topics  php  mysql  query
Related Questions
  • PHP MYSQL Multiple table query

    I have two tables 'p_tuts' and 'h_tuts'. I'm using a method to display all the returned rows. Although I'm not sure how to set it up to run multiple queries, I can only get it to return one query at…
  • How to display my detail in correct order using MySQL query?

    I have no idea how to query mysql to display the pattern I want. The table store all booking details of customers. A customer may multiple booking from multiple events. And in 1st stage I only want …
  • MySQL Query - Result Table Name

    When I query a mysql database in PHP, I can not figure out how to specify the table of the field in the result set. For example, I would like to do the following: $row = mysql_fetch_assoc($res…
  • printing [mysql_query] result in a table

    I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this. <?php mysql_connect("localhost","root","") or die("Could not Connect."); mysql_select_d…
  • Display PHP Query Array in the table

    Hi all I would like to display my Query from Mysql on the table. However, my data is like this: Array([0]=>data1, [1]=>data2, [2]=>data3,[3]=>data4,[4]=>pic1,[5]=>pic2,[6]=>…
  • Query time result in MySQL w/ PHP

    Is there a way that I can get the time of a MySQL query (specifically with PHP)? The actual time it took to complete the query, that is. Something such as: Results 1 - 10 for brown. (0.11 seconds…
  • PHP MySql help with complex Multiple table Query

    I'm storing a bunch of data for communities in MySql My initial communities table has these fields: id (primary key) name logo map description I'm then storing different data fo…
  • Put result from mysql_query in php array

    I want to get some results from the MySQL database and put them in a array like this: array("value2", "value2", "value3"); I have tried this: $models = array(); $getmodels = mysql_query("s…
  • Display multiple records in json using php mysql query

    I have written a script to query a mysql database and encode the data in json format. I have added $encoded = json_encode($encodable[0]); which removes the other [ ] brackets but then only displa…
  • How can I order a query result same as the id specified in the WHERE condition?

    I have a query like this SELECT * FROM test JOIN test2 ON test.id=test2.id WHERE test.id IN (562,553,572) GROUP BY test.id Its results are ordered like this: 553, 562, 572... But I need…