Jump to content

Data Array - Checking For Admin


Irresistable

Recommended Posts

I want to check

"if admin == 1 { display admin center link }"

Basically.. using that.. with the code below, comes up as..

 

if($admin == 1){

Admin Center
}

On the website page. (It's not recognising the php)

 

Here is my code for the Data Array

$data = array(
	'<p><a href="logout.php">Logout</a></p>
	 <p><a href="member.php?change=password">Change Password</a></p>
	 <p><a href="member.php?change=email">Change Email</a></p>
	 <p><a href="member.php?change=account">Close account</a></p>
	 if($admin == 1){
	 <p><a href="member.php?change=admin">Admin Center</a></p>
	 }',

	'<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
	<p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',

	);

Can you help me out. Thanks

Link to comment
Share on other sites

That's because you're including that in your string. How it could be done, using the ternary operator:

 

<?php
   $data = array(
      '<p><a href="logout.php">Logout</a></p>
       <p><a href="member.php?change=password">Change Password</a></p>
       <p><a href="member.php?change=email">Change Email</a></p>
       <p><a href="member.php?change=account">Close account</a></p>' .
       (($admin == 1) ? '<p><a href="member.php?change=admin">Admin Center</a></p>' : ''),
      
      '<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
      <p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>'
      
      );
?>

Link to comment
Share on other sites

Hey There,

 

Thebadbad is absolutely right!

 

if you have variables within single quotes ('), they are always interpreted *literally*

Use double quotes (") to have PHP interpret the variables.

 

Logic, though, should not be in quotes at all (like your "if" statement)

 

(($admin == 1) ? '<p><a href="member.php?change=admin">Admin Center</a></p>' : ''), will work but I generally try not to use it. (It acts like a shorthand if/else).

 

What i would do personally is append that data to the array value.

Ex.

 


$data = array(
      '<p><a href="logout.php">Logout</a></p>
       <p><a href="member.php?change=password">Change Password</a></p>
       <p><a href="member.php?change=email">Change Email</a></p>
       <p><a href="member.php?change=account">Close account</a></p>',
      
      '<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
      <p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>',
      );

if($admin == 1){
    $data[0] .= '<p><a href="member.php?change=admin">Admin Center</a></p>';
}

 

Just a personal Preference :)

 

Ryan

Link to comment
Share on other sites

Ok thanks guys. Ryan, I'll test out yours after this bit is fixed.. As i don't know if either would work and how it'd turn out like..

 

It doesn't actually get the Admin value.

If it does.. it retrieves it as "0" which means Not Admin. When it is actually 1 According to looking on the database.

 

Here is the full code. Config.php holds the database connection.

 

<?php session_start(); 

include 'config.php';
$p = mysql_query("SELECT Admin FROM Users WHERE Username = '$_SESSION[s_logged_n]' LIMIT 1") or die(mysql_error());
$admini = mysql_fetch_array($p);
$admin = $admini['Admin'];

if($_GET['set'] > 0)
{

if($_SESSION['whattoclose'] == NULL)
{

	$_SESSION['whattoclose'] = $_GET['set'];

} else {

	$_SESSION['whattoclose'] = $_SESSION['whattoclose'] . '|' . $_GET['set'];

}

}

if($_GET['undo'] > 0)
{

if(!empty($_SESSION['whattoclose'])){

	$_SESSION['whattoclose'] = str_replace("|".$_GET['undo'] , "" , $_SESSION['whattoclose']);
	$_SESSION['whattoclose'] = str_replace($_GET['undo'] , "" , $_SESSION['whattoclose']);

}

}

function showTitle($id)
{

$title = array(
	"Navigation",
	"Login information",
	);

$data = $_SESSION['whattoclose'];

$not = explode( "|" , $data );
if(!in_array( $id , $not ))
{

	echo '<a href="member.php?set='.$id.'">close</a> - ';
	echo $title[$id-1];

} else {

	echo '<a href="member.php?undo='.$id.'">open</a> - ';
	echo $title[$id-1];

}		

}

function checkIt($id)
{

   $data = array(
      '<p><a href="logout.php">Logout</a></p>
       <p><a href="member.php?change=password">Change Password</a></p>
       <p><a href="member.php?change=email">Change Email</a></p>
       <p><a href="member.php?change=account">Close account</a></p>' .
       (($admin == 1) ? '<p><a href="member.php?change=admin">Admin Center</a></p>' : ''),
      
      '<p>Welcome to the members only page '.$_SESSION['s_name'].'!</p>
      <p>You are logged in as '.$_SESSION['s_username'].' from the IP address '.$_SERVER['REMOTE_ADDR'].'</p>'
      
      );

$str = $_SESSION['whattoclose'];

$not = explode( "|" , $str );
if(!in_array( $id , $not ))
{

	echo '<div class="table">';
	echo $data[$id-1];
	echo '</div>';

}

}

?>

<div class="subheader">
<? showTitle(1) ?>
</div>
<? checkIt(1) ?>
<br>
<div class="subheader">
<? showTitle(2) ?>
</div>
<? checkIt(2) ?>

Link to comment
Share on other sites

That's because $admin isn't accessible inside your function (read up on variable scope). Simplest solution is to pass $admin to the function in question:

 

function checkIt($id, $admin)
//...

and when you run the function further down the script;

 

checkIt(1, $admin)

Also, you really shouldn't use the short opening PHP tags.

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.