I have some PHP code which:
- connects to mysql table
- retrieves results
- formats results for display as HTML table (add tags, etc)
- stores the properly formatted results into a var named $echoResultsFinal
Then, inside of the HTML, I have some inline PHP. it:
- checks to see if $echoResultsFinal isset
- if it is, echo’s out its contents
- 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
foreachloop. The code you have is this:The important thing to note here is that your foreach is assigning all the keys to
$keyand all of the values to$valueas it iterates through the array you passed it ($key => $value). The problem here is you are using the$keyto retrieve values from the$rowarray. However, with the array passed in, the keys are the numbers 0-8 (the indices of the array). You can see this by doing avar_dumpof the array you pass to foreach:The
$keys that you are using are in brackets. So you are looking for numeric indexes in the$rowarray. This still gives you output because of the default behavior ofmysql_fetch_array():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’sSELECTordered 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 theSELECT, 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()ormysql_fetch_row()or pass theMYSQL_ASSOCorMYSQL_NUMconstants tomysql_fetch_array(). However, themysql_*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 likeMYSQL_BOTHso be careful!)To fix this, you should be using the
$valueto get the column from the query because this variable will contain the string name of the columns.Also note that I changed your
as $key => $valueto justas $valuebecause if you don’t use the key, you don’t need to specify a variable to receive it. Those are the two forms offoreach.