I am using a for loop to generate a table for a SQL query’s results. I am trying to alter the code so that new text is echoed into a specific column of the table (column 8)
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='0' id=resultTable><tr>";
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td id" . $i;
echo "></td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
mysql_free_result($result);
?>
what actually do you mean by “new text” in “I am trying to alter the code so that new text is echoed into a specific column of the table”?
Is “text” the name of a particular table field? If so, it would be better to alter the sequence of the fields in the mysql_query that you are using instead of doing any change in the html generation for this
e.g.
Put
new_textin the eighth position.Update
If you want to display various things for various columns in the result table, you can put a counter and do what you want as per count like this:-
But it would be more readable,maintainable and customizable to have some config array where you specify which column is going to hold what like this:-
and then use this in your iteration:-