【问题标题】:Display Php Array result in an Html table在 Html 表中显示 Php 数组结果
【发布时间】:2012-12-07 08:04:47
【问题描述】:

我试图将 php 中的数组显示到 HTML 表中,但出现了问题。我在堆栈溢出中找到了几个示例,但它们不适用于我的情况。

控制器:

<?php include('inc/db_connect.php');?>

<?php
try
{
  $sql = "SELECT id GroupName, VideoTITLE, ByArtist FROM videoclip";
  $result = $pdo->query($sql);
}
catch(PDOException $e)
{
  $error = 'unable to fetch data: '.$e->getMessage();
  include'error.html.php';
  exit();
}
$URLS = array();
while ($row = $result->fetch())
{
  $URLS[] = array('id' => $row['id'], 'GroupName' => $row['GroupName'], 'VideoTITLE' => $row['VideoTITLE'], 'ByArtist'=> $row['ByArtist'] );
}

html:

<div id="table_admin" class="span7">
        <h3>Videoclip List</h3>

        <table class="table table-striped table-condensed">

                <thead>
                <tr>
                <th>Song name</th>
                <th>Group name </th>
                <th>Artist </th>
                </tr>
                </thead>
            <?php foreach ($URLS as $URL){
                echo'<tbody>';
                echo'<tr>'; 
                echo'<td>'. $row['VideoTITLE']."</td>";
                echo'<td>'. $row['GroupName'].'</td>';
                echo'<td>'. $row['ByArtist'].'</td>';
                echo'<tr>';
                echo'</tbody>';
              }
            ?>

        </table>

    </div>

【问题讨论】:

    标签: php


    【解决方案1】:

    从数据库中获取结果后请关注

    <?php
      echo '<table>';
      echo '<thead>';
      echo '<tr>';
      echo '<th>Song name</th>';
      echo '<th>Group name </th>';
      echo '<th>Artist </th>';
      echo '</tr>';
      echo '</thead>'
      echo '<tbody>'; 
      foreach($URLS as $URL) {
        echo'<tr>'; 
        echo'<td>'. $URL['VideoTITLE']."</td>";
        echo'<td>'. $URL['GroupName'].'</td>';
        echo'<td>'. $URL['ByArtist'].'</td>';
        echo'<tr>';  
      }
      echo '</tbody>'; 
      echo '</table>'; 
    ?>
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      你已经接近了:

      </thead>
      <tbody>
      <?php 
          foreach ($URLS as $URL){
              echo'<tr>'; 
              echo'<td>'. $URL['VideoTITLE']."</td>";
              echo'<td>'. $URL['GroupName'].'</td>';
              echo'<td>'. $URL['ByArtist'].'</td>';
              echo'<tr>';
          }
      ?>
      </tbody>
      

      由于您正在获取$URLS 数组的值并调用每个$URL,因此您需要为每一行的值引用$URL。不是您最初用于从数据库结果中填充数组的 $row 变量。

      仅供参考,您可能需要查看 htmlentities() 以逃避您的数据以帮助防止 XSS 攻击。

      【讨论】:

      • 谢谢,这是一个个人项目,我打算在完成构建后对其进行消毒,以确保我学习正确。谢谢,愚蠢的错误,我从未见过,请纠正它
      【解决方案3】:

      假设您的 fetchRow 数据很好,我在您的 html 中只看到一个主要错误:在您的 foreach 循环中将 $row 更改为 $URL。此外,您可以将 PHP 与 HTML 混合使用以使其更漂亮:

      <?php foreach ($URLS as $URL) { ?>
          <tr>
              <td><?php echo $URL['VideoTITLE']; ?></td>
              <td><?php echo $URL['GroupName']; ?></td>
              <td><?php echo $URL['ByArtist']; ?></td>
          <tr>
      <?php } ?>
      

      【讨论】:

        【解决方案4】:

        是的,正如 john 所说,您将 $URLs 作为 $URL,但随后您引用了一个在 for each 中调用 $row 的数组。

        另一件事是您可能希望将 tbody 标签放在 for each 循环之外

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-14
          • 1970-01-01
          • 1970-01-01
          • 2011-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-08-15
          相关资源
          最近更新 更多