Jump to content

Archived

This topic is now archived and is closed to further replies.

OriginalSunny

Outputting objects correctly

Recommended Posts

Hi,
I am trying to ouput data in the way i want. I have used the code below and the data is output correctly, but there are too many objects on one row. All objects are output on one row and i want to limit it to 5 objects per row, so once the array reaches 5 it begins a new row. I do however still want all objects in the array to be output. How do i alter the code to do this?? ( I know it can't be too difficult but the only way i can see is by limiting the array to 5 and this will not ouput all the objects i want). Thanks.

echo"<table cellpadding='10'>";
echo "<tr>";
foreach($food_categories as $key => $subarray)
{
foreach($subarray as $type)
{
echo "<td>";
echo "<input type='radio' name='interest'
value='$type'><b><img src='$type' width=100 height=100></b><br>\n";
echo "</td>";
}
}
echo "</tr>";
echo "</table>";

Share this post


Link to post
Share on other sites
[code]$i = "1";
    while($row  = mysql_fetch_object($result)) {
if ($i == "1") {
    echo "<tr>"; }
if ($i == "3") {
    echo "</tr";
    $i = "1"; }
    echo "<center><td><p align=\"center\"><a href=\"$row->url\"><img src=\"$row->url\" heigh=\"%40\" width=\"%40\"           border=\"0\"></a><br>$row->caption</p></td>";
    $i++;
} [/code]

thats what i use for my gallery, obviously you need to format it for your own needs

Share this post


Link to post
Share on other sites
Here's my general-purpose code for multi-col output from an array

[code]$myArray = array(1,2,3,4,5,6,7,8,9,10,11,12);

define ("NUMCOLS",5);
$count = 0;
echo "<TABLE border=1>";
foreach ($myArray as $item) {
     if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row
     echo "<TD>$item</TD>\n";
     $count++;
     if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
    # end row if not already ended
    while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
    echo "</TR>\n";
}
echo "</TABLE>";

echo "<table><tr>";
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.