Irresistable Posted November 13, 2009 Share Posted November 13, 2009 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 https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/ Share on other sites More sharing options...
thebadbad Posted November 13, 2009 Share Posted November 13, 2009 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 https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/#findComment-956893 Share on other sites More sharing options...
Irresistable Posted November 13, 2009 Author Share Posted November 13, 2009 yeah, I thought that I was including it in the string. Though wasn't sure how to include it otherwise.. Thanks for your help ! Link to comment https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/#findComment-956894 Share on other sites More sharing options...
rlelek Posted November 13, 2009 Share Posted November 13, 2009 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 https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/#findComment-956895 Share on other sites More sharing options...
Irresistable Posted November 13, 2009 Author Share Posted November 13, 2009 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 https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/#findComment-956898 Share on other sites More sharing options...
thebadbad Posted November 13, 2009 Share Posted November 13, 2009 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 https://forums.phpfreaks.com/topic/181399-data-array-checking-for-admin/#findComment-956948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.