Solved

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

Posted on 2008-10-20
6
319 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

747 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

10 Experts available now in Live!

Get 1:1 Help Now