Jump to content

php include with varying number of variables


hemerocallis

Recommended Posts

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.