fetch_array 中的 PHP MySQL 数组

标签 php mysql arrays

我有一个表单,用户可以在其中输入 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/

相关文章:

Php 到 JSON 传递到高排行榜

php - Bash 字符串删除特定字符

java - 尝试检索用户在 Controller 中输入的值并将其添加到 SpringBoot 应用程序中的 mysql 数据库中

java - 具有该位置 [5] 的参数不存在;

php - 在 foreach 中捕获数组的下一个值

c++ - 如何删除这个二维指针数组?制作析构函数

php - 如何改进 php 和 MySQL 中的查询以实现快速显示

php - htaccess 中的自定义 503 页面为 "/path/to/index.php?maintenance"?

mysql - 删除mysql上的重复记录?

javascript - 在javascript中找到不同的匹配项和resort数组