在 PHP MYSQL 中使用 ORDER BY 将多个表显示为查询结果
我需要有关这些的帮助,我想在其中显示多个表的查询结果,因为资产具有不同的属性,它将是 ORDER BY 类别。比方说,类别=笔记本电脑将列出所有笔记本电脑的详细信息,电视将有自己的表格及其功能和功能。很快。所有这些都将在同一页面上,但按表格进行细分。我怎样才能实现这个目标?这是我认为问题所在的部分。非常感谢任何帮助!
$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>";
}
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>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在你的代码中看到的最大问题是它的重复,它完全打破了DRY原则。此外,您还可以将类别硬编码在代码中,并存储在数据库中。我修改了脚本,以便只要在结果集中找到新类别,它现在就应该创建一个通用表头。
请尝试此操作(而不是您的所有代码),看看它是否适合您:
此代码假设您的结果与
ORDER BY Category
一起发送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:
This code assumes that your results are comming with
ORDER BY category
这样做怎么样? :
What about doing it like this? :