Page 1 of 1

loop question

Posted: Thu Aug 07, 2003 8:48 am
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

Posted: Thu Aug 07, 2003 9:25 am
by Monk

Posted: Thu Aug 07, 2003 9:36 am
by Kriek

Code: Select all

$result = Array(); 
$x = 0; 
while ($row = mysql_fetch_array($sql_enquiry)) { 
$result[$x] = $row; 
$x++; 
} 
echo $result[$x][0];

Posted: Thu Aug 07, 2003 9:57 am
by gurjit
does'nt seem to work with the code supplied Kriek,

Posted: Thu Aug 07, 2003 11:37 am
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.