【问题标题】:Array into html table using PHP MySQL使用 PHP MySQL 将数组放入 html 表
【发布时间】:2020-01-01 02:09:44
【问题描述】:

我在一个数据库中有 2 个表,我正在加入一个 PDO 执行,我让它以一种形式的表输出,但我需要它以另一种格式。我需要某种形式的动态枢轴吗?

两张表是这样的:

我希望从中得到下表:

我用来整理数据的当前查询是:

$sql3 = $pdo->prepare('SELECT b.title, a.nameFROM poll_summary a INNER JOIN poll_answers b ON a.answers_id = b.id WHERE a.poll_id = ? ORDER BY title');
$sql3->execute([$_GET['id']]);
$testing = $sql3->fetchAll(\PDO::FETCH_GROUP|\PDO::FETCH_ASSOC);    

这是它给出的示例数组:

Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )

我可以使用以下方法从标题列中获取表头:

<table border=1>
  <thead>
    <tr>
        <?php
            foreach($testing as $key => $val):
            ?>
                <th><?php echo $key;?></th>
             <?php endforeach; ?>
    </tr>
    </thead>
    <tbody>

        <tr>        


    </tbody>
</table>

但我不知道如何将名称值放入正确的列中。 我尝试使用此代码,但无法让它按我想要的方式工作。

foreach($testing as $key => $val) {
    $arrlength = count($val);
    for($x = 0; $x < $arrlength; $x++) {
    //echo $key;

    echo $key;
    echo $val[$x]['name']; 

    }

}

输出:

Knight Dave Knight Simon Lieutenant Tom

【问题讨论】:

  • 您应该在execute 查询之前先bindParam。
  • @LeonKunštek 为什么?这是 PDO,如图所示的准备/执行代码是完全有效的。
  • 你会将Param绑定到什么?此页面将用于各种不同的投票/输入/输出,因此每次都会有不同的值。绑定会回答我提出的问题吗?
  • OP,为什么“西蒙”会有“骑士”的头衔?为什么“汤姆”会有“中尉”的头衔?

标签: php html sql


【解决方案1】:

在 $testing 中已经有了这个结构:

Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) )

制作桌子

<?php
    // Just to keep same variable
    $output = $testing;
?>
<table border=1>
  <thead>
    <tr>
<?php
    // We need to know wich $title has more names
    $max = 0;
    foreach($output as $title => $names) {
       if(count($names) > $max) {
          $max = count($names);
       }
?>
                <th><?php echo $title;?></th>
<?php } // end foreach ?>
    </tr>
    </thead>
    <tbody>
<?php
    // Loop for creating table rows
    for($i = 0; $i < $max; $i++) {
?>
    <tr>
<?php
        // Loop for creating table cells
        foreach($output as $title => $names) {
?>
        <td><?php
             // Echo only if name exists
             if(isset($names[$i]['name'])) {
                echo $names[$i]['name'];
             }
        ?></td>
<?php
        } // end foreach
?>
    </tr>
<?php
    } // end for
?>
    </tbody>
</table>

【讨论】:

  • 打印 $output 没有数据 = Array ( [] => Array ( [0] => [1] => [2] => [3] => ) )
  • 在你的问题中你已经发布了 Array ( [Knight ] => Array ( [0] => Array ( [name] => Dave ) [1] => Array ( [name ] => Simon ) ) [Lieutenant ] => Array ( [0] => Array ( [name] => Tom ) ) ) 和我建议的 $output 一样,然后直接去制作表格,只需更新变量名。
  • 没有什么要更新的,因为变量都是一样的......但是 $output 的 print / vardump 是空白的
  • 其实我觉得我可以看出你哪里出错了,['title']在数组中,是关键。
  • 这绝对是完美的!非常感谢特里比。也似乎比原来的答案更干净。对我所说的“出错”表示歉意
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 2017-05-14
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 1970-01-01
相关资源
最近更新 更多