variable scope problem

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

Post Reply
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

variable scope problem

Post by ashrafzia »

I want the value of the variable $user to go inside the if statement inside the query, but its not working.
How can i make the value of the variable $user as a global for access inside if....else block statements.

Code: Select all

$val = $_GET['id'];
$user = $_GET['user'];

if ($val == "AddPaper" && !isset($_POST['next_x'])){

//echo "$user";

$sql="SELECT programe_name FROM teachers WHERE teacher_name='$user' ";

$result = mysql_query($sql, $conn) or die ("Can't Process Query".mysql_query());
while ($row=mysql_fetch_array($result)){
		$add1 .= "<option value='$row[programe_name]'>$row[programe_name]</option>";
}

I have a dropdown menu which has a link as http://localhost/xampp/......../myfile?id=AddPaper
when i click the button the above if statement is executed but the value of the variable $user isn't coming inside it and before the button click i have a value inside the link as http://localhost/xampp.......myfile?user=$_post[name].

I hope you get what i want to say.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

if blocks don't maintain their own scope like functions do. $user will be visible inside that block. Check that the if conditions are evaluating to true.
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

aaronhall wrote:if blocks don't maintain their own scope like functions do. $user will be visible inside that block. Check that the if conditions are evaluating to true.
I have checked the if condition are evaluating to true but the value of the variable isn't printing.
I have echoed it. Above if statement its working, but inside if statement its not working.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

aaronhall wrote:Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true
It's giving a parse error and will you kindly explain what is it used for ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

ashrafzia wrote:
aaronhall wrote:Place this line directly above the if statement:

Code: Select all

var_dump($val == "AddPaper" && !isset($_POST['next_x']);
If it doesn't print true, the statement isn't true
It's giving a parse error and will you kindly explain what is it used for ?
It's missing a closing bracket.

Try var_dump()'ing all the variables separately to get a better picture.

Code: Select all

echo 'Val: '; var_dump($val); echo '<br />';
echo '$_POST[\'next_x\']: '; var_dump($_POST['next_x']); echo '<br />';
echo 'if(): '; var_dump($val == "AddPaper" && !isset($_POST['next_x']));
ashrafzia
Forum Commoner
Posts: 37
Joined: Wed Sep 28, 2005 12:23 pm

Post by ashrafzia »

Jcart wrote:
It's missing a closing bracket.

Try var_dump()'ing all the variables separately to get a better picture.

Code: Select all

echo 'Val: '; var_dump($val); echo '<br />';
echo '$_POST[\'next_x\']: '; var_dump($_POST['next_x']); echo '<br />';
echo 'if(): '; var_dump($val == "AddPaper" && !isset($_POST['next_x']));
Its giving the following output:

Code: Select all

Val: NULL 
$_POST['next_x']: NULL 
if(): bool(false)
Post Reply