Solved

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

Posted on 2008-10-20
6
324 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 110

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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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