For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > May 2004 > SV: [PHP-DB] Drawing table by while









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SV: [PHP-DB] Drawing table by while
Henrik Hornemann

2004-05-26, 9:34 am

Something like this:

echo '<table>';
$count=3D1;
while ($myrow =3D mysql_fetch_array($sql))
{
If ($count=3D=3D5) {
echo "</tr>";
$count=3D1;
}
If ($count=3D=3D1) echo "<tr>";
$count++;
echo $myrow[0];
}
echo '</table>';

Hth Henrik Hornemann

-----Oprindelig meddelelse-----
Fra: nabil [mailto:nabil.attar@aws-syria.com]=20
Sendt: 26. maj 2004 14:28
Til: php-db@lists.php.net
Emne: [PHP-DB] Drawing table by while


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 =3D mysql_fetch_array($sql))
{
echo $myrow[0];
}
echo '</table>';


------------------------------
| x | y | z | o |
------------------------------
| f | q | h | hj |
------------------------------
..
..
..

--=20
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Mike S.

2004-05-26, 9:31 pm

Or like this (to be a little more complete, with comments):

// initialize counter
$count=0;

// start the table and the first row
echo "<table><tr>";

// loop through fetch results
while ($myrow = mysql_fetch_array($sql))
{
// if we've output 5 columns...
if ($count==5) {
// end the current row, start another
echo "</tr><tr>";
// and reset our counter
$count=0;
}
// output the next cell in this row
echo "<td>".$myrow[0]."</td>";
// and increment the counter
$count++;
}
// end the row and the table
echo '</tr></table>';


I've used this type of code before, but have not checked the specific code
above for spelling or other typographic errors.

The example code posted earlier (see below) had a small error in that the
counter was incremented twice if it was the first column, therefore only
printing 4 columns.


:Mike S.
:Austin TX USA



> Something like this:
>
> echo '<table>';
> $count=1;
> while ($myrow = mysql_fetch_array($sql))
> {
> If ($count==5) {
> echo "</tr>";
> $count=1;
> }
> If ($count==1) echo "<tr>";
> $count++;
> echo $myrow[0];
> }
> echo '</table>';
>
> Hth Henrik Hornemann
>
> -----Oprindelig meddelelse-----
> Fra: nabil [mailto:nabil.attar@aws-syria.com]
> Sendt: 26. maj 2004 14:28
> Til: php-db@lists.php.net
> Emne: [PHP-DB] Drawing table by while
>
>
> 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>';
>
>
> ------------------------------
> | x | y | z | o |
> ------------------------------
> | f | q | h | hj |
> ------------------------------
> .
> .
> .

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com