我有一个表单,用户可以在其中输入 ShopID、从汽车列表中进行多项选择以及该组的价格。汽车列表插入数据库,如“奥迪、萨博、沃尔沃”。
我现在正在尝试显示一个表格,其中显示所有 ShopID、选定的汽车和团体价格。理想情况下,表格应该是这样的:
Shop ID Cars Price
audi
1 saab 100
volvo
--------------------------- (this is a table border)
saab
2 bmw 200
ford
honda
此表显示与所有汽车相关联的商店 ID 和组价格(而不是每辆汽车的商店 ID 和价格)。
但是现在,我让所有的汽车只显示在一条线上,例如:
Shop ID Cars Price
1 audi, saab, volvo 100
有谁知道我如何循环遍历“Cars”中的数组并在其自己的行中显示每个...同时保持“商店 ID”和“价格”仅显示一次并将所有这些信息集中在一起? 目前,我创建这个表的代码如下:
<table class="table table-striped">
<tr>
<th>ShopID</th>
<th>Cars</th>
<th>Price</th>
</tr>
<?php
while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['shopID'].'</td>';
$output .= '<td>'.$row['cars'].'</td>';
$output .= '<td>'.$row['price'].'</td>';
$output .= '</tr>';
echo $output;
}
?>
</table>
谢谢!
编辑 我确实尝试过使用 explode(),但我遇到了同样的问题,我不知道如何让它们进入自己的行!
最佳答案
使用爆炸()
while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['shopID'].'</td>';
$car = explode(",",$row['cars']);
$output .= '<td>';
foreach($car as $c)
{
$output .= $c."</br>";
}
$output .='</td>';
$output .= '<td>'.$row['price'].'</td>';
$output .= '</tr>';
echo $output;
}
使用 str_replace()
while($row = $result->fetch_array()) {
$output = '<tr>';
$output .= '<td>'.$row['shopID'].'</td>';
$cars = str_replace(",", "<br/>", $row['cars']);
$output .= '<td>'.$cars.'</td>';
$output .= '<td>'.$row['price'].'</td>';
$output .= '</tr>';
echo $output;
}
关于fetch_array 中的 PHP MySQL 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967963/