Re: dynamic table
| From: | Curt Zirzow | Date: | Wed, 26 May 2004 14:22:14 +0000 |
| Subject: | Re: dynamic table | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-186993@lists.php.net to get a copy of this message | ||
* Thus wrote nabil (nabil.attar@aws-syria.com):
> Hiya,
>
> How can i draw a new <tr> AFTER FIVE <td> in the following loop
>
> (i want to echo the records in 5 columns width tables whatever the number of
> records will be fetched)
>
> ..
> echo '<table>';
>
> while ($myrow = mysql_fetch_array($sql))
> {
> echo $myrow[0];
> }
> echo '</table>';
Use the modulus (%) operator.
$columns = 5; // Human readable number
$new_row = $columns - 1; // the magic number
$html_row = ''; // Column buffer
$i = 0; // Counter
echo '<table>';
while (...) {
$html_row .= "<td>{$myrow[0]}</td>";
// $i % $columns sequence will be:
// 0, 1, 2, 3, 4, 0, 1, 2, 3, 4 ...
if ( ($i % $columns) == $new_row ) {
echo "<tr>$html_row</tr>";
$html_row = '';
$i++;
}
}
// if we are on a sequence number other than zero
// html_row will contain some columns.
if(($i % $columns) != 0) {
echo "<tr>$html_row</tr>";
}
echo '</table>';
Curt
--
"I used to think I was indecisive, but now I'm not so sure."