How to call a variable from a different php page?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
hibbard
Forum Newbie
Posts: 11
Joined: Sun Nov 02, 2003 1:15 pm

How to call a variable from a different php page?

Post by hibbard »

Hello, I was wondering how I would call a variable i have declared in another php page.

ie. i have the php page Bob.php and want to call the variable $name from show.php.

any help is appreciated.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

you could [php_man]include()[/php_man] it.
hibbard
Forum Newbie
Posts: 11
Joined: Sun Nov 02, 2003 1:15 pm

Post by hibbard »

the only problem is, if my prior page had printed any data from the variable i'm calling from before on the previous page, it prints that same information at the top of the page. any ideas?

in other words, let's say i have the following names:

Bob
James
Sally


The way this code is working, it defines the variable $fname as bob, then james, and then sally. Instead of creating an array that can be called later, it defines $fname as Sally ONLY because it's the last name to be used to define #fname.

So, everytime i select one of the names to bring up info on the next page with, it will ONLY give me the information to Sally, and non of the others. Also, it's posting all the names at the top, instead of only the information i was wanting to see from the specific user Sally...





here is the code i'm using :

list.php

Code: Select all

<html>
<head><title>Pick IP</title></head>
<body>


<?php

@ mysql_pconnect("localhost","username","password"); 

$db = mysql_select_db("people");

//* Get all records 
$sql = "SELECT * FROM info"; 

$sql_result = mysql_query($sql); 

while ($row = mysql_fetch_array($sql_result)) 

&#123; 
	$fname = $row&#1111;"fname"]; 
	echo "<a href="detail.php?name=$name&fname=$fname">$fname</a>"; 
	echo "<br />";
&#125;

mysql_free_result($sql_result);

?>

</body>
</html>



detail.php

Code: Select all

<html>
<head><title>Bob</title></head>
<body>

<?php
	include 'list.php';

	$sql = "SELECT * FROM info where fname = '$fname'"; 
	$sql_result = mysql_query($sql); 


	while ($row = mysql_fetch_array($sql_result)) 

	&#123; 
    		$detail = $row&#1111;"address"]; 
    		$detail1 = $row&#1111;"phone"]; 
    		echo "<br /> Detail : $detail <br /> Other Detail : $detail1 <br />"; 

&#125;

mysql_free_result($sql_result);

?> 
</body>
</html>



However, there is a problem with this code. When you click one of the names, it WILL display the information you are wanting, however, the names stay at the top of the page ( meaning, not only does the name you want to view stay at the top, but ALL the first names in your database ). Also, should you try to select another name, the values do not change from the first name's values.

in other words, it doesn't clear the array. what am i doing wrong?



***********edit 3 ***************

i think the problem is that the variable $fname is being reassigned by each new name, and the very last name is the final value of it, making every link's details that is generated equal to only the last record value of fname... :( help me :P
Last edited by hibbard on Sun Nov 02, 2003 10:26 pm, edited 1 time in total.
hibbard
Forum Newbie
Posts: 11
Joined: Sun Nov 02, 2003 1:15 pm

Post by hibbard »

if anyone could help me out with this, i'd be eternally grateful... 8O
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

btw, this is me. sorry, had to wait for my password to be resent...

if anyone can help me out with this, i'd really appreciate it 8O
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This is very similar to another thread, so I'm locking this one and sending all you helpful people to the other.

viewtopic.php?t=14226

Mac
Locked