hemerocallis Posted April 11, 2012 Share Posted April 11, 2012 Hey everyone, I'm new to PHP so excuse me if this is an obvious question: I've got a horizontal scrolling image gallery as an include, as it will be used in dozens of pages. Each page that includes the image gallery will be using different pictures and in different numbers (anywhere from 1 - 15 images). In each page that includes the image gallery, I've turned each image to be used into a variable.. $image 1 = "images/image1.jpg"; $image 2 = "images/image2.jpg"; ... $image 7 = "images/image7.jpg"; etc. Now, I imagine I need to write a loop in the include that will account for as many pictures as there are variables in each page, without adding links for variables that aren't in the page in question. Something like (simplified): Defined on page: $image 1 = "images/image1.jpg"; $image 2 = "images/image2.jpg"; $image 3 = "images/image3.jpg"; $image 4 = "images/image4.jpg"; Loop in photo gallery include: <?php for($i = 1; $i <=15; $i++) { $imagei = ("$" ."image".$i); if (defined($imagei)) { echo '<li><a href="' .$imagei.'">'; echo '<img src=" '.$imagei.'" >'; echo '</a></li>'; } } ?> I know this doesn't work because $imagei isn't referring to the defined variables on the external page, but I'm not sure exactly where to go from here. As in, $imagei in the first execution of the loop is equal to "$image1", but how do I link that to the $image1 variable defined in the page (as "image/image1.jpg") to display the image? Does that make sense? Thanks! Quote Link to comment Share on other sites More sharing options...
bspace Posted April 11, 2012 Share Posted April 11, 2012 forget the $imagei stuff put the refs in an array eg $pageImages = array("images/image1.jpg", "images/image2.jpg", "images/image3.jpg"); loop through the array Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 11, 2012 Share Posted April 11, 2012 A) You would use an array, instead of a sequence of named variables. Arrays are for sets of data. You would then simply loop over the array. See: foreach B) How do you know which images are to be used for any page? What defines the range of images? Are they present in a specific folder for each page, in which case you would simply get a listing of the images and loop over them to output the <img tags? Quote Link to comment Share on other sites More sharing options...
hemerocallis Posted April 11, 2012 Author Share Posted April 11, 2012 Wow, quick replies. An array definitely makes more sense, I'll get on it. Thanks guys! Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 11, 2012 Share Posted April 11, 2012 While the proposed changes are indeed correct, it is possible to do what you're attempting to do. You need to change any reference to $imagei to $$imagei after the first definition. Try this to see what I mean, inside your loop. $imagei = 'image'.$i; if (defined($$imagei)){ echo 'imagei name: '.$imagei.' - imagei value: '.$$imagei; } Quote Link to comment Share on other sites More sharing options...
xyph Posted April 11, 2012 Share Posted April 11, 2012 While the proposed changes are indeed correct, it is possible to do what you're attempting to do. You need to change any reference to $imagei to $$imagei after the first definition. Try this to see what I mean, inside your loop. $imagei = 'image'.$i; if (defined($$imagei)){ echo 'imagei name: '.$imagei.' - imagei value: '.$$imagei; } define is used to check constants, isset is used to check variables/indexes. Overall though, you should avoid variably-named variables, and instead stick to an associative array. They're much easier to isolate/track/manage. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 11, 2012 Share Posted April 11, 2012 Variable-variables are also three times slower than using an array. Quote Link to comment Share on other sites More sharing options...
Jessica Posted April 11, 2012 Share Posted April 11, 2012 While the proposed changes are indeed correct, it is possible to do what you're attempting to do. You need to change any reference to $imagei to $$imagei after the first definition. Try this to see what I mean, inside your loop. $imagei = 'image'.$i; if (defined($$imagei)){ echo 'imagei name: '.$imagei.' - imagei value: '.$$imagei; } define is used to check constants, isset is used to check variables/indexes. Overall though, you should avoid variably-named variables, and instead stick to an associative array. They're much easier to isolate/track/manage. True, the defined was from his code. I agree, just pointing out that it is possible, he just wasn't constructing the variable correctly. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.