【问题标题】:Multidimensional PHP array from a MySQLi table to HTML table从 MySQLi 表到 HTML 表的多维 PHP 数组
【发布时间】:2015-10-16 23:16:50
【问题描述】:

我的网站有一部分可以给会员颁奖。现在我正在为每个奖项创建单独的描述页面。在该页面上,我想包括哪些成员已经获得了该奖项,并将这些数据显示在 HTML 表格中。

我已经将数据传递到一个多维数组中,如下所示。

<?php 

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $currentAward = $awardid; // Current Page Award. Local value to the page.

    $result = $conn->query("SELECT * FROM vms_awardsgranted WHERE awardid='$currentAward'");

    $awardsList = array();
    while($pilotsList = $result->fetch_assoc()){
        $awardsList[ $pilotsList["id"] ] = $pilotsList;
    }

    echo nl2br("DEBUG: $currentAward \n");
    print_r(array_values($awardsList));

    $conn->close();
?>

示例结果

DEBUG: 8 
Array ( [0] => Array ( [id] => 28 [awardid] => 8 [pilotid] => 4 [dateissued] => 2015-10-14 20:12:21 ) [1] => Array ( [id] => 32 [awardid] => 8 [pilotid] => 1 [dateissued] => 2015-10-14 20:14:14 ) )

从这里我试图解析这些信息并将其添加到下面的 HTML 表中,但老实说,我找不到使用多维数组执行此操作的正确方法。有人可以在这里给我一些见解吗?我的 HTML 表格如下所示。

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
    <thead>
        <tr>
        <th>Pilot ID</th>
        <th>Pilot Name</th>
        <th>Date Awarded</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td align="center">

            </td>
            <td align="center">

            </td>
            <td align="center">

            </td>
        </tr>
    </tbody>
</table>

【问题讨论】:

    标签: php html arrays multidimensional-array mysqli


    【解决方案1】:

    基本上,要遍历结果,您的代码可能如下所示:

      <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
          <thead>
              <tr>
              <th>Pilot ID</th>
              <th>Pilot Name</th>
              <th>Date Awarded</th>
              </tr>
          </thead>
          <tbody>
      <?php foreach($awardsList as $member):?>    
    
              <tr>
                  <td align="center">
                    <?=$pilot['pilotid']?>
                  </td>
                  <td align="center">
                  </td>
                  <td align="center">
                    <?=$pilot['dateissued']?>
                  </td>
              </tr>
      <?php endforeach;?>    
    
          </tbody>
      </table>
    

    不过,有几点要说。

    • 您没有从查询中返回飞行员姓名。您应该加入成员/飞行员表以获取他们的名字。
    • 您的命名令人困惑。 $pilotList 建议使用飞行员列表,但该变量仅包含奖励和一名飞行员之间的参考/连接。 Dito for awardsList 实际上包含获奖者名单。

    【讨论】:

    • 是的,我的变量名现在真的很混乱。我迷失在我的页面代码中并开始混淆变量。感谢您指出这一点。我现在会把它清理干净。 编辑感谢您注意到我的表中缺少飞行员姓名。
    • 这很完美。但是,除了复制您的代码之外,您能否准确地告诉我这里发生了什么,以便我可以学习?我看到您将每一行数组数据作为 $pilot 传递,但那里实际发生了什么?您是否将每一行定义为 $pilot,然后您是否通过 =$pilot['pilotid']?> 调用查询每一行并在该行中找到 Pilotid?
    • 呃是的。很简单,代码包含一个遍历$awardsList 的foreach 循环。在每次迭代中,下一项在变量$pilot 中可用(因为awardsList 实际上包含人,正如我之前提到的)。因此,该代码只会为该数组中的每个项目生成一个表格行。然后&lt;?=$pilot['pilotid']?&gt;&lt;?= 只是 &lt;?php echo 的简写,因此该代码只是回显来自 $pilot 的值。除此之外,它只是您的代码未修改。 @JulioSoares 的回答显示了相同的解决方案,只是对循环和回声使用了稍微不同的符号。
    • 非常感谢您抽出额外的时间来解释。与其滥用这项伟大的服务,只是让人们为我写东西,我实际上有这种强烈的学习热情。谢谢你。
    【解决方案2】:

    类似

    <tbody>
        <?php foreach($awardsList as $record){ ?>
        <tr>
            <td align="center">
                  <?php echo $record['id']; ?>
            </td>
            <td align="center">
                  <?php echo $record['awardid']; ?>
            </td>
            <td align="center">
                  <?php echo $record['pilotid']; ?>
            </td>
        </tr>
        <?php } ?>
    </tbody>
    

    【讨论】:

      猜你喜欢
      • 2020-05-27
      • 2023-03-30
      • 2011-01-28
      • 2014-05-17
      • 2019-12-14
      • 2016-01-13
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      相关资源
      最近更新 更多