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 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?