Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with foreach ($images as $img):

Posted on 2013-05-14
13
Medium Priority
?
427 Views
Last Modified: 2013-05-18
Hi i am making a before and after plugin for a CMS. The plugin requires 2 images on each loop of the foreach. one for the before and one for the after. the 2 images are then layered on top of each other

my code gets the same image out twice. can i make the loop store the URLs and then echo the url i want? am not good with arrays:

<?php foreach ($images as $img):
?>	

<img src="<?php echo $img->large->src; ?>" alt="Before"/>
<img src="<?php echo $img->large->src; ?>" alt="After"/>  
			

<?php  endforeach; ?> 

Open in new window


I am actually tying to output the before and after into an image slider. So i need to get 2 images out for each slide <li> of the image slider.

hope thats understandable.
0
Comment
Question by:helpchrisplz
[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
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39165545
It sounds a little like $images only has one image in it, but if you have a URL that demonstrates the problem, that might help.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39165554
It looks like you are outputting the exact same image (twice) only changing the 'alt' property.

What is the end result you want to see? You could create the actual div element that you want to see on your page inside the php script and add each image to the same container - then position them at the same location so they overlay.

It would help to see the image class you are using. Where does $images come from (where is it set and what is in it exactly)?

This will be easy to do but I need a little more information to be able to help you.

Cheers
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39165870
$images as $img is just the helper class that the CMS (concrete5) gives to output images that have been put in a group / set


all i know is:

when it first goes through the foreach loop it gets the first images url in the group and then the next loop it gets the second.

what i need is:
a way of outputting 2 images on each loop or storing the out put of the loop in a varable and echoing out 2 images from the varable each time
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:helpchrisplz
ID: 39165922
so in code it will look like this:
the <li> is one slide on the jquery image slider
the <div id="before-and-after"> is the before and after

i allready have it working but i need to make it dynamic by outputting the the before and after images each time it goes through the loop

<ul>

<?php foreach ($images as $img): ?>	
<li>

<div id="before-and-after">
<img src="<?php echo $img->large->src; ?>" alt="Before"/>
<img src="<?php echo $img->large->src; ?>" alt="After"/> 
<div> 	
		
</li>
<?php  endforeach; ?> 

</ul>

Open in new window

0
 
LVL 20

Assisted Solution

by:Mark Brady
Mark Brady earned 750 total points
ID: 39165934
I'm still as little confused. How many images are going to be in this 'group' you mention?

If there are only two images and their URL's are unique/different then that is one thing but according to your code this is the same image and you are only changing the 'alt' field. If you output a div element with both of those images inside the div (overlayed) are run your mouse over the top image you will only get the 'alt' text from the top image.

Unless you are going to swap the image order (change the z-index) programmaticaly  at some point.

You say your code is pulling the same image out twice. That is because you are outputting the same thing twice in a row.

$img->large->src;

That value is not going to change inside your loop unless there is other code in there to change it.

I think a good place to start would be to create an array to store the URL(s) then you can pull from that array after the loop runs.

$results = array();

foreach ($images as $img) {
    array_push ($results, $img->large->src);
}

Now you have an array with any URL's that came from $img->large->src but you should var_dump($images) and show us the result.

We can't what is in that object so who knows how to get the proper values out.
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39166050
so the array $results will just have the url for however many images i have assigned to the set.


Imagine i had 10 images in the set.

i need to get 2 images out each time in this order:

loop 1: get url for image 1 & 2
loop 2: get url for image 3 & 4
loop 3: get url for image 5 & 6
loop 4: get url for image 7 & 8
loop 5: get url for image 9 & 10

no more images left so loop ends

how can i split the array into individual steps that i can output into each slide.
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39166223
Step 1:  Read my request in the last post (supply a var_dump of $images)
Step 2: Post that var dump in here so I can see what is inside the object
Step 3: I will then be able to write a proper loop structure for you and have the results you need.

Cheers
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39166522
ok i used this:
$results = array();

foreach ($images as $img) {
    array_push ($results, $img->large->src);
}

var_dump($images);

Open in new window


and got:

array(6) { [0]=> object(stdClass)#663 (9) { ["fID"]=> string(1) "9" ["titleRaw"]=> string(6) "1a.JPG" ["title"]=> string(6) "1a.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#665 (3) { ["src"]=> string(41) "/sites/gibson/files/4013/6853/6060/1a.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#668 (3) { ["src"]=> string(65) "/sites/gibson/files/cache/79646c9c16d6331d2a53e50f2b3c3129_f9.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#669 (3) { ["src"]=> string(65) "/sites/gibson/files/cache/a8893c3e3933fc474986228494df244b_f9.JPG" ["width"]=> int(90) ["height"]=> int(60) } } [1]=> object(stdClass)#670 (9) { ["fID"]=> string(2) "10" ["titleRaw"]=> string(6) "1b.JPG" ["title"]=> string(6) "1b.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#672 (3) { ["src"]=> string(41) "/sites/gibson/files/4113/6853/6064/1b.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#673 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/0fc1fadf5d137dcd72c0098e69a2df00_f10.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#674 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/f174e2e75ba152d90a34163cfe327dd1_f10.JPG" ["width"]=> int(90) ["height"]=> int(60) } } [2]=> object(stdClass)#675 (9) { ["fID"]=> string(2) "11" ["titleRaw"]=> string(6) "2a.JPG" ["title"]=> string(6) "2a.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#677 (3) { ["src"]=> string(41) "/sites/gibson/files/5713/6853/6068/2a.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#678 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/7b28f3b02d2607b0e8acdc2e70ebb883_f11.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#679 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/42b4d9c68e3662091622e2f71440172a_f11.JPG" ["width"]=> int(90) ["height"]=> int(60) } } [3]=> object(stdClass)#680 (9) { ["fID"]=> string(2) "12" ["titleRaw"]=> string(6) "2b.JPG" ["title"]=> string(6) "2b.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#682 (3) { ["src"]=> string(41) "/sites/gibson/files/1913/6853/6072/2b.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#683 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/0b56e985e84ef0ab4149829f6ac39485_f12.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#684 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/20f7a2e7927aab971098fc625966d632_f12.JPG" ["width"]=> int(90) ["height"]=> int(60) } } [4]=> object(stdClass)#685 (9) { ["fID"]=> string(2) "13" ["titleRaw"]=> string(6) "3a.JPG" ["title"]=> string(6) "3a.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#687 (3) { ["src"]=> string(41) "/sites/gibson/files/1613/6853/6075/3a.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#688 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/03c6914fea8095f3f8fb43b713b6a2d7_f13.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#689 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/3183ef6963a334cc57b241de6b183078_f13.JPG" ["width"]=> int(90) ["height"]=> int(60) } } [5]=> object(stdClass)#690 (9) { ["fID"]=> string(2) "14" ["titleRaw"]=> string(6) "3b.JPG" ["title"]=> string(6) "3b.JPG" ["descriptionRaw"]=> string(0) "" ["description"]=> string(0) "" ["linkUrl"]=> string(0) "" ["orig"]=> object(stdClass)#692 (3) { ["src"]=> string(41) "/sites/gibson/files/9513/6853/6079/3b.JPG" ["width"]=> int(3648) ["height"]=> int(2432) } ["large"]=> object(stdClass)#693 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/0baf79fae9f28cdda2fbe76d06992c79_f14.JPG" ["width"]=> int(868) ["height"]=> int(400) } ["thumb"]=> object(stdClass)#694 (3) { ["src"]=> string(66) "/sites/gibson/files/cache/152bc39e74946c2a8e4fe9561170da6b_f14.JPG" ["width"]=> int(90) ["height"]=> int(60) } } }
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 750 total points
ID: 39167941
$img->large->src
When your script uses this twice, you're going to get the same variable every time.  It sounds to me like you want to use one of these functions.
http://php.net/manual/en/function.array-slice.php
http://php.net/manual/en/function.array-pop.php (used twice)

Be sure to read the man pages carefully. Array_Slice() returns a  value but does not modify the original array.  Array_Pop() modifies the array.
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39177576
managed to get it working thanks:

$i = 0;
foreach($images as $img){
	
if($i % 2)
  {
				?>
				<img src="<?php echo $img->large->src; ?>" alt="Before" width="<?php echo $img->large->width ?>" height="<?php echo $img->large->height ?>"/>
					
				<?php 
  }else{
				?>
				<img src="<?php echo $img->large->src; ?>" alt="After" width="<?php echo $img->large->width ?>" height="<?php echo $img->large->height ?>"/>
					
				<?php
  }
	
    $i++; 
        
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39177620
Another bad grade?!  WTF?

In the year 2013, you have graded 10 questions, and only 4 have been "A" grades.  That is unheard of here at Experts-Exchange, where the default grade is "A" and the grading guidelines call for you to explain any grade that is not "A."  The answers just cannot possibly be that bad.  In this case, it seems the answers were particularly apt, and led you to the solution.  The grade should have reflected that fact.

http://support.experts-exchange.com/customer/portal/articles/481419

Please, get with the program.  Or at least explain why we should help you if you give marked-down grades without any explanation of what we did wrong.
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39177629
sorry. i didn't know that the grade ment much or stud for a lot. does it look bad on your profile?

i guess i give an A if the answer i get is a working example of how to do it. i will be more generous in future as i know you do help me alot Ray! not sure if i can reopen this question?
0
 
LVL 1

Author Comment

by:helpchrisplz
ID: 39177632
The grade will act as a multiplier for the points the solution receives:

ahh :O
sorry.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

688 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