Jump to content

Archived

This topic is now archived and is closed to further replies.

Perad

Failing to declare variable?

Recommended Posts

My eyes hurt, i am tired and frustrated.

Why isn't this variable being declared!?

Displayadminpanel(); works because it is showing and hiding admin link based on user group. Why isn't it also declaring the variable?

[code] function displayadminpanel () {

global $dbc, $user_id;
$adminaccess = 0;
$query = "SELECT user_id=$user_id FROM unc_user_group WHERE group_id=2;";
$result = mysql_query($query);
$row = (mysql_fetch_assoc ($result));
if (($row['user_id='.$user_id])==1) {
$adminaccess = 1;
echo '<a href="admin.php">Admin Control Panel</a>';
} else {
echo 'You cannot access this';
$adminaccess = 0;
}



}[/code]

[code] displayadminpanel();
echo 'adminaccess = '. $adminaccess;[/code]

Share this post


Link to post
Share on other sites
It is but if you don't return it from the function then you can't display it outside of it.  If you want to see it set, then do an echo of the variable inside the function, or return $adminaccess from the function.  But if it is displaying the link to only admins then you know it's working already.

[code]
function displayadminpanel () {

global $dbc, $user_id;
$adminaccess = 0;
$query = "SELECT user_id=$user_id FROM unc_user_group WHERE group_id=2;";
$result = mysql_query($query);
$row = (mysql_fetch_assoc ($result));
if (($row['user_id='.$user_id])==1) {
$adminaccess = 1;
echo '<a href="admin.php">Admin Control Panel</a>: $adminaccess is set to ' . $adminaccess ;
} else {
echo 'You cannot access this, $adminaccess is set to ' . $adminaccess;
$adminaccess = 0;
}



}
[/code]

or if you want to return it you can put just outside the last if...else a return $adminaccess and it will return that variable to the calling page.

[code]
function displayadminpanel ()
{
global $dbc, $user_id;
$adminaccess = 0;
        $query = "SELECT user_id=$user_id FROM unc_user_group WHERE group_id=2;";
$result = mysql_query($query);
$row = (mysql_fetch_assoc ($result));
if (($row['user_id='.$user_id])==1) {
$adminaccess = 1;
        echo '<a href="admin.php">Admin Control Panel</a>';
} else {
echo 'You cannot access this';
$adminaccess = 0;                                                                       
}
return $adminaccess;
}
[/code]

Hope that helps!

Jonathon

Share this post


Link to post
Share on other sites
Another option is to define $adminaccess as global. Change [code=php:0]global $dbc, $user_id;[/code] to [code=php:0]global $dbc, $user_id, $adminaccess;[/code]

Now will be able to echo $adminaccess after you have called the displayamdinpanel function.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.