Print Database Data In Html Table
I know how to print data from database on html table but i have one task that i can not understand, and i can not do it. I have a table in database and when i select those data i
Solution 1:
To achieve a table layout like that you just need to condition the placement of the end of your table rows.
<table><tr><?php$count = 0;
while($row = mysql_fetch_assoc($result)) :
$count++;
?><td><?phpecho$row['value'] ?></td><?phpif($count % 2 == 0 || $count == mysql_num_rows($res)) : ?></tr><?phpif($count != mysql_num_rows($result)) : ?><tr><?phpendif; ?><?phpendif; ?><?phpendwhile; ?></table>
What the above does is use the modulus operator (%
, calculates the remainder from division) to print a closing and opening row tag whenever we're at an evenly numbered result.
Also, if you wanted to change your table layout to be 3 or 4 columns wide all you need to do is change the number applied to the modulus:
$count % 3 == 0//3 columns$count % 4 == 0//4 columns, etc
Solution 2:
<table><?phpwhile (TRUE) { ?><?php// Make an array of n rows. Trailing items may be FALSE if there// are not enough rows to fill the table row.//$rows= array();
for ($i= 0; $i<n; $i++)
$rows[$i]= mysql_fetch_assoc($result);
if (!$row[0])
break;
?><tr><?phpforeach ($rowsas$row) { ?><td><?phpif ($row) { ?>
Value: <?phpecho(htmlspecialchars($row['value']); ?><?php } ?></td><?php } ?></tr><?php } ?></table>
Post a Comment for "Print Database Data In Html Table"