Page 1 of 1

Printing DB fields to table columns

Posted: Thu May 22, 2003 9:39 am
by Czar
How can i simplify this? I want to print contents of a rows from MySQL into a table so, that each value of a field prints to a table cell horizontally and when next row starts from db it prints to next table row. How can i do it so, that i dont need to specify a number of columns, but column number is automatically a number of fields in a row?

Here's the code

Code: Select all

<?php
$field_list = mysql_query("SELECT * FROM $table") or die(mysql_error());
	if(mysql_num_rows($field_list)) {
	  print "<table border=1 cellspacing=0>\n";
	   while($field = mysql_fetch_array($field_list)) {
		  print "<tr><td>".$field['0']."</td><td>".$field['1']."</td><td>".$field['2']."</td>
			<td>".$field['3']."</td><td>".$field['4']."</td><td>".$field['5']."</td>
			<td>".$field['6']."</td><td>".$field['7']."</td><td>".$field['8']."</td>
			<td>".$field['9']."</td><td>".$field['10']."</td></tr>";
			}
		print "</table>";
?>
I would appreciate any help.
?>

Posted: Thu May 22, 2003 11:55 am
by liljester

Code: Select all

<?php
$field_list = mysql_query("SELECT * FROM $table") or die(mysql_error());

if(mysql_num_rows($field_list))
{
     print"<TABLE border="1" cellspacing="0">\n";
     $num_fields = mysql_num_fields($field_list);
     while($field = mysql_fetch_array($field_list))
     {
          print"<TR>\n";
          $current_field = 0;
          while($current_field < $num_fields)
          {
               print"<TD>$field[$current_field]</TD>\n";
               $current_field++;
          }
          print"</TR>\n";
     }
     print"</TABLE>\n";
}

?>

Posted: Thu May 22, 2003 12:15 pm
by Czar
Thanks. Cleared it.

Posted: Thu May 22, 2003 12:15 pm
by liljester
Sweet =)