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($开发者_JS百科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>";
}
精彩评论