drsfgwuw61488
2018-06-01 11:41
浏览 65
已采纳

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

I'm struggeling with this one. How can list all devices, their model and supplier from my database? The data is stored comma-separated in the fields device, model and supplier.

<?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>";
  }
?>

图片转代码服务由CSDN问答提供 功能建议

我正在努力解决这个问题。 如何从我的数据库中列出所有设备,他们的型号和供应商? 数据以逗号分隔的形式存储在设备,型号和供应商字段中。

 &lt;?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?我该怎么做?
 array($ model_explode [0]),
  array($ supplier_explode [0])
); 
 
 echo“&lt; table border ='1'&gt; 
&lt; tr&gt; 
&lt; th&gt;Møterom&lt; / th&gt; 
&lt; th&gt;  ; Romnummer&LT; /第&GT; 
&LT的第i; Lokasjon&LT; /第&GT; 
&LT的第i; Antall sitteplasser&LT; /第&GT; 
&LT的第i; Tilgjengelig utstyr&LT; /第&GT; 
&LT的第i; Bilde AV rommet&LT  ; / th&gt; 
&lt; th&gt; Endre&lt; / th&gt; 
&lt; th&gt; Slett&lt; / th&gt; 
&lt; / tr&gt; 
&lt; tr&gt; 
&lt; td&gt;“。$ row [  '名称']“&LT。  / td&gt; 
&lt; td&gt;“。$ row ['roomnbr']。”&lt; / td&gt; 
&lt; td&gt;“。$ row ['location']。”&lt; / td&gt; 
&lt;  ; td&gt;“。$ row ['seat']。”&lt; / td&gt; 
&lt; td&gt;“。$ device [0] [0]。”&lt; br&gt;“。$ device [1] [0  ] “&LT峰; br&gt;” 中。$装置[2] [0]“&LT; BR&GT;&LT; / TD&GT;  //也计数?
&lt; td&gt;&lt; img src ='.. / img / uploads /".$ row ['img']。“'width ='150px'&gt;&lt; / td&gt; 
  &lt; td&gt;&lt; a href ='?p = editconfroomv2&amp; id =“。$ row ['id']。”'&gt; Endre&lt; / a&gt;&lt; / td&gt; 
&lt; td&gt;&lt; a  href ='。/ index.php?p = deleteconfroomdb&amp; id =“。$ row ['id']。”'class ='delete'&gt; Slett&lt; / a&gt;&lt; / td&gt; 
&lt; / tr&gt;  ; 
&lt; / table&gt;“; 
} 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanheye7423 2018-06-01 12:01
    已采纳

    It can be done this way (if I understand correct your table structure). I assume, that $device_explode, $model_explode and $supplier_explode have equal number of elements:

    <?php
      // Table Header
      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>";
      while ($row = mysqli_fetch_array($result)) {
        $device_explode = explode(',', $row['device']);
        $model_explode = explode(',', $row['model']);
        $supplier_explode = explode(',', $row['supplier']);
    
        // Table rows   
        $td = '';
        $td .= 
            "<tr>
                <td>".$row['name']."</td>
                <td>".$row['roomnbr']."</td>
                <td>".$row['location']."</td>
                <td>".$row['seats']."</td>
                <td>";
    
        for ($i = 0; $i < count($device_explode); $i++) {
            $td .=$device_explode[$i]." ".$model_explode[$i]." ".$supplier_explode[$i]."<br>";
        } 
    
        $td .= 
                "</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>";
        echo $td;   
      }
      echo "</table>";
    ?>
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题