Arrays with if statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Arrays with if statements

Post by Pazuzu156 »

In my CMS site, there is an admin section of the site. I am an admin, but I want more than one username to be an admin, how would i do this by adding multiple users into an array for administrator based on if they are set to admin in the database.

here is what I have as far as setting admin status:

Code: Select all

if($_SESSION['user']=='Administrator') {
	echo '<p align="center">| <a href="admin/">Admin</a> |</p>';
}
I want to keep that structure of the admin link based on if the user logged in is an admin, but using an array instead of just the username itself.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
amargharat
Forum Commoner
Posts: 82
Joined: Wed Sep 16, 2009 2:43 am
Location: Mumbai, India
Contact:

Re: Arrays with if statements

Post by amargharat »

You can modify your script as follows;

Code: Select all

if(in_array($_SESSION['user'], $array_of_usernames)) {
        echo '<p align="center">| <a href="admin/">Admin</a> |</p>';
}
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: Arrays with if statements

Post by Pazuzu156 »

Thanks man. Works :)
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
Post Reply