Link to home
Create AccountLog in
Avatar of helpchrisplz
helpchrisplz

asked on

help with foreach ($images as $img):

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.
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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.
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
Avatar of helpchrisplz
helpchrisplz

ASKER

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

SOLUTION
Avatar of Mark Brady
Mark Brady
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
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
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) } } }
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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

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.
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?
The grade will act as a multiplier for the points the solution receives:

ahh :O
sorry.