【问题标题】:how to organise a table with php and html?如何用 php 和 html 组织表格?
【发布时间】:2021-12-27 23:36:35
【问题描述】:

我是学生,所以我是代码初学者。

我在 MySQL 的数据库中进行了搜索,结果是一个包含 3 个其他表的表:

array (size=3)
  0 => 
    array (size=4)
      'type_de_sport' => string 'boxe' (length=4)
      'Adresse' => string '33 rue de Paradis' (length=17)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13006' (length=5)
  1 => 
    array (size=4)
      'type_de_sport' => string 'boxe' (length=4)
      'Adresse' => string '44 boulevard Sakakini' (length=21)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13005' (length=5)
  2 => 
    array (size=4)
      'type_de_sport' => string 'BOXE' (length=4)
      'Adresse' => string 'place a moi' (length=11)
      'ville' => string 'Marseille' (length=9)
      'code_postal' => string '13000' (length=5)

我想整理一下这个结果,我试过了,但是没有用:

<table>
<thead>
    <tr>
      <th scope="col">Type de sport</th>
      <th scope="col">Adresse </th>
      <th scope="col">Ville</th>
      <th scope="col">code postal</th>
    </tr>
  </thead>
  <tbody>
  <? for ($i=0; $i >$num ; $i++){ ?>
    <tr>
      <th scope="row"><?php $row=$i + 1 ; ?></th>
      <?php foreach ($resultats[$i] as $contenu) { ?>
      <td><?= $contenu ; ?></td>
      <?php } ?>
    </tr>
    <?php } ?>

  </tbody>
</table>

请问您还有其他想法吗?

最好的问候

【问题讨论】:

  • “没用”到底是怎么回事?一个错误?出乎意料的结果?在这个相当简单的情况下,我可以猜到,但总的来说,在询问代码问题时,您应该始终对您的问题给出清晰准确的解释。它使人们更容易帮助您。否则他们只需要从问问题开始。
  • 你的输出应该是什么样的,你得到了什么?具体说明软件版本。审查结束。

标签: php html arrays


【解决方案1】:

你有一个数组数组。 $results 应该包含 while 数组。

<tbody>

<?php 
   // $results is the main array
   foreach($results as $result){ 
   // so when we are looping here $result will contain one single array
   // this $result array is an associative array, so you have to access the values using the key
?>

    <tr>
       <td><?php echo($result['type_de_sport']); ?></td>
       <td><?php echo($result['Adresse']); ?></td>
       <td><?php echo($result['ville']); ?></td>
       <td><?php echo($result['code_postal']); ?></td>
    </tr>

<? } //end of foreach ?>
</tbody>

【讨论】:

    【解决方案2】:

    代码中有一些小逻辑问题:

    1. $num 似乎没有定义。

    2. &gt; 应该是&lt;,因为因为你在增加$i,它要么已经大于$num(如果它存在的话)并且你会得到一个无限循环(最终是一个崩溃),否则它不会开始并且循环永远不会开始。

    $i 小于结果数组的大小时,您应该循环。

    1. 您从不使用$row,不清楚为什么要将计算放在&lt;th&gt; 中,而不仅仅是在代码块中。所以我猜你实际上是想在屏幕上将其输出为行号。

    这将解决这些小问题,代码应该可以正常工作:

    for ($i=0; $i < count($resultats); $i++){ ?>
        <tr>
          <th scope="row"><?php echo $i + 1 ; ?></th>
          <?php foreach ($resultats[$i] as $contenu) { ?>
          <td><?= $contenu ; ?></td>
          <?php } ?>
        </tr>
    <?php } ?>
    

    现场演示:http://sandbox.onlinephpfunctions.com/code/6d214f826ec35e959d8420fcf3d95726557f3637

    【讨论】:

      猜你喜欢
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多