Page 1 of 1

How to call a variable from a different php page?

Posted: Sun Nov 02, 2003 2:13 pm
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.

Posted: Sun Nov 02, 2003 3:33 pm
by DuFF
you could [php_man]include()[/php_man] it.

Posted: Sun Nov 02, 2003 6:06 pm
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

Posted: Sun Nov 02, 2003 9:27 pm
by hibbard
if anyone could help me out with this, i'd be eternally grateful... 8O

Posted: Sun Nov 02, 2003 10:30 pm
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

Posted: Mon Nov 03, 2003 3:59 am
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