I have a table that I want to display some records from my database. I have a specific spec I'd like the table to conform to, 3 cells wide. I can get the data into a cell. But my problem is, I'd like the table to have the 3 cells fill and then have the additional data create a new rows and continue on infinately. Hopefully someone can shed some light on this for me.
echo "<table border=1><tr><th>First name</th><th>Last name</th><th>Phone</th></tr>";//set up table $count=0; while($row = mysql_fetch_array($result, MYSQL_ASSOC))//Could have used mysql_fetch_assoc() instead { if($count&1){ //using bitshift operator ofr every other row //Here's a row that is not accented echo "<tr><td>{$row['first']}</td>";//print the value of the key $row['first'] (first name) echo "<td>{$row['last']}</td>"; //ditto except last name echo "<td>{$row['phone']}</td></tr>";//ditto except the phone number value $count++;//add 1 to count } else{ //Here's a highlighted row echo "<tr bgcolor=lightcyan><td>{$row['first']}</td>"; echo "<td>{$row['last']}</td>"; echo "<td>{$row['phone']}</td></tr>"; $count++;//add 1 to count } } echo "</table>";//close the table after the while loop
I think that is more of a vertical solution. I need somthing that will go across 3 cells then after the 3 cells are filled create a new row.
The above will print out three headers then below them the related data kind of like First Last Phone Sally Struthers 234-1234 John Doe 456-1324
I need it to be something like this. Sorry I don't speak the language to well.
<tr><td>$name </td> <td>$name </td> <td>$name </td><tr>
if more records exist create new <tr> below
That is exactly what my code is doing just formatted differently look closely;
echo "<tr bgcolor=lightcyan><td>{$row['first']}</td>";//<--start row here echo "<td>{$row['last']}</td>"; echo "<td>{$row['phone']}</td></tr>";//<--end row here $count++;//add 1 to count
Do you mean
define ("NUMCOLS",3); $res = mysql_query("SELECT col1, col2 FROM mytable"); $count = 0; echo "<TABLE border=1>"; while (list($col1, $col2) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>$col1<br>$col2</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } # end row if not already ended if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</TABLE>";