Jump to content


Photo

Outputting objects correctly


  • Please log in to reply
2 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 19 March 2006 - 03:15 PM

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>";

#2 insrtsnhere13

insrtsnhere13
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 20 March 2006 - 02:17 AM

$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++;
}

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

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 20 March 2006 - 08:56 PM

Here's my general-purpose code for multi-col output from an array

$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>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users