template menu problem ...

Discussion in 'PHP' started by pepe_lepew1962, Nov 10, 2013.

  1. #1
    Hello:
    I am trying to create templates for my files and I am having a problem with getting the menu to work properly. I have 3 template files, head.php, main.php and foot.php. The head.php contains the following menu html:
    <li><a href="page1.php" class="<?php echo "$head001";?>">One</a></li>
      <li><a href="page2.php" class="<?php echo "$head002";?>">Two</a></li>
      <li><a href="page3.php" class="<?php echo "$head003";?>">Three</a></li>
      <li><a href="page4.php" class=<?php echo "$head004";?>>Four</a></li>
      <li><a href="page5.php" class="<?php echo "$head005";?>">Five</a></li>
    HTML:
    In the main.php, I would like to have 1 variable for what the status of the menu is for that page. For example when the person is on page 3, the class should be current.

    $head003 = "Current";
    The variable current is in my external css file and changes the appearance. Simple, but for some reason, the variable is not being carrier over. Can you please help.
     
    pepe_lepew1962, Nov 10, 2013 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    So main.php looks something like this
    
    <?php
    $head001 = $head002 = $head003 = $head004 = $head005 = '';
    $currentpage = $_SERVER['REQUEST_URI'];
    $pagenum = intval(str_replace(array('/page','.php'),'',$currentpage);
    $head00{$pagenum} = 'Current';
    include 'head.php';
    echo 'page stuff';
    include 'foot.php';
    ?>
    PHP:
    If so, then that should work.

    Have you tried just echo'ing out the variable as you go through parts of your script to check to see that it's not being changed... and if it is you then have a clue for where to look!

    FYI: the quote marks on the page4 class aren't in the right place.

    BTW: why aren't you using a proper CMS like WordPress which gives you all of this as standard and lets you get on with creating quality content?
     
    sarahk, Nov 10, 2013 IP
  3. senecacollege

    senecacollege Well-Known Member

    Messages:
    458
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    add
    
    <?php
    $page='page1';
    ?>
    
    Code (markup):
    to page1.php
    add
    
    <?php
    $page='page2';
    ?>
    
    Code (markup):
    to page2.php
    add
    
    <?php
    $page='page3';
    ?>
    
    Code (markup):
    to page3.php
    add
    
    <?php
    $page='page4';
    ?>
    
    Code (markup):
    to page4.php
    add
    
    <?php
    $page='page5';
    ?>
    
    Code (markup):
    to page5.php

    then in header.php
    
    <li><a href="page1.php" class="<?php if($page=='page1') echo 'current';?>">One</a></li>
    <li><a href="page2.php" class="<?php if($page=='page2') echo 'current';?>">Two</a></li>
    <li><a href="page3.php" class="<?php if($page=='page3') echo 'current';?>">Three</a></li>
    <li><a href="page4.php" class="<?php if($page=='page4') echo 'current';?>">Four</a></li>
    <li><a href="page5.php" class="<?php if($page=='page5') echo 'current';?>">Five</a></li>
    
    Code (markup):
     
    senecacollege, Nov 11, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Jeez. Loops, people, loops.

    Do this:
    
    <?php
    $menu = array(1 => 'page1.php', 2 => 'page2.php', 3 => 'page3.php', 4 => 'page4.php', 5 => 'page5.php');
    foreach ($menu as $key => $value) {
    $current = ($value == basename(__FILE__)) ? 'class="current"' : '';
      echo '<li><a href="'.$value.'.php" '.$current.'>'.$value.'</a></li>';
    }
    ?>
    
    PHP:
     
    PoPSiCLe, Nov 12, 2013 IP