• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1030
  • Last Modified:

php looping an array

I have been racking my brain out and can't seem to think of a straight forward way to do this.

I have an array of objects image[1] through image[21]. Each object has 2 variables, a name and a path.

I need to loop through beginning to end and place it in a table layout.

Here is the pseudo code:

table
start loop
tr
td
div
add image[1] name and path
/div
/td
td
blank space
/td
td
div
add image[2] name and path
/div
/td
/tr
repeat loop until array has ended

see code below for better syntax.


<table width="250" border="0" cellspacing="0" cellpadding="0">
    <tr id="thumbs" class="fontThumbs">
        <td width="120" valign="top">
            <div align="center">
            <strong><?php echo $imgExt['v0']->get_name(); ?></strong><br />
                <a onclick="document.mainWindow.src='<?php echo $imgExt['v0']->get_Path(); ?>';" href="#"> <img src="<?php echo $imgExt['v0']->get_Path(); ?>" width="120" height="40" border="0" /></a>
                </div>
                </td>
                <td width="10">&nbsp;</td>
                <td width="120" valign="top"><div align="center">
                <strong><?php echo $imgExt['v3']->get_name(); ?></strong><br />
                <a onclick="document.mainWindow.src='<?php echo $imgExt['v3']->get_Path(); ?>';" href="#"> <img src="<?php echo $imgExt['v']->get_Path(); ?>" width="120" height="40" border="0" /></a>
            </div>
        </td>
    </tr>
</table>

Open in new window

0
javajamminman
Asked:
javajamminman
  • 5
  • 3
  • 3
  • +3
1 Solution
 
Thomas WheelerCommented:
what does print_r($imgExt) look like?
0
 
gr8gonzoConsultantCommented:
Sometimes it's easier to understand when you eliminate all the extra details (CSS, widths, etc). See if this snippet helps:
<?
$arr = array(0,1,2,3,4,5,6,7,8,9,10);
$col = 0;
$maxcols = 2;
print "<table>\n";
print "\t<tr>\n";
 
foreach($arr as $key => $val)
{
	$col++;
	if($col > $maxcols)
	{
		print "\t</tr>\n\t<tr>\n";
		$col = 1;
	}
 
	print "\t\t<td>" . $val . "</td>\n";
	
	if($col % 2)
	{
		print "\t\t<td>&nbsp;</td>\n";
	}
}
 
if($col <> $maxcols)
{
	print "\t\t<td>&nbsp;</td>\n";
}
print "\t</tr>\n";
print "</table>";
 
?>

Open in new window

0
 
gr8gonzoConsultantCommented:
There's probably a better way to handle the cell spacing (in case you ever want to add columns), but that should work for a IMG - SPACE  - IMG layout.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DubouxCommented:
I think you want 1 row with a linked-picture per column.

I made it, though I dunno objects that well.

I replaced the empty 10px wide column with a 2x 5px wider pic-column.
otherwhise you can set the cellspacing to 10.
<table width="250" border="0" cellspacing="0" cellpadding="0">
    <tr id="thumbs" class="fontThumbs">
        <?php
            foreach ($imgExt as $img) {
        ?>
        <td width="120" valign="top" align="center" style="padding: 0 5px 0 5px;">
            <strong><?php echo $img->get_name(); ?></strong><br />
            <a onclick="document.mainWindow.src='<?php echo $img->get_Path(); ?>';" href="#"><img src="<?php echo $img->get_Path(); ?>" width="120" height="40" border="0" /></a>
        </td>
        <?php
            }
        ?>
    </tr>
</table>

Open in new window

0
 
javajamminmanAuthor Commented:
Array (

[v0] => image Object (
[name] => Oceanside
[sp] => 795_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_795_0802.jpg
[spTnPath] => images/SPtn/IMG_795_0802.jpg )

[v1] => image Object (
[name] => Oceanside
[sp] => 798_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_798_0802.jpg
[spTnPath] => images/SPtn/IMG_798_0802.jpg )
 
[v2] => image Object (
[name] => Front
[sp] => 792a_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_792a_0802.jpg
[spTnPath] => images/SPtn/IMG_792a_0802.jpg )
 
[v3] => image Object (
[name] => Scr Porch
[sp] => 801a_0802
[vt] => 841a_0802
[folder] => images
[spLgPath] => images/SPlg/IMG_801a_0802.jpg
[spTnPath] => images/SPtn/IMG_801a_0802.jpg )
 
[v4] => image Object (
[name] => Porch - 1st
[sp] => 799_0802
[vt] => 843_0802
[folder] => images
[spLgPath] => images/SPlg/IMG_799_0802.jpg
[spTnPath] => images/SPtn/IMG_799_0802.jpg )
 
[v5] => image Object (
[name] => View
[sp] => 800_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_800_0802.jpg
[spTnPath] => images/SPtn/IMG_800_0802.jpg )
 
[v6] => image Object (
[name] => Porch - 2nd
[sp] => 823_0802
[vt] => 825_0802
[folder] => images
[spLgPath] => images/SPlg/IMG_823_0802.jpg
[spTnPath] => images/SPtn/IMG_823_0802.jpg )
 
[v7] => image Object (
[name] => View
[sp] => 824_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_824_0802.jpg
[spTnPath] => images/SPtn/IMG_824_0802.jpg )
 
[v8] => image Object (
[name] => Beach
[sp] => 796_0802
[vt] =>
[folder] => images
[spLgPath] => images/SPlg/IMG_796_0802.jpg
[spTnPath] => images/SPtn/IMG_796_0802.jpg )
0
 
javajamminmanAuthor Commented:
to clarify, i need a row with 3 columns

1st and 3rd column have images while the middle column has the spacer.

I need the row to repeat until every array element has been placed.
0
 
DubouxCommented:
Okay, this is not making any sense to me, lol

please post a print_r() from your object array
(or w/e the command needed for object arrays)


And in your first post, u show 3 columns. I thought the 3rd column was holding the 3rd pic.

And what do you want to happen after the first 3 columns.. add another 3 columns and keep it one row ?
or do you want a new row per 3 columns ?
0
 
javajamminmanAuthor Commented:
Sorry for the confusion

I want a row of 3 columns (the first and third have an image)
then add another row of 3 columns (again first and third have an image)
and keep repeat through the entire array.
0
 
DubouxCommented:
k, how about this ?

ps, I still don't know if $img->get_name() actually works..
<table width="250" border="0" cellspacing="0" cellpadding="0">
    <?php
        $col = 1;
        foreach ($imgExt as $img) {
            if ($col == 1) {
                echo "<tr id=\"thumbs\" class=\"fontThumbs\">";
            }
            if ($col == 2) {
                echo "<td width=\"10\">&nbsp;</td>";
            }
            else {
    ?>
    <td width="120" valign="top" align="center" style="padding: 0 5px 0 5px;">
        <strong><?php echo $img->get_name(); ?></strong><br />
        <a onclick="document.mainWindow.src='<?php echo $img->get_Path(); ?>';" href="#"><img src="<?php echo $img->get_Path(); ?>" width="120" height="40" border="0" /></a>
    </td>
    <?php
            }
            if ($col == 3) {
                echo "</tr>";
            }
            $col  =  ($col >= 3)  ?  1  :  ($col + 1);
        }
    ?>
</table>

Open in new window

0
 
bleach77Commented:
A little modification on Duboux: comments. Somehow I thinks it's not gonna work.
<table width="250" border="0" cellspacing="0" cellpadding="0">
    <?php
        $col = 1;
        foreach ($imgExt as $img) {
            if ($col == 1) {
                echo "<tr id=\"thumbs\" class=\"fontThumbs\">";
            }                        
    ?>
    <td width="120" valign="top" align="center" style="padding: 0 5px 0 5px;">
        <strong><?php echo $img->get_name(); ?></strong><br />
        <a onclick="document.mainWindow.src='<?php echo $img->get_Path(); ?>';" href="#"><img src="<?php echo $img->get_Path(); ?>" width="120" height="40" border="0" /></a>
    </td>
    <?php            
            $col++;
            if ($col == 2) {
                echo "<td width=\"10\"> </td>";
            }
 
            if ($col >= 3) {
                echo "</tr>";
                $col = 1;
            }
            
        }
        if($col!=1) {
            echo "<td></td></tr>";
        }
    ?>
</table>

Open in new window

0
 
javajamminmanAuthor Commented:
Ok, I solved it. I had to separate the array into 2 and then loop through each one. I only used one row with 3 columns and had them repeat in div tags. Thanks for all the imput
0
 
NerdsOfTechTechnology ScientistCommented:
Another way would to use modulus division so that for every even image (index) would place spacer image in 2nd column before hand and a row terminator after the even numbered image was placed.

Also fixed for odd counts to use 2 spacers on last row

=NerdsOfTech
<table width="250" border="0" cellspacing="0" cellpadding="0">
    <?php
        $imgcount = count($imgExt);
        $iscountodd = (count($imgExt) % 2);
        $col = 1;
        foreach ($imgExt as $img) {
            if (($col % 2) == 1) { // odd index START row
                echo "<tr id=\"thumbs\" class=\"fontThumbs\">";
            }
            else
            { // even index ADD SPACER
                echo "<td width=\"10\"> </td>";
            }
    ?>
    <td width="120" valign="top" align="center" style="padding: 0 5px 0 5px;">
        <strong><?php echo $img->get_name(); ?></strong><br />
        <a onclick="document.mainWindow.src='<?php echo $img->get_Path(); ?>';" href="#"><img src="<?php echo $img->get_Path(); ?>" width="120" height="40" border="0" /></a>
    </td>
    <?php            
 
            if (($iscountodd==1) && ($col==$imgcount)){ 
             // col is now last image except index count is odd add 2 SPACERS and TERMINATE LAST ROW
                echo "<td width=\"10\"> </td>";
                echo "<td width=\"10\"> </td>";
                echo "</tr>";             
            } 
            elseif ($col % 2 == 0) { // even index TERMINATE ROW
                echo "</tr>";
            }           
        }
    ?>
</table>

Open in new window

0
 
javajamminmanAuthor Commented:
Thanks for all the input.
0
 
NerdsOfTechTechnology ScientistCommented:
You are welcome.

=NerdsOfTech
0
 
NerdsOfTechTechnology ScientistCommented:
Please reconsider my answer as the optimal solution beyond splitting the array into two parts.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 5
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now