HTML表格中的PHP显示数组

问题描述 投票:0回答:4

我将数据库和Web服务器放在不同的机器上,将mysql查询中的数组传递给Web服务器,看起来就像这样

Array (
[user_id] => 1
[username] => phillip
[password] => 12345
[email] => [email protected]
[account_balance] => 100   )

如果我使用print_r($ myArray)在PHP中打印出这个数组;它在Web浏览器中显示如此

 Array ( [user_id] => 1 [username] => phillip [password] => 12345 [email] => [email protected] [account_balance] => 100 ) 

我想创建PHP代码,迭代这个数组并为user_id,username,password,email和account_balance创建列,然后以行显示结果。

这是我在文件display.php中的PHP代码

<!DOCTYPE html>
   <html>
       <head>
           <meta charset="UTF-8">
           <title>Page</title>
       </head>
       <body>
          <table border="1">
<?php
 include(fileThatCallsDBServer.php);
 $myArray = getMYSQLArray(); //This calls a function located in the file in the include() and returns array
 print_r($myArray); //print the array for testing purposes

 //Create table to display array 
 $html = "<table>";
 foreach($myArray as $row) {
    $html .= "<tr>";
       foreach ($row as $cell) {
          $html .= "<td>" . $cell . "</td>";
       }
       $html .= "</tr>";
 }
 $html .= "</table>";
 ?>

       </body>

 </html>

但是,当我在浏览器上查看页面时,没有显示任何内容。想法?

php mysql arrays loops html-table
4个回答
1
投票

您需要显示键及其各自的值。

因此,在数组循环中,获取键和值。

你不需要两个foreach循环。

...
foreach($myArray as $key => $row) {
 $html .= "<tr>";
 $html .= "<td>" . $key . ': ' . $row . "</td>";
 $html .= "</tr>";
}
...

0
投票

你的代码看起来很好,你似乎忘了回显你的$ html变量的最终结果。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Page</title>
    </head>
    <body>
        <table border="1">
        <?php
        include(fileThatCallsDBServer.php);
        $myArray = getMYSQLArray(); //This calls a function located in the file in the include() and returns array
        print_r($myArray); //print the array for testing purposes

        //Create table to display array 
        $html = "<table>";
        foreach($myArray as $row) {
            $html .= "<tr>";
            foreach ($row as $cell) {
                $html .= "<td>" . $cell . "</td>";
            }
            $html .= "</tr>";
        }
        $html .= "</table>";
        ?>

        echo $html;

    </body>

</html>

0
投票
<?php
    $data = Array ( 'user_id' => 1,'username' => 'phillip', 'password' => 12345, 'email' => '[email protected]','account_balance' => 100 );
    $keys = array_keys($data);
?>
<table border=1>
    <tr>
        <?php
            foreach($keys as $key=>$value):
            ?>
                <td><?php echo $value;?></td>
            <?php
            endforeach;
        ?>
    </tr>
    <tr>
        <?php
            foreach($data as $key=>$value):
            ?>
                <td><?php echo $value;?></td>
            <?php
            endforeach;
        ?>
    </tr>
</table>

0
投票

您好你的数组不是嵌套数组所以你只需要回应它:

echo "<tr><td>".$myArray['user_id']."</td>
          <td>".$myArray['username']."</td>
          <td>".$myArray['password']."</td>
          <td>".$myArray['email']."</td>
          <td>".$myArray['account_balance']."<td></tr>";
© www.soinside.com 2019 - 2024. All rights reserved.