Solved

How do you pass a variable value to a php include?

Posted on 2008-10-20
6
320 Views
Last Modified: 2012-08-13
Hello,

I have the following bit of code in my database against each record where i wish to display photos:

<?php include ('./includes/travel_pic_thumbnails.php'); ?>

How can i define a value for $poi_id in this bit of php and pass it to the php include, so that when it (travel_pic_thumbnails.php - see the attached code snippet) runs it uses that value?

for example, i have tried:

<?php include ('./includes/travel_pic_thumbnails.php?$poi_id=163'); ?>

but that doesn't work so guess there's more to it than that. Any suggestions or help greatly appreciated.


Many thanks
<?php

error_reporting(E_ALL);
 

// GET THE ID FROM THE URL

$poi_id = $_GET["poi_id"];
 

// MAKE THE DIRECTORY ID

$directory_name = getcwd();

$directory_name .= '/travel_pics';

$directory_name .= '/' . $poi_id;

 

// TRY THE OPENDIR

if (!$handle = opendir($directory_name)) { 

   die("Cannot Open Directory $directory_name"); 

}

else {

   while (false !== ($file = readdir($handle))) {

       if ($file != "." && $file != "..") {

           echo "<a href=\"$file\" onclick='return hs.expand(this, { thumbnailId: 'thumb1' })' class='highslide'><img src=\"/thumbphp/phpThumb.php?src=/includes/travel_pics/$poi_id/$file\" alt=\"\" /></a>\n";         

       }

   }

   closedir($handle);

}

?>

Open in new window

0
Comment
Question by:Daniish
6 Comments
 
LVL 28

Expert Comment

by:gamebits
ID: 22763358
I do not see the include statement in your code but basically if the variable is defined before the include statement the code inside the include will use the variable

<?php

$poi_id = $_GET["poi_id"];

include ('./includes/travel_pic_thumbnails.php');

?>

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22763361
Drop one dollar sign!
<?php include ('./includes/travel_pic_thumbnails.php?poi_id=163'); ?>

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 22763371
Inside your script, variables start with dollar signs.  In the URL, the dollar sign is unnecessary and causes confusion.

You can use this statement to see what is in the URL:

var_dump($_GET);

Best regards, ~Ray
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 22763511
As gamebits says, you should be able to see the $poi_id variable from inside includes/travel_pic_thumbnails.php

However, if the code that uses $poi_id is inside a function, it will not be able to see the value of $poi_id unless you put the line:
  global $poi_id;
in the function (or of course pass the value in as a parameter).
0
 
LVL 2

Expert Comment

by:Gudorian
ID: 22764635
You could use a querystring that gets picked up on the other side like

$poi_id = 163;

<?php include ('./includes/travel_pic_thumbnails.php?poi_id='.$poi_id); ?>

Then on the page you're including get the variable by putting:

$poi_id = $_GET['poi_id'];
0
 

Author Closing Comment

by:Daniish
ID: 31508089
Thanks again Ray
I'm continuing this problem (which is at last in its final stages) as a new question: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23832767.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 24
form button worked now it doesnt anymore 9 39
WP_Query multiple custom taxonomies 5 24
Checking if varaible is empty 6 25
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now