我正在尝试获取一个区域中的物品清单。
我希望将地区名称作为标题 然后,下表中该区域中的所有项目 即
HEADING-Region1
表格-Region1项
HEADING-Region2
表格-Region2项
HEADING-Region3
表格-Region3项
HEADING-Region4
表格-Region4项
我可以输出:
HEADING
HEADING
HEADING
如果使用“实际区域名称”之类的代码,则可以在表格中输出区域详细信息
我想使用LIKE $ region进行输出,因此不需要为每个“实际区域名称”编写新的语句。
2个SELECT查询:
$region = mysqli_query($conn,"SELECT DISTINCT region FROM country") or die($conn->error);
$result = mysqli_query($conn,"SELECT * FROM country WHERE region LIKE '$region'") or die($conn->error);
输出1
这会将每个区域输出为
区域名称
$i = 0;
while($row = $region->fetch_assoc())
{
if ($i == 0) {
foreach ($row as $value) {
echo "<p>" . $value . "</p>";
}
}
}
输出2
这将输出所有区域数据并将其放在表格中。
echo "This is table Build";
echo "<table border='1'>";
$i = 0;
while($row = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
OUTPUT 1和OUTPUT 2都分别工作。 我不能让他们一起工作
答案 0 :(得分:1)
您可以从查询中获取所有region
的名称,然后将其传递到next query
并打印与该查询相关的所有record
,即:
<?php
$region = mysqli_query($conn,"SELECT DISTINCT region FROM country") or die($conn->error);
if($region->num_rows > 0)
{ {
while($row = $region->fetch_assoc())
{
//printing region name
echo "<p>" . $row['region'] . "</p>";
//only those row will be retrieve which belong to current region name
$result = mysqli_query($conn,"SELECT * FROM country WHERE region LIKE '$row['region']'") or die($conn->error);
//printing table
echo "This is table Build";
echo "<table border='1'>";
$i = 0;
while($row1 = $result->fetch_assoc())
{
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row1 as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row1 as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
}
mysqli_close($conn);
?>