简体   繁体   English

在PHP MYSQL中使用ORDER BY将多个表显示为查询结果

[英]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. 我需要这些方面的帮助,由于资产具有不同的属性,因此我想在其中显示带有多个表的查询结果,它将是ORDER BY类别。 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 . 我在您的代码中看到的最大问题是它的重复性,它完全破坏了DRY原理 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 此代码假定您的结果与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>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM