Page 1 of 1

"Intelligent" Navigation Bar

Posted: Mon Jun 18, 2007 12:08 pm
by ReverendDexter
Howdy,

I was wondering if there is a standard, best-practice way of making a (half-way) intelligent navigation bar. Right now I have 3 "levels" of user (not logged in, logged in as user, logged in as admin) that should see some of the same things, and some very different things. Also, I'd like the nav bar to be halfway responsive to which page the user is on, and not showing the page the user is currently at. On top of that, I'd like the nav bar to have a certain order of elements, which interjects elements of the three user levels (i.e. the elements are not in the order : Unlogged elements, Logged elements, Admin elements)

Right now, I have a cluster of if and echo statements that doesn't look very pretty to me. I'm in the middle of reorganizing it again; I'll post the code for critique once I'm done if no one has a better idea in the next 10 minutes or so ;)

Code

Posted: Mon Jun 18, 2007 12:28 pm
by ReverendDexter
Okay, no one had a response by the time I got this done, so here's the code I have thus far:

Code: Select all

function temp_nav_bar($page)
{
	//open the table..
	echo "<table class = temp_nav_bar>
		<tbody>
		<tr>";
	//echo "<td>".whoamI()."</td>"; //uncomment this line if having db login/permission issues
	
	//Show the username if logged in
	if (isset($_SESSION['USERID']) && ($page != "Logout"))
		echo "<td class = userid>".$_SESSION['USERID']."</td>";

	//EVERYONE
	if ($page != "Job List")
	echo '<td><a href="index.php">Job List</a></td>';

	//Not logged in
	if (!isset($_SESSION['USERID']) || ($page == "Logout"))
		echo '<td><a href="login.php">Log in</a></td>';

	//Logged in
	elseif ($_SESSION['USERID'])
	{
		//Logged in as Admin
		if (($_SESSION['USERID'] == "Admin") && $page != "Company and Post Administration")	
			echo '<td><a href="administration.php">Administration</a></td>';
		
		//logged in NOT as admin
		if (($_SESSION['USERID'] != "Admin") && $page != "Edit Contact Info")
			echo '<td><a href="contactedit.php">Edit Contact Info</a></td>';

		if (($_SESSION['USERID'] != "Admin") && $page != "Post New Jobs")
			echo "<td><a href=post_job.php>Post Job</a></td>";

		//everyone logged in 
		if ($page != "Logout")
			echo "<td><a href=\"logout.php\">Log out</a></td>";
	}

	//close the table	
	echo "</tr>
		</tbody>
		</table>";
}
Like I said, is there a better/easier/more managable way of doing this?

Posted: Mon Jun 18, 2007 12:30 pm
by RobertGonzalez
In cases like this you can set a group identifier in the pages table of the database that tells the app which to show under which circumstances. This can be done in a single method usually, and the creation of the navigation can also be handled in a single method usually.

Basically you would need to know you current page and the level of the user to pass to the query and you should be golden.

Posted: Mon Jun 18, 2007 12:33 pm
by ReverendDexter
Everah wrote:In cases like this you can set a group identifier in the pages table of the database ...
Pages table in database? That's an entirely foreign concept to me, but it sounds like it would be*much* cleaner that what I'm doing! Is there someplace I can go to read up on that?

Posted: Mon Jun 18, 2007 12:50 pm
by RobertGonzalez
Have a look at content management concepts. Essentially you store all of your page content in a database. When a page is requested, you serve it from the database. That page can hold lots of information, including who should see it (this isn't necessarily in the page content table, but it does relate to it so it can be anywhere in the database). Then, when the page is called, show it if it is to be shown.

Obviously, this same concept can be applied to the navigation as the pages you want your users to visit are the same ones you want your users to see. So, given that all of your pages are in the content table, select them all (based on criteria) and create a navigation from what you know they should have access to based on their profile and the pages in the page_content table.

For now, for what you got going on, I would look at something a little more simplified...

Code: Select all

<?php
function buildNavigation($page)
{
    $return = '';

    //open the table..
    $return .= '<table class="temp_nav_bar">
                <tbody>
                <tr>';

    if (isset($_SESSION['USERID']))
    {
        // User is logged in
        $return .= '<td class="userid">' . $_SESSION['USERID'] . '</td>';

        // Show a logout link if not on the logout page
        if ($page != 'Logout')
        {
            $return .= '<td><a href="logout.php">Log out</a></td>';
        }

        /**
         * I would seriously consider using the actual link name instead 
         * of the link text name, as it would be a lot easier for comparison
         */
		
        //EVERYONE
        if ($page != "Job List")
        {
            $return .= '<td><a href="index.php">Job List</a></td>';
        }
        
        // Are we an admin?
        if ($_SESSION['USERID'] == 'admin')
        {
            // yes we are
            if ($page != 'Company and Post Administration')
            {
                $return .= '<td><a href="administration.php">Administration</a></td>';
            }

            if ($page != 'Edit Contact Info')
            {
                $return .= '<td><a href="contactedit.php">Edit Contact Info</a></td>';
            }

            if ($page != "Post New Jobs")
            {
                $return .= '<td><a href="post_job.php">Post Job</a></td>';
            }
        }
    }
    else
    {
        $return .= '<td><a href="login.php">Log in</a></td>';
    }

    //close the table       
    $return .= '</tr>
                </tbody>
                </table>';
    return $return;
}
?>

Posted: Mon Jun 18, 2007 1:02 pm
by ReverendDexter
Just for clarification, they don't need to be admin to post a job, but I think I see what you're getting at. Thanks for the pointers, everytime I sit down I'm realizing just how amateur I am when it comes to doing this level of development!

For this project, I think I'm gonna stick with what I have, and implement something more along what you were saying on my next.

Thanks!

Posted: Mon Jun 18, 2007 1:03 pm
by RobertGonzalez
Sorry, I got != 'Admin' and == 'Admin' confused with one another.