用PHP创建一个表 [英] Creating a table with PHP

查看:75
本文介绍了用PHP创建一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用PHP创建表。

I am trying to create a table using PHP.

出于某种原因,我无法显示密钥。任何想法为什么?

For some reason I can't display the key. Any idea why?

这是表格。

这是我试过的代码:

Here's my code that I tried:

<table>
   <?php
       $users = array( 
                      array('first_name' => 'RS', 'last_name' => 'AD'),
                      array('first_name' => 'SQ', 'last_name' => 'FS'),
                      array('first_name' => 'SA', 'last_name' => 'Guillen'),
                      array('first_name' => 'AS', 'last_name' => 'Gs') 
       );

       $array_number = count($users)-1; 
       foreach($users as $key => $user) {
           for($i=0; $i<=$array_number; $i++){
               echo $users[$i]['first_name'].' '. $users[$i]['last_name'].'<br>';
           } 
       }
   ?>
</table>

有什么想法吗?

Any idea?

推荐答案

试试这个

Try this

<table>
    <tr>
        <th>User #</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Full Name</th>
        <th>Full Name in UpperCase</th>
    </tr>
    <?php
    $users = array(
        array('first_name' => 'Michael', 'last_name' => 'Choi'),
        array('first_name' => 'John', 'last_name' => 'Supsupin'),
        array('first_name' => 'Mark', 'last_name' => 'Guillen'),
        array('first_name' => 'KB', 'last_name' => 'Tonel')
    );


    foreach ($users as $key => $user) {
        echo "<tr>";
        echo "<td>" , $key + 1 , "</td>";
        echo "<td>" . $user['first_name'] . "</td>";
        echo "<td>" . $user['last_name'] . "</td>";
        echo "<td>" . $user['first_name'] . " " . $user['last_name'] . "</td>";
        echo "<td>" . strtoupper($user['first_name']) . " " . strtoupper($user['last_name']) . "</td>";
        echo "<td></td>";
        echo "</tr>";   
    }
    ?>

</table>

简要说明:

正如您在图像中显示的那样,您需要使用< th>< / th>< / code>来定义预定义标题,以及其他值从 $ users 数组可以放在循环中。

As you have shown within your image you need to define predefined headings which can be done using <th></th> and rest of those values you were getting from an $users array which can be placed within loop.

注意:这里您需要使用单循环而非嵌套循环,如示例

Note: Here you need to use single loop not nested loops as shown in example

这篇关于用PHP创建一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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