Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在递归ArrayIterator中打印表数据索引_Php_Html_Mysql_Pdo - Fatal编程技术网

Php 如何在递归ArrayIterator中打印表数据索引

Php 如何在递归ArrayIterator中打印表数据索引,php,html,mysql,pdo,Php,Html,Mysql,Pdo,我想打印一个带有索引的表,目前我还不熟悉PDO和递归表打印(这就是它的名称吗?)。我找到了一个示例代码,但它没有将索引和数据一起打印。所以我修改了代码以包含索引。然而,我正在使用全局变量,据我所知,使用全局变量是一种不好的做法。是这样吗?还有别的办法吗 <?php echo "<table id='DataTable1' class='display' cellspacing='0' width='100%'>"; echo "<tr>&l

我想打印一个带有索引的表,目前我还不熟悉PDO和递归表打印(这就是它的名称吗?)。我找到了一个示例代码,但它没有将索引和数据一起打印。所以我修改了代码以包含索引。然而,我正在使用全局变量,据我所知,使用全局变量是一种不好的做法。是这样吗?还有别的办法吗

<?php

      echo "<table id='DataTable1' class='display' cellspacing='0' width='100%'>";
      echo "<tr><th>No</th><th>Name</th><th>Phone</th><th>Email</th><th>Guest</th><th>Attendance</th></tr>";

      $i = 1;

      class TableRows extends RecursiveIteratorIterator { 

          function __construct($it) {
              parent::__construct($it, self::LEAVES_ONLY); 
          }

          function current() {
              return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
          }

          function beginChildren() { 
              $index = $GLOBALS['i'];
              echo "<tr><td>$index</td>"; 
          } 

          function endChildren() { 
              echo "</tr>" . "\n";
              $GLOBALS['i']++;
          } 
      } 

      try {
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $stmt = $conn->prepare("SELECT guestName, guestPhone, guestEmail, guestGuest, attendance FROM rsvp_list"); 
          $stmt->execute();

          // set the resulting array to associative
          $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
          foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
            echo $v;
          }
      }
      catch(PDOException $e) {
          echo "Error: " . $e->getMessage();
      }
      $conn = null;
      echo "</table>";
      ?>

最佳实践显然是根本不使用递归迭代器来输出HTML表。一些奇怪的家伙把它放在了网页上,出于某种原因,许多人开始让他们的生活变得复杂十倍

虽然要输出HTML表,根本不需要迭代器,更不用说递归了

<?php

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT guestName, guestPhone, guestEmail, guestGuest, attendance FROM rsvp_list";
$data = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC); 

?>
<table id='DataTable1' class='display' cellspacing='0' width='100%'>
    <tr><th>No</th><th>Name</th><th>Phone</th><th>Email</th><th>Guest</th><th>Attendance</th></tr>
    <? foreach ($data as $index => $row): ?>
        <tr>
            <td><?=$index+1?></td>
            <? foreach ($row as $value): ?>
                <td style='width:150px;border:1px solid black;'><?=$value?></td>
            <? endforeach ?>
        </tr>
    <? endforeach ?>
</table>

非甲酰化

这段代码更干净、更短、更理智十倍

最佳实践是基于意见的,不属于这里。请阅读如何回答有关Stackoverflow的问题。@xOrf否则,我是否应该将主题重命名为“如何在RecursiveArrayInterator PHP PDO中打印表数据索引”呢?此时您已经标记了“PHP”和“PDO”,它们不属于标题中。
做什么?打印$index+1值正是我要查找的。谢谢