Hello, I'm trying to create a html table for all the data in my database (mysql). My goal is to keep it dynamic, so if i'd add one variable to the array of columns we must load, it would also display that one. But it is not working. It currently displays this: Can anyone help me? The code: /*START------TABLE WITH USERDATA*/ $table = 'mytable'; $vars = array( 1 => 'user', 2 => 'runtime', 3 => 'created', 4 => 'xpgained', 5 => 'profit' ); echo "<table border='1'><tr>"; /*here we make the 'header' of the table*/ $length = sizeof($vars); for ($i = 1; $i < $length + 1; $i ++) { echo "<td>$vars[$i]</td>"; } echo "</tr>"; /*and here the table itself*/ $first = true; $imploded = implode(",", $vars); $sql = mysql_query("SELECT $imploded FROM $table"); while ($row = mysql_fetch_array($sql)) { if ($first == true) { for ($i = 1; $i < $length + 1; $i ++) { $string .="<td>".$row[$vars [$i]]."</td>"; } } echo "<tr>"; echo $string; echo "</tr>"; $first = false; } echo "</table>"; /*END------TABLE WITH USERDATA*/ PHP:
Use mysql_fetch_row instead of mysql_fetch_array. Example: <?php // table to use $table = 'mytable'; // fields to include $fieldHeaders = array('user','runtime','created','xpgained','profit'); // begin the table $output = '<table border="1"><tr>'; foreach ($fieldHeaders as $field) $output .= '<td>'.$field.'</td>'; $output .= '</tr>'; // table body $sql = mysql_query('SELECT '.implode(',', $fieldHeaders).' FROM '.$table); while ($row = mysql_fetch_row($sql)) { $output .= '<tr>'; foreach ($row as $el) $output .= '<td>'.$el.'</td>'; $output .= '</tr>'; } // finish the table $output .= '</table>'; // output the generated table echo $output; ?> PHP: I haven't tested that, but it should work. Are you a C coder by any chance?
Thanks for your reply sir Deacalion. Though PHP, CSS, HTML, Java, JavaScript and MySQL are my main languages, i have actually done a very little C coding.. Probably to help you in any way.
Haha i've done alot of JAVA. BTW: Thanks alot for your code. it is working very well. I'm going to use it to make a highscore function. Thanks.