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

时间:2011-06-23 08:54:24

标签: php mysql

我需要一些关于这些的帮助,我希望用多个表显示查询结果,因为资产具有不同的属性,它将是ORDER BY类别。比方说,Category = Laptop会列出所有的笔记本电脑细节,电视将有自己的表格及其功能和等等。所有这些都将在同一页面上,但按表格细分。我怎样才能做到这一点?这是我认为问题所在的部分。任何帮助都非常感谢!

$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>";
}

2 个答案:

答案 0 :(得分:1)

我在代码中看到的一个大问题是它的重复,它完全打破了DRY principle。此外,您的类别中的类别是硬编码的,并存储在数据库中。我修改了脚本,因此只要在结果集中找到新类别,它就应该创建一个通用的表头。

请尝试此操作(而不是所有代码)并查看它是否适合您:

$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

此代码假定您的结果与ORDER BY category

一起出现

答案 1 :(得分:0)

这样做怎么样? :

$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>";
}