Hi All,
This is a recurring newbie question and I thought I'd post an elegant solution to "how do I get values to show in a table horizontally for x number of columns?
Here is a simple way. Note: sometimes I break out of PHP for the <tr> and </tr> tags and sometimes I echo them. Reason: I want to be able to see the table in Dreamweaver and this doesn't freak it out by </tr>'s in the wrong place.
?><table class="models" cellpadding="0" cellspacing="0" border="1"><?php
//preliminary stuff - your data
$cols=4; //set as you wish
$a=array('data','data','data','data','more data','even more data','too much data..' /* etc. etc. - probably you're getting this from a database */);
//here is the table
$i=0;
foreach($a as $n=>$v){
$i++;
if(!fmod($i-1,$cols)){
//end previous row (only after first row)
if($i>1)echo '</tr>';
//start a row
?><tr class="1"><?php
}
?><td><?php
//your data here
?></td><?php
}
//finish the final row with empty cells and a space if needed
if($end=fmod($i,$cols)){
for($i=1; $i<=$cols-$end; $i++) echo '<td> </td>';
echo '</tr>';
}
?></table>