loop question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

loop question

Post by gurjit »

i have a query:

$sql_enquiry = "select fname,lname from tbl_enquiry order by eqid asc";
$result = mysql_query($sql_enquiry,$my_conn);
$num_rows = mysql_num_rows($result);

i want to loop the results like this:

---------------------------
Result 1 | Result 2 | Result 3 |
---------------------------
Result 4 | Result 5 | Result 6 |
---------------------------
Result 7 | Result 8 | Result 9 |
---------------------------
Result 10 | Result 11 | Result 12

---------------------------


HOW CAN I DO THIS
Monk
Forum Newbie
Posts: 8
Joined: Wed Jul 16, 2003 3:19 am
Location: Germany - Temporarily

Post by Monk »

User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Code: Select all

$result = Array(); 
$x = 0; 
while ($row = mysql_fetch_array($sql_enquiry)) { 
$result[$x] = $row; 
$x++; 
} 
echo $result[$x][0];
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

does'nt seem to work with the code supplied Kriek,
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

Code: Select all

echo '<table border="1">';
$tmp_cnt = 0;
$per_row = 3;
while($row = mysql_fetch_array($result))
    {
    echo (($tmp_cnt %$per_row == 0) ? '<tr>' : '').
    ' <td>' .$row['fname']. ' ' .$row['lname']. '</td>'
        .((++$tmp_cnt %$per_row == 0) ? '</tr>' : '');
    }
echo ($tmp_cnt %$per_row !== 0)
    ? '<td colspan="' .($per_row - ($tmp_cnt % $per_row)). '">& nbsp;</td></tr>'
    : '';
echo '</table>';
should do. remove the space from the & nbsp; in the filler td.
Post Reply