Jump to content

Outputting objects correctly


OriginalSunny

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>";
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.