faqts : Computers : Programming : Languages : PHP : Database Backed Sites

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 7 people (43%) answered Yes
Recently 3 of 7 people (43%) answered Yes

Entry

How can I display my query results in a 2 column HTML table?

Aug 1st, 2004 05:16
Cinley Rodick, Bill Sanders, Jon F, http://www.generalforums.com , http://www.generalforums.com/yellow/


If you have only two fields in your query, you can use the following:
$result = mysql_query($query) or die("Could not run query.");
$numfields = mysql_num_fields($result);
  echo "<table cellpadding='2' cellspacing='0' border='1'>\n";
  echo "\t<tr>\n";
  for ($i=0; $i<$numfields; $i++) { 
    echo ("\t\t<th>".mysql_field_name($result,$i)."</th>\n"); 
  }
  echo "\t</tr>\n";
  $j=0;
  while ($answer = mysql_fetch_array($result)) {
    echo "\t<tr>\n";
    for ($k=0; $k<$numfields; $k++) { 
      echo "\t\t<td>" .$answer[$k]. "</td>\n"; 
    }
    echo ("\t</tr>\n"); 
    $j++;
  /*// used to repeat header row after 7 rows of results
    if ($j%7==0) { 
    echo "\t<tr>\n";
      for ($m=0; $m<$numfields; $m++) { 
        echo ("\t\t<th>".mysql_field_name($result,$m). "</th>\n"); 
      } 
    echo "\t</tr>\n";
    } */
  }
  echo "</table>\n";