Forum Moderators: coopster

Message Too Old, No Replies

Query Result Arrangement . .

How can I fill in a table?

         

Willis

4:42 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



What I wanna do is fill in a table per cell, so if I define the width to be idunno 4 colums, a <tr> </tr> would automaticly be shoved in after every 4 results. Making a query list of say 30 results, into 7 rows of 4, and the last row having only two.

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?

jatar_k

5:28 pm on Apr 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There is one around here somewhere that I posted but I am not sure where, I think this was the code this is made for building a dynamic table based on mysql results

$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>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";

MrCrowley

6:31 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



Can also do it with a modulo:

$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>&nbsp;</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!

Willis

4:25 am on Apr 24, 2004 (gmt 0)

10+ Year Member



jatar_k I took your aproach . . made alot of sense and easy to customize. Exactly what I was looking for!