Solved

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

Posted on 2008-10-20
6
322 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 109

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 109

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

791 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