如何在RecursiveArrayIterator中打印表数据索引 [英] How to print table data index in RecursiveArrayIterator

查看:36
本文介绍了如何在RecursiveArrayIterator中打印表数据索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印带有索引的表,而我目前对PDO和递归表打印不熟悉(这就是所谓的吗?).我找到了一个示例代码来执行此操作,但是它没有将索引与数据一起打印.因此,我修改了代码以包含索引.但是,我使用的是全局变量,据我了解,使用全局变量是一种不好的做法.是这样吗?还有其他方法吗?

I want to print a table with index and I'm currently new to PDO and recursive table printing (is that what it's called?). I found a sample code to do that but it didn't print the index together with the data. So I modified the code to include index. However, I'm using Global Variable and from what I understand, it is a bad practice to use global variable. Is that so? Any other way to do it?

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

推荐答案

最佳实践显然是<​​strong>完全不使用任何递归迭代器来输出HTML表.某个陌生的家伙将其放在网页上,由于某种原因,许多人开始使生活变得更加复杂十倍.

the best practice is apparently NOT to use no recursive iterators to output HTML tables at all. Some strange guy put it up on the web page and for some reason many people started to make their lives ten times more complicated.

在输出HTML表时,根本不需要迭代器,更不用说递归了.

While to output an HTML table one don't need no iterators at all, let alone recursion.

<?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>

此代码更干净,更短,更干净十倍.

this code is ten times cleaner, shorter, and saner.

这篇关于如何在RecursiveArrayIterator中打印表数据索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆