?
Solved

php looping an array

Posted on 2009-04-08
15
Medium Priority
?
1,021 Views
Last Modified: 2013-12-12
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
Comment
Question by:javajamminman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +3
15 Comments
 
LVL 7

Expert Comment

by:Thomas Wheeler
ID: 24103538
what does print_r($imgExt) look like?
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 24103562
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
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 24103573
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!

 
LVL 3

Expert Comment

by:Duboux
ID: 24103659
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
 

Author Comment

by:javajamminman
ID: 24103680
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
 

Author Comment

by:javajamminman
ID: 24103698
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
 
LVL 3

Expert Comment

by:Duboux
ID: 24103716
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
 

Author Comment

by:javajamminman
ID: 24103733
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
 
LVL 3

Expert Comment

by:Duboux
ID: 24103775
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
 
LVL 4

Expert Comment

by:bleach77
ID: 24104380
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
 

Accepted Solution

by:
javajamminman earned 0 total points
ID: 24113371
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
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24136179
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
 

Author Comment

by:javajamminman
ID: 24234606
Thanks for all the input.
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24235138
You are welcome.

=NerdsOfTech
0
 
LVL 19

Expert Comment

by:NerdsOfTech
ID: 24237650
Please reconsider my answer as the optimal solution beyond splitting the array into two parts.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question