Links generated by user/account type after login

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
nympymplee
Forum Newbie
Posts: 3
Joined: Sun Jan 01, 2012 9:28 am

Links generated by user/account type after login

Post by nympymplee »

As my site is set up now when the user is logged out they only see a login and register link in a drop down menu, however, when they login they see member links in the menu as shown below (condensed):

Code: Select all

<?php
session_start(); 
$toplinks = "";
if (isset($_SESSION['id'])) { 
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '<li><a href="members.php">Member Zone</a>
<ul>
<li><a href="upgrade.php">Upgrade Membership</a></li>
<li><a href="buy.php">Buy Credits</a></li>
</ul>
</li>';

} else {
$toplinks = '<li><a href="members.php">Members</a>
<ul>
<li><a href="register.php">Register</a></li>
<li><a href="login.php">Login</a></li>
</ul>
</li>';
}
?>
Pretty simple and does exactly what I want it to do but now onto the problem. I'm adding multiple account types that will display different links. ie. links for a basic member, links for a paying member, links for admin, etc... The links displayed in the drop down menu will depend on the account type currently logged in. Something like the following:

Code: Select all

if ($accounttype=="a"){
$toplinks = '<li>links 1</li>';
} else if ($accounttype=="b"){
$toplinks = '<li>links 2</li>';
} else if ($accounttype=="c"){
$toplinks = '<li>links 3</li>';
} else {
$toplinks = '<li>standard links</li>';
}
I'm having difficulty getting this to work the way I want it to. So how can I wrap this up in a neat package without making huge changes? I hope I've been clear. Please ask me if you need additional details.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Links generated by user/account type after login

Post by Celauran »

nympymplee wrote:I'm having difficulty getting this to work the way I want it to.
Can you elaborate on this? It looks like you've got the basic idea, so what isn't working? What is the expected result? What happens instead?
nympymplee
Forum Newbie
Posts: 3
Joined: Sun Jan 01, 2012 9:28 am

Re: Links generated by user/account type after login

Post by nympymplee »

Thanks for replying. I've gotten rid of most of the problems. The first major one was forgetting to query (?) the accounttype for the session when logging in (sorry if that doesn't make sense. I haven't bothered to learn the terminology.) Anyhow, here is the condensed code again in its current state. I've tested it with both an 'a' and 'b' accounttype and the links appear as they should. The only problem I'm having now is the links disappearing upon logging out; the 'else' statement doesn't appear to be working.

Code: Select all

<?php
session_start(); 
$toplinks = "";
if (isset($_SESSION['id'])) { 
    $userid = $_SESSION['id'];
    $username = $_SESSION['username'];
    $accounttype = $_SESSION['accounttype'];
	
    if ($accounttype=="a") {
    $toplinks = '<li><a href="members.php">Member Zone</a>
    <ul>
    <li><a href="#">Upgrade Membership</a></li>
    <li><a href="#">Buy Credits</a></li>
    </ul>
    </li>';
	
}   else if ($accounttype=="b") {
    $toplinks = '<li><a href="admin.php">Admin Zone</a>
    <ul>
    <li><a href="#">Admin Junk</a></li>
    <li><a href="#">Browse</a></li>
    </ul>
    </li>';

} else {
    $toplinks = '<li><a href="members.php">General Zone</a>
    <ul>
    <li><a href="register.php">Register</a></li>
    <li><a href="login.php">Login</a></li>
    </ul>
    </li>';
}
}
?>
nympymplee
Forum Newbie
Posts: 3
Joined: Sun Jan 01, 2012 9:28 am

Re: Links generated by user/account type after login

Post by nympymplee »

I'm an idiot. I just noticed the brace is in the wrong spot. Works fine now.
Post Reply