Jump to content
Larry Ullman's Book Forums

Recommended Posts

Question:

I have never created a menu system dynamically.  I can not figure out how to hide() the register tab if a user is logged in.  I am unsure where to start in the header file to do so.

 

Near the bottom of the code is where we decide if we should show the log in page within the index page.  I figure I would have to start there, or change the dynamic code to check for this first?  I'm such a newbie... LOL

 

Any help, or ideas would be most appreciated.

 

Thank you,

 

The Code:

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
 
    <title><?php // Use a default page title if one was not provided...
if (isset($page_title)) { 
echo $page_title; 
} else { 
echo 'Knowledge is Power: And It Pays to Know'; 
?></title>
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
 
    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
 
    <!-- Custom styles for this template -->
    <link href="css/sticky-footer-navbar.css" rel="stylesheet">
 
  </head>
  <body>
 
    <!-- Wrap all page content here -->
    <div id="wrap">
 
      <!-- Fixed navbar -->
      <div class="navbar navbar-fixed-top">
        <div class="container">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.php">Knowledge is Power</a>
          <div class="nav-collapse collapse">
            <ul class="nav navbar-nav">
            
<?php // Dynamically create header menus...
 
 
// Array of labels and pages (without extensions):
$pages = array (
'Home' => 'index.php',
'About' => '#',
'Contact' => '#',
'Register' => 'register.php' 
);
 
// The page being viewed:
$this_page = basename($_SERVER['PHP_SELF']);
 
// Create each menu item:
foreach ($pages as $k => $v) {
 
// Start the item:
echo '<li';
 
// Add the class if it's the current page:
if ($this_page == $v) echo ' class="active"';
 
// Complete the item:
echo '><a href="' . $v . '">' . $k . '</a></li>
';
 
} // End of FOREACH loop.
 
// Show the user options:
if (isset($_SESSION['user_id'])) {
 
// Show basic user options:
// Includes references to some bonus material discussed in Part Four!
echo '<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Account <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="logout.php">Logout</a></li>
<li><a href="renew.php">Renew</a></li>
<li><a href="change_password.php">Change Password</a></li>
<li><a href="favorites.php">Favorites</a></li>
<li><a href="recommendations.php">Recommendations</a></li>
</ul>
</li>';
 
// Show admin options, if appropriate:
if (isset($_SESSION['user_admin'])) {
echo '<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Admin <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="add_page.php">Add Page</a></li>
<li><a href="add_pdf.php">Add PDF</a></li>
<li><a href="#">Something else here</a></li>
</ul>
</li>';
}
 
} // user_id not set.
 
?>
            </ul>
          </div><!--/.nav-collapse -->
        </div><!--/container-->
      </div><!--/navbar-->
 
      <!-- Begin page content -->
      <div class="container">
 
<div class="row">
 
<div class="col-3">
<h3 class="text-success">Content</h3>
<div class="list-group">
<?php // Dynamically generate the content links:
$q = 'SELECT * FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);
while (list($id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)) {
echo '<a href="category.php?id=' . $id . '" class="list-group-item" title="' . $category . '">' . htmlspecialchars($category) . '
</a>';
}
?>
 <a href="pdfs.php" class="list-group-item" title="PDFs">PDF Guides
 </a>
</div><!--/list-group-->
 
<?php // Should we show the login form?
if (!isset($_SESSION['user_id'])) {
require('login_form.inc.php');
}
//I figure here is where I could add an (isset($_SESSION['user_id']. hide() or something.  I can't figure it out.
?>
</div><!--/col-3-->
 
 
 <div class="col-9">
<!-- CONTENT -->
  • 0
Link to post
Share on other sites

Oh shoot I did it this way.  Which is better practice?

 

<?php // Dynamically create header menus.
 
 
// Array of labels and pages (without extensions):
// Show or hide register.php link if user is logged in via session data:
if (!isset($_SESSION['user_id'])){
$pages = array (
'Home' => 'index.php',
'About' => '#',
'Contact' => '#',
'Register' => 'register.php'
);
}else{
$pages = array(
'Home' => 'index.php',
    'About' => '#',
'Contact' => '#'
    );
}    
Link to post
Share on other sites

I Think your way is easier and better, but I did not think of that.   I finally got it to work this way.  But it sure is a lot less code via your example.

 

Thank you very much for your reply.  It took me some time to figure this out.  I should keep the KISS method in mind from now on.

 

:-)

 

-eon-

Link to post
Share on other sites

A little confused, sorry...

 

After looking at the above closer I am thinking my way may be easier to add or subtract from the menu that isn't part of the drop down?  I use an 'if' to choose the array in which to dynamically create the main links (not via drop down menu).

 

Can you explain where you would add this code to my original?  I see only the drop down menu portion as a option. If I am not mistaken the original code is dynamically creating the links (main menu portion) from the $pages array?  I could add register to the drop down portion, but for usability I think I would like it more obvious?

 

I may not fully understand the suggestion. 

 

 

Link to post
Share on other sites

You can do it either way. I was providing my way more as an example than as actual code that you should implement as-is. (Sorry, I should have explicitly stated that.)

 

Anyway, if you want to do it your way with the array, but by applying the same principle, then what about the following:

$pages = array(
  'Home' => 'index.php',
  'About' => '#',
  'Contact' => '#'
);

if (!isset(SESSION['user_id'])) {
  $pages['Register'] = 'register.php';
}
 
This is essentially the same as what you recommended, but it offers more flexibility with any links you may want to add in the future and/or make conditional.
Also, you're welcome to question my code at any time. :)
 
Edit: As a side note, I wouldn't recommend using an associative array (i.e., string keys) when you care about the order of your items (which I'm assuming you do).
In all likelihood, PHP will retain the intended order of your nav links, but still, there's no reason PHP has to (after all, JavaScript does not in the same case), so it kind of scares me.
 
The $pages array may be better implemented as follows:
$pages = array(
  array(
    'label' => 'Home',
    'url' => 'index.php'
  ),
  array(
    'label' => 'About',
    'url' => '#'
  ),
  array(
    'label' => 'Contact',
    'url' => '#'
  )
);
 
The structure is a bit more complex, but not unnecessarily so, and you now have ultimate control and flexibility on how you present the nav via your data model.

Just my two cents.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...