Newbie: foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nytol
Forum Newbie
Posts: 13
Joined: Thu Jul 30, 2009 3:53 pm

Newbie: foreach

Post by Nytol »

I am (trying) to create an online gallery. I have a div to show the thumbnails, and a div to show the main image when you click the thumbnail (I guess like a banner rotator).

Main Image Div:

Code: Select all

<div class="main_image"><img src="<?php echo $p['image']; ?>"/></div>
Thumbnail Div:

Code: Select all

<?php foreach( $posts as $p ) { ?>
    <div class="post">
        
        <?php if( $p['image'] ) { ?>
            <a href="<?php echo $p['image']; ?>">
                <img src="<?php echo $p['thumb']; ?>"/></a>
 
    </div>
 <?php } else { ?>
In

Code: Select all

config.class.php
I have:-

Code: Select all

public static $postsPerPage = 4;
It shows the 4 latest thumbnails in the thumbnail Div. When you click on a thumbnail, the main image appears in the main image Div:

It works fine except when you initially load the page, no main image shows UNTIL you select a thumbnail. I tried adding this to the main DIV...

Code: Select all

<?php foreach( $posts as $p ) { ?>
<div class="main_image"><img src="<?php echo $p['image']; ?>"/></div> 
<?php } else { ?>
It works, but obviously shows 4 main images. How can I change

Code: Select all

<?php foreach( $posts as $p ) { ?>
to only show 1 image just on this occasion? If I change

Code: Select all

public static $postsPerPage = 4;
to =1 then I will only get 1 thumbnail.

I am not a programmer, I am using bits and bobs of code from several sources so go easy on me. :) But I am trying to learn.
joeynovak
Forum Commoner
Posts: 26
Joined: Sun May 10, 2009 11:09 am

Re: Newbie: foreach

Post by joeynovak »

You should get a book and read it. :lol:

But... $posts is an array (like a list of things). The for each loop "iterates" (that's a magic word that means, goes through each thing in the list one at a time). And creates the variable $p that represents an item in the array (a list). So... There are other ways to get the items out of the list (elements out of the array),try...

Code: Select all

$p = $posts[0];
It will set $p equal to the first "thing" or element in $posts. This "may" not work... Because... Every element in an array (thing in a list) has a "key", for just a plain list, the "key" is a number, and the first "element" in the array's "key" is 0, the second is 1, etc... But... It is possible the keys aren't 0,1,2,3 but rather something else. But, it's unlikely.

I think you can also do:
$p = array_pop($posts);
FYI: That will remove "pop" the first (or last, I forget) "element" off of the array, so it will change the array by removing whatever you get from array_pop($posts) from the array. Probably harmless in your code, but nice to know.

Happy Coding,

Joey
Nytol
Forum Newbie
Posts: 13
Joined: Thu Jul 30, 2009 3:53 pm

Re: Newbie: foreach

Post by Nytol »

Thanks so much!

Code: Select all

<?php $p = $posts[0]; ?>
worked like a charm. Incredible.
Post Reply