Perad Posted October 28, 2006 Share Posted October 28, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/25409-failing-to-declare-variable/ Share on other sites More sharing options...
Janus13 Posted October 28, 2006 Share Posted October 28, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/25409-failing-to-declare-variable/#findComment-115929 Share on other sites More sharing options...
wildteen88 Posted October 28, 2006 Share Posted October 28, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/25409-failing-to-declare-variable/#findComment-115953 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.