|
Posted by Cliff Smith on 12/22/06 13:14
davihigh@gmail.com wrote:
> Hello all,
>
> I am looking for a neat and best practice way to layout a number of
> items (say, no more than 16 records) in one page, fill in a 4x4 grid.
> The most clear way is using <table> to display HTML. But the number of
> records is variable, so how i can program to render HTML code
> correctly? For example, if we have 5 records, it should appear 4 items
> at first row, and 1 item at second row. so on.
> I am new to PHP and PHP template, so i wanna start from this point to
> get more understand of PHP. I have also read book <Programming PHP> but
> there is no obvious answer of this.
>
> $result = mysql_query ("SELECT * FROM item", $connection);
> .......
>
> Expecting your great reponse. Thanks!
> Regards,
> David
>
Don't know if it would be "best practice", but this would work-
$result = mysql_query ("SELECT * FROM item", $connection);
$num_results = mysql_num_rows($result);
$num_rows = ceil($num_results/4);
for( $row=0; $row<$num_rows; $row++ ){
echo '<tr>';
for( $col=0; $col<4; $col++ ) {
$data = @mysql_fetch_assoc($result);
echo '<td>';
.....
do some stuff with the data
.....
echo ' </td>'; // the is important for // when no
data is returned
}
echo '</tr>';
}
Navigation:
[Reply to this message]
|