PHP生成的表列数据不在适当的位置?

I have some PHP code which:

  1. connects to mysql table
  2. retrieves results
  3. formats results for display as HTML table (add tags, etc)
  4. stores the properly formatted results into a var named $echoResultsFinal

Then, inside of the HTML, I have some inline PHP. it:

  1. checks to see if $echoResultsFinal isset
  2. if it is, echo's out its contents
  3. if not, echos out some stuff unrelated to the question

The problem: When the PHP generated table is echoed out, the table contents are not in the proper order.

The code which generates the table:

$echoResultArray = array();
$i = 0;
while ($row = mysql_fetch_array($result))
{
    $td = '';
    foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $key => $value)
    {
        $td .= "<td>" . $row[$key] . "</td>";
    }
    $echoResultArray[$i] = "<tr>" . $td . "</tr>";
    $i++;
}
//Table closing tag
$echoResultsClosing = "</tbody></table>";
mysql_close();

$echoResultData = '';
foreach($echoResultArray as $var)
{
    $echoResultData .= $var;
    $echoResultData .= PHP_EOL;
}

$echoResultFinal = $echoResult . $echoResultData . $echoResultsClosing;

Any idea what's up? :)

Thanks for any help!

There is a problem in your first foreach loop. The code you have is this:

foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $key => $value)
{
    $td .= "<td>" . $row[$key] . "</td>";
}

The important thing to note here is that your foreach is assigning all the keys to $key and all of the values to $value as it iterates through the array you passed it ($key => $value). The problem here is you are using the $key to retrieve values from the $row array. However, with the array passed in, the keys are the numbers 0-8 (the indices of the array). You can see this by doing a var_dump of the array you pass to foreach:

array(9) {
  [0] => string(10) "FIRST_NAME"
  [1] => string(9) "LAST_NAME"
  [2] => string(7) "RIT_ACC"
  [3] => string(6) "LINK_1"
  [4] => string(6) "LINK_2"
  [5] => string(6) "LINK_3"
  [6] => string(6) "LINK_4"
  [7] => string(6) "LINK_5"
  [8] => string(6) "LINK_6"
}

The $keys that you are using are in brackets. So you are looking for numeric indexes in the $row array. This still gives you output because of the default behavior of mysql_fetch_array():

The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

I've added the emphasis here to the quote from the docs.

So your call to mysql_fetch_array() returns an array with column names and values as well as a numeric index of the columns and their values. Your results were out of order because your query's SELECT ordered the fields in a different order than in the array you passed to foreach. Since the numeric index would correspond to the order of the fields in the SELECT, you were outputting them in the order the database gave them to you rather than the order you wanted.

To avoid this bug in the future, explicitly use mysql_fetch_assoc() or mysql_fetch_row() or pass the MYSQL_ASSOC or MYSQL_NUM constants to mysql_fetch_array(). However, the mysql_* functions have been deprecated and will be removed in PHP 5.4. I highly suggest you look into PDO. (PDO has a similar return strategy which returns the column names and indices like MYSQL_BOTH so be careful!)

To fix this, you should be using the $value to get the column from the query because this variable will contain the string name of the columns.

foreach(array('FIRST_NAME', 'LAST_NAME', 'RIT_ACC', 'LINK_1', 'LINK_2', 'LINK_3', 'LINK_4', 'LINK_5', 'LINK_6' ) as $value)
{
    $td .= "<td>" . $row[$value] . "</td>";
}

Also note that I changed your as $key => $value to just as $value because if you don't use the key, you don't need to specify a variable to receive it. Those are the two forms of foreach.