简体   繁体   中英

create a table from a multidimensional array

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?

The problem you're having is in your $fieldarray variable, where you're using "human friendly" strings like "First Name", "E-mail" and such. The information being returned from the database looks like this:

Array ( [FIRST_NAME] => Admin [MIDDLE_NAME] => Admin [LAST_NAME] => Admin [PHONE] => [EMAIL] => )

You have to be very specific in code: FIRST_NAME is not the same as First Name .

Likely that your array key will be the DB column name, no? so instead of First Name you need to look for first_name etc.

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 ie <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM