Jump to content

Failing to declare variable?


Perad

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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.