【发布时间】:2022-06-13 22:55:22
【问题描述】:
我正在尝试显示两个表中的数据。我能够组合数据,显示它们,但无法打破它们以特定方式显示。下面是它的样子。
这是我的输出,后面是我的代码。我想将类别显示为标题以及与之配套的产品,而不是担心布局,只担心逻辑。
<?php
$getCategories = "select id AS categoryList, category AS catGor from categories ORDER BY catGor";
$resCategories = mysqli_query($con,$getCategories);
$information = "select products.product AS product, products.price AS price, products.category_id AS category_id, categories.id AS idCat, categories.category AS category from products,categories ORDER BY products.product";
$getInformation = mysqli_query($con,$information);
if($resCategories->num_rows != 0)
{
while($row = $resCategories->fetch_assoc())
{
$catList = $row['categoryList'];
$catGor = $row['catGor'];
for($i = 0; $i < $catList; $i+=3)
{
echo "<h2>$catGor</h2>";
echo "Product - Price"."<br/>";
if(($getInformation->num_rows != 0))
{
while($rows = $getInformation->fetch_assoc())
{
$product = $rows['product'];
$price = $rows['price'];
$categoryid = $rows['category_id'];
$idCat = $rows['idCat'];
$category = $rows['category'];
if((($idCat) == ($categoryid)) && (($categoryid) == ($catList)))
{
echo "$product ------ R$price"."</br>";
}
}
}
}
}
}
?>
【问题讨论】:
-
把你的输出放到
<table>作为它的表格数据