Jump to content


Photo

Failing to declare variable?


  • Please log in to reply
2 replies to this topic

#1 Perad

Perad
  • Members
  • PipPipPip
  • Advanced Member
  • 287 posts

Posted 28 October 2006 - 04:37 PM

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?

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;
								}
							

												
		}

displayadminpanel();
								echo 'adminaccess = '. $adminaccess;


#2 Janus13

Janus13
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 28 October 2006 - 04:55 PM

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.

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;
								}
							

												
		}

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.

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;
}

Hope that helps!

Jonathon

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 October 2006 - 05:51 PM

Another option is to define $adminaccess as global. Change
global $dbc, $user_id;
to
global $dbc, $user_id, $adminaccess;

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users