Forum Moderators: coopster
Basicly I'm sure you seen webpages where pictures are offered, they arrage them in tables of maybe 5 wide and however long pends on the number of pictures. . I wanna do something along that concept.
Anyone know how to do somethin like this?
$tdcount = 1;
$numtd = 3; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)) {
if ($tdcount == 1) echo "<tr>";
echo "<td>some stuff: $tdcount</td>";
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td> </td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
$records = array('<TD>record 1</TD>', '<TD>record 2</TD>', '<TD>record 3</TD>', '<TD>record 4</TD>', '<TD>record 5</TD>', '<TD>record 6</TD>');
$rowlength = 5;
$n = count($records);
$npad = $n % $rowlength;
//padd array to make it a multiple of the number of desired cells by row
if ($npad) $npad=$rowlength-$npad;
for ($i=0;$i<$npad;$i++){
$records[] = '<TD> </TD>';
}
//now you know that the array has a number of records that can be divided by the number of cells per row.
//so you could:
echo '<TABLE border=1>';
$i=1;
foreach($records as $cell){
if(($i%$rowlength) == 1){
echo '<TR>';
}
echo $cell;
if(($i%$rowlength)==0){
echo '</TR>';
}
$i++;
}
echo '</TABLE>';
It seems rather complicated to use modulos to do it, but for my templating system, I need to have an array of records that fits exatcly. Might help someone!