Click to See Complete Forum and Search --> : access variables in another PHP script file
dataline
07-24-2008, 02:23 PM
Hi,
Newbie question. Below is not exactly what I'm doing, but is similar.
I have an array of people names and other details (DOB, Sex, etc.). I'm displaying the names, and at the same time I want the names to be links which when clicked would run "Details.php" where all details are displayed for the name clicked.
for ($i=0; $i < $num_persons; $i++)
{
$person_name = $group_data[$i][1];
echo 'name = '.'<a href="Details.php">'.$person_name.'</a><br />';
}
Question: how can I access the array $group_data in Details.php?
Thanks in advance
DL
Phill Pafford
07-24-2008, 03:30 PM
for ($i=0; $i < $num_persons; $i++)
{
$person_name = $group_data[$i][1];
echo 'name = '.'<a href="Details.php?pname=' . $person_name . '">'.$person_name.'</a><br />';
}
as for details.php
$pname = $_REQUEST['pname'];
echo $pname;
// NOTE: Should validate this before using $pname in anything to avoid SQL Injection and other awful events
dataline
07-24-2008, 03:50 PM
Thanks for the help Phil.
Details.php displays the name fine. Now the question is how do I get Details.php to display the details for that person.
Phill Pafford
07-24-2008, 04:25 PM
well there are a couple of ways, the most preferred way would be by object. if your going to pass by URL then you have to pass all of them or you could do another query from details.php but I think this is a waste of time.
if pass by object you will need to make a class that holds all the data in an object then include the access to the object to the details.php page and use the object values there.
a better understanding on objects:
http://us3.php.net/zend-engine-2.php
http://www.killerphp.com/videos/object-oriented-php-videos.php
also search for class
Hope this helps
callumd
07-24-2008, 08:51 PM
I'd advise against using $_REQUEST, you really should instead use $_GET.
$_REQUEST is considered to be less secure.
dataline
07-28-2008, 11:33 AM
Phill and callumd,
Thank you both for your help!
I tried $_REQUEST and $_GET, and both work equally well. Being a newbie, I will take callumd's words and use $_GET.
Phill, I took a look at the 2 links you provided on objects in PHP5. I'm still running PHP4. So, I will skip this for now until I upgrade. What I will do for my issue is run the query in "details.php". As you said, this is a waste of time (hitting the database when the data is already available). Right now, my immediate objective is to get the code working. When I get to that, then I will look into improving efficiency (including upgrade to PHP5 and use objects, data validation to prevent SQL injection - another one of your suggestions, etc.)
Again thank you both!
DL