Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

Table rows

Code Wizard, 12-30-2006

A common function on many php (or other data driven) sites is to loop through an array and write the contents of the array to the page. It is preferable to spread your data over multiple columns instead of writing it all in a straight list thus forcing your readers to scroll forever. Here is an easy way to dynamically write your cells and rows using expressions.

<html><body><table><tr>
<?
$names=explode(",","Joe,Lisa,Bill,Roger,Fred,Kenne y");
$counter=-1;
while (list(,$value)=each ($names))
{ $counter=$counter+1;
echo "<td>" . $names[$counter] . "&nbsp </td>";
//this is the key - if field has no remainder then field is in second column
$newrow=($counter % 3);
//echo $newrow;
if ($newrow == 2) echo "</tr><tr>" ;
} // end of while
//****This will write the data in three cells per row. To only do two cells
//** per row change the 3 in the $newrow definition to a 2 and the
//** comparison to a 1.
?>
<td>&nbsp</td></tr></table></body></html>
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions