duanpo2813 2014-10-15 11:37
浏览 42
已采纳

从多维数组创建一个表

I need to generate an html table that shows information stored in the database. My code reads this information and puts it in a multidimensional array:

Array ( [1] => Array ( 
    [FIRST_NAME] => Admin 
    [MIDDLE_NAME] => Admin 
    [LAST_NAME] => Admin 
    [PHONE] => 
    [EMAIL] => 
) [2] => Array ( 
    [FIRST_NAME] => Jad 
    [MIDDLE_NAME] => 
    [LAST_NAME] => Jad 
    [PHONE] => 961558777
    [EMAIL] => jad.jad@gmail.com 
) [3] => Array ( 
    [FIRST_NAME] => Sara 
    [MIDDLE_NAME] => 
    [LAST_NAME] => Sara 
    [PHONE] => 
    [EMAIL] => 
) )

I need to show this array as a table with 5 columns: First Name, Middle Name, Last Name, Phone, Email

I tried this:

$fieldarray = array("First Name","Middle Name","Last Name", "Phone", "E-mail");

maketable("SELECT first_name, middle_name, last_name, phone, email FROM staff", $fieldarray);

function maketable($query, $fieldarray){
    //count number of columns

    $columns = count($fieldarray);
    //run the query

    $result = DBGet(DBQuery(($query))) or die(mysql_error()) ;
    // $itemnum = mysql_num_rows($result);


    if(count($result) > 0){ 
        do {   
            echo "< tr >" ;

            for($x = 0; $x < $columns; $x++) {
                echo "< td >" .$items[$fieldarray[$x]]. "< /td >" ;
            }

            echo "< /tr >" ;
        } while($items = mysql_fetch_assoc($result));
    }

but this is the result i get:

< table >< tr >< td >< /td >< td >< /td >< td >< /td >< td >< /td >< td >< /td >< /tr >< /table >

How can I solve this?

  • 写回答

3条回答 默认 最新

  • doubo3384 2014-10-18 08:22
    关注

    What both 2unco and B-and-P state is correct, you are using the wrong assoc key for your database fields. However, you also have another issue surrounding your do/while loop — in the fact that in the first iteration $items will not be set. This in itself will likely cause an error/warning(s) while PHP executes as the keys you are attempting to read from $items will not exist.

    To rectify both issues, you could try the following:

    <?php
    /// build a look up for both field names and nice equivalents
    $fieldarray = array(
      'first_name' => 'First Name',
      'middle_name' => 'Middle Name',
      'last_name' => 'Last Name',
      'phone' => 'Phone',
      'email' => 'E-mail'
    );
    function maketable($query, $fieldarray) {
        //count number of columns
        $columns = count($fieldarray);
        //run the query
        $result = DBGet(DBQuery(($query))) or die(mysql_error());
        $table = '';
        if ($result) { 
            while( ($items = mysql_fetch_assoc($result)) ) {
                /// build the table into a string, makes for easier function usage
                $table .= '<tr>';
                foreach ( $fieldarray as $field_name => $field_nice ) {
                    $table .= '<td>' .$items[$field_name]. '</td>';
                }
                $table .= '</tr>';
            } ;
        }
        /// only return markup if we found rows
        return $table ? '<table>' . $table . '</table>' : '';
    }
    /// I've switched all quotes to singular, as it's best to reserve double 
    /// quotes for strings you wish parsed for $variables.
    $table = maketable('SELECT first_name, middle_name, last_name, phone, email '.
                       'FROM staff', $fieldarray);
    echo $table;
    

    As an extension to the above you could improve the code by adding the ability to generate the table headers i.e. <th> when you build the first row of values — you would do this using the $field_nice variable.

    I need to generate an html table that shows information stored in the database. My code reads this information and puts it in a multidimensional array

    The actual code you paste doesn't build a multidimensional array, nor does it work from one either; it takes an assoc array directly from the database. Which is why I have built the code to handle just that. If you need to generate the table from a multidimensional array — you will need to change the maketable function to accept that array as its first argument and remove the database handling code from inside. With only slight modifications however you should be able to get it working for a multidim array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用