循环遍历多个数组并动态列出结果

时间:2018-06-01 11:41:48

标签: php arrays loops

我和这个人在一起。如何从我的数据库中列出所有设备,他们的型号和供应商?数据以逗号分隔的形式存储在设备,模型和供应商字段中。

<?php
  while ($row = mysqli_fetch_array($result)) {
    $device_explode = explode(',', $row['device']);
    $model_explode = explode(',', $row['model']);
    $supplier_explode = explode(',', $row['supplier']);

    $device = array(array($device_explode[0]), //count function? How would I do that?
                    array($model_explode[0]),
                    array($supplier_explode[0])
                  );

    echo "<table border='1'>
        <tr>
            <th>Møterom</th>
            <th>Romnummer</th>
        <th>Lokasjon</th>
        <th>Antall sitteplasser</th>
        <th>Tilgjengelig utstyr</th>
        <th>Bilde av rommet</th>
        <th>Endre</th>
        <th>Slett</th>
        </tr>
      <tr>
        <td>".$row['name']."</td>
        <td>".$row['roomnbr']."</td>
        <td>".$row['location']."</td>
        <td>".$row['seats']."</td>
        <td>".$device[0][0]."<br>".$device[1][0]."<br>".$device[2][0]."<br></td> //also count?
        <td><img src='../img/uploads/".$row['img']."' width='150px' ></td>
        <td><a href='?p=editconfroomv2&id=".$row['id']."'>Endre</a></td>
        <td><a href='./index.php?p=deleteconfroomdb&id=".$row['id']."' class='delete'>Slett</a></td>
      </tr>
      </table>";
  }
?>

2 个答案:

答案 0 :(得分:2)

可以这样做(如果我理解你的表格结构正确)。我假设webview.getSettings().setJavaScriptEnabled(true);$device_explode$model_explode具有相同数量的元素:

$supplier_explode

答案 1 :(得分:2)

我建议循环使用标记中的数组,并使用html模板语法和替代控制结构生成输出。

我做过的一些事情[与问题没有直接关系]:

  • 切换了array()语法的简写[可以从5.4版开始]
  • 从使用单个回显切换到利用html模板语法[通过从php中展示非动态部分并回显动态部分]
  • 从使用echo语句切换到使用简写
  • 切换到其他控制结构

PHP documentation for strings

<?php

while ($row = mysqli_fetch_array($result)) {
    $device_explode = explode(',', $row['device']);
    $model_explode = explode(',', $row['model']);
    $supplier_explode = explode(',', $row['supplier']);

    //I am assuming you want to loop trough all exploded fields related to each row
    $devices = [
        $device_explode,
        $model_explode,
        $supplier_explode
    ];

?>


<?php while(($row = mysqli_fetch_array($result))) : ?>
    <table border='1'>
        <tr>
            <th>Møterom</th>
            <th>Romnummer</th>
            <th>Lokasjon</th>
            <th>Antall sitteplasser</th>
            <th>Tilgjengelig utstyr</th>
            <th>Bilde av rommet</th>
            <th>Endre</th>
            <th>Slett</th>
        </tr>
        <tr>
            <td><?= $row['name'] ?></td>
            <td><?= $row['roomnbr'] ?></td>
            <td><?= $row['location'] ?></td>
            <td><?= $row['seats'] ?></td>
            <td>
            <?php foreach ($devices as $deviceSpecs) :  //Here we loop trough the first dimension fo the array ?>
                <?php foreach ($deviceSpecs as $deviceSpec): //Here we loop trough the second dimension ?>
                <?= $deviceSpec ?><br>
                <?php endforeach; ?>
            <?php endforeach; ?>
            </td>
            <td><img src='../img/uploads/<?= $row['img'] ?>' width='150px' ></td>
            <td><a href='?p=editconfroomv2&id=<?= $row['id'] ?>>Endre</a></td>
            <td><a href='./index.php?p=deleteconfroomdb&id=<?= $row['id'] ?>' class='delete'>Slett</a></td>

        </tr>
    </table>
<?php endwhile; ?>