?
Solved

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

Posted on 2008-10-20
6
Medium Priority
?
328 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

Accepted Solution

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

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

800 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