从PHP

时间:2018-02-12 06:07:53

标签: php html arrays loops html-table

我有一个从PHP fetchAll()函数返回的多维数组,如下所示:

 $sql="SELECT * FROM users ORDER BY id ASC LIMIT 10";
 $stmt = $db->prepare($sql);
 $stmt->execute();
 $numcolumn = $stmt->columnCount();  
 $res = $stmt -> fetchAll();

IT返回这样的数组。

Array
(
    [0] => Array
        (   [0] => 1
            [id] => 1
            [1] => Firstname one
            [firstname] => Firstname one
            [2] => Lastname one
            [lastname] => Lastname one
        )

    [1] => Array
        (
            [0] => 1
            [id] => 1
            [1] => Firstname one
            [firstname] => Firstname one
            [2] => Lastname one
            [lastname] => Lastname one
        )

    [2] => Array
        (
            [0] => 1
            [id] => 1
            [1] => Firstname one
            [firstname] => Firstname one
            [2] => Lastname one
            [lastname] => Lastname one
        )
)

如何在PHP循环中在html表中打印此数据?

<table>
<th>
 <td>id</td>
 <td>firstname</td>
 <td>lastname</td>
</th>
<tr>
 <td>1</td>
 <td>Firstname one</td>
 <td>Lastname one</td>
</tr>
</table>

请注意,键和值都来自数据库,而不是静态的。请帮忙。 已编辑:
IT代表密钥(不工作):

$keys = array_keys($res);
    for($i = 0; $i < 1 ; $i++) {
       foreach($res[$keys[$i]] as $key => $value) {

        $msg.="<th style='background-color:red;'>";
        $msg.= $key;  //will store column name of the table to msg variable
        $msg.="</th>";

      }}
     $msg.="</tr>";
     $i=0;
      $count=1; //used to print sl.no

用于值(工作):

foreach($res as $row){
  $msg.="<tr><td>".$count."</td>";
    for($i=0;$i< $numcolumn;$i++)
    {
        $var=$row[$i]; //will store all the values of row 
        $msg.="<td style='white-space: nowrap;word-break: keep-all;'>".$var."</td>";
    }
    $count=$count+1;
    $msg.="</tr>";
}

$msg.="</table>";
echo $msg;  

答案:

     $res = $stmt -> fetchAll(PDO::FETCH_ASSOC);

     $msg="<table><tr><th style='white-space: nowrap;background-color:red;'>Serial No.</th>";
// for keys:
    $keys = array_keys($res);
        for($i = 0; $i < 1 ; $i++) 
        {
           foreach($res[$keys[$i]] as $keyg => $value) 
           {
              $msg.="<th style='background-color:red;'>";
              $msg.= $keyg;  //will store column name of the table to msg variable
              $msg.="</th>";          
           }
        }
            $msg.="</tr>";
            $i=0;
            $count=1; //used to print sl.no
  //for values  
     foreach($res as $row => $key)
    {
       $msg.="<tr><td>".$count."</td>";

        foreach($key as $C=>$d)
        {
            $var = $d;
            $msg.="<td style='white-space: nowrap;word-break: keep-all;'>".$var."</td>";
        }

       $count=$count+1;
       $msg.="</tr>";
    }

    $msg.="</table>";
    echo $msg;  //for printing table in html

2 个答案:

答案 0 :(得分:0)

您可以尝试使用while这样的循环

 while($res)
 {
 echo "<tr><td>" . $res['id'] . "</td>" .
     "<td>" . $res['firstname'] . "</td>" .
     "<td>"  . $res['lastname'] . "</td></tr>";
 }

答案 1 :(得分:0)

您可以尝试foreach循环。

在使用循环迭代结果之前,您可以在表格头中添加字段名称。

foreach ($res as $row) {
    echo "<tr>" .
             "<td>" . $row['id'] . "</td>" .
             "<td>" . $row['firstname'] . "</td>" .
             "<td>" . $row['lastname'] . "</td>" .
         "</tr>";
}

修改:为动态字段使用索引。

你可以尝试这样的事情。

foreach ($res as $row) {
    echo "<tr>";
    for ($i=0; $i<$numcolumn; $i++) {
        echo "<td>" . $row[$i] . "</td>";
    }
    echo "</tr>";
}

参考:http://php.net/manual/en/control-structures.foreach.php