Jump to content

Recommended Posts

Hey guys,

I tried to create a header file for my website to make it easier to edit the template.

It's a php file because I wanted the navigation menu to be dynamic. When you click on a navigation link, based on the link page that is calling the header.php file, the header should change the color of the navigation item. But it is not working for some reason.

Here's the structure of the navigation menu:

index.php

    2.php

    3.php

    20.php

4.php

    5.php

    6.php

    7.php

    8.php

9.php

    10.php

    11.php

    19.php

12.php

    13.php

    14.php

    15.php

16.php

17.php

    18.php

 

$main_nav contains the html for the main navigation bar.

and $sub_nav contains the html for the sub-navigation bar.

All the files above contain a variable $page which is a string of the page name.

if the class of a navigation link is main_nav_active_item or sub_nav_active_item, it should have a different color.

Everything else is simple conditional statements.

 

The Output of the current code:

1. All the parts of the header are showing up, but the main navigation is not dynamic;i.e., only Home has active color, regardless of which navigation link I click. But the sub navigation bar under Home is working perfectly.

2. When I click on other main navigation links, the page is showing up, but the sub navigation bar is not showing up.

The website is guesscricket.com...see the problem for urself.

 

There is no problem with the css files or anything because, before I used the header file, all the files were working fine.

 

 

Here's the code:

 

<?php

if ($page == "index.php" || "2.php" || "3.php" || "20.php") {

$main_nav = "<ul id=\"main_nav_list\">

  <li><a class=\"main_nav_active_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_item\" href=\"17.php\">About Us[/url]</li>

</ul>";

      if($page == "index.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"2.php\">Latest News[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"3.php\">Register[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"20.php\">Login[/url]</li>

  </ul>";

    } elseif($page == "2.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_active_item\" href=\"2.php\">Latest News[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"3.php\">Register[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"20.php\">Login[/url]</li>

  </ul>";

    } elseif($page == "3.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"2.php\">Latest News[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"3.php\">Register[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"20.php\">Login[/url]</li>

  </ul>";

    } elseif($page == "20.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"2.php\">Latest News[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"3.php\">Register[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"20.php\">Login[/url]</li>

  </ul>";

    }

}

elseif ($page == "4.php" || "5.php" || "6.php" || "7.php" || "8.php") {

$main_nav = "<ul id=\"main_nav_list\">

  <li><a class=\"main_nav_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_active_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_item\" href=\"17.php\">About

Us[/url]</li>

</ul>";

      if($page == "4.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"5.php\">Fixtures[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"6.php\">Rankings[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"7.php\">Tests[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"8.php\">ODIs[/url]</li>

  </ul>";

    } elseif($page == "5.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_active_item\" href=\"5.php\">Fixtures[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"6.php\">Rankings[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"7.php\">Tests[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"8.php\">ODIs[/url]</li>

  </ul>";

    } elseif($page == "6.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"5.php\">Fixtures[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"6.php\">Rankings[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"7.php\">Tests[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"8.php\">ODIs[/url]</li>

  </ul>";

    } elseif($page == "7.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"5.php\">Fixtures[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"6.php\">Rankings[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"7.php\">Tests[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"8.php\">ODIs[/url]</li>

  </ul>";

    } elseif($page == "8.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"5.php\">Fixtures[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"6.php\">Rankings[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"7.php\">Tests[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"8.php\">ODIs[/url]</li>

  </ul>";

    }}

elseif ($page == "9.php" || "10.php" || "11.php" || "19.php") {

$main_nav = "<ul id=\"main_nav_list\">

  <li><a class=\"main_nav_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_active_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_item\" href=\"17.php\">About Us[/url]</li>

</ul>";

      if($page == "9.php"){

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"10.php\">Control Panel[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"11.php\">Savings Market[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"19.php\">Betting Market[/url]</li>

  </ul>";

    } elseif($page == "10.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_active_item\" href=\"10.php\">Control Panel[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"11.php\">Savings Market[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"19.php\">Betting Market[/url]</li>

  </ul>";

    } elseif($page == "11.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"10.php\">Control Panel[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"11.php\">Savings Market[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"19.php\">Betting Market[/url]</li>

  </ul>";

    } elseif($page == "19.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"10.php\">Control Panel[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"11.php\">Savings Market[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"19.php\">Betting Market[/url]</li>

  </ul>";

    }

}

elseif ($page == "12.php" || "13.php" || "14.php" || "15.php") {

$main_nav = " <ul id=\"main_nav_list\">

  <li><a class=\"main_nav_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_active_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_item\" href=\"17.php\">About Us[/url]</li>

</ul>";

      if ($page == "12.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"13.php\">Live Matches[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"14.php\">Video Highlights[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"15.php\">Games[/url]</li>

  </ul>";

    } elseif($page == "13.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_active_item\" href=\"13.php\">Live Matches[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"14.php\">Video Highlights[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"15.php\">Games[/url]</li>

  </ul>";

    } elseif($page == "14.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"13.php\">Live Matches[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"14.php\">Video Highlights[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"15.php\">Games[/url]</li>

  </ul>";

    } elseif($page == "15.php") {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"13.php\">Live Matches[/url]</li>

        <li><a class=\"sub_nav_item\" href=\"14.php\">Video Highlights[/url]</li>

        <li><a class=\"sub_nav_active_item\" href=\"15.php\">Games[/url]</li>

  </ul>";

    }

}

elseif ($page == "16.php") {

$main_nav = " <ul id=\"main_nav_list\">

  <li><a class=\"main_nav_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_active_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_item\" href=\"17.php\">About Us[/url]</li>

</ul>";

}

elseif ($page == "17.php" || "18.php") {

$main_nav = " <ul id=\"main_nav_list\">

  <li><a class=\"main_nav_item\" href=\"index.php\">Home[/url]</li>

  <li><a class=\"main_nav_item\" href=\"4.php\">Statistics[/url]</li>

  <li><a class=\"main_nav_item\" href=\"9.php\">GUESS[/url]</li>

  <li><a class=\"main_nav_item\" href=\"12.php\">Links[/url]</li>

  <li><a class=\"main_nav_item\" href=\"16.php\">FAQs[/url]</li>

  <li><a class=\"main_nav_active_item\" href=\"17.php\">About Us[/url]</li>

</ul>";

      if($page == "17.php")

      {

      $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"18.php\">Feedback[/url]</li>

  </ul>";

    }

    elseif($page == "18.php")

    {

  $sub_nav = "<ul class=\"sub_nav_list\">

        <li><a class=\"sub_nav_item\" href=\"18.php\">Feedback[/url]</li>

  </ul>";

    }

}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

  <meta http-equiv="Content-Type"

content="text/html; charset=UTF-8">

  <meta name="author" content="1&1 WebsiteBuilder">

  <meta name="description" content="A WebsiteBuilder Website">

  <title><?php echo $page_title;

?></title>

  <link rel="stylesheet" type="text/css" media="screen"

href="main.css">

  <link rel="stylesheet" type="text/css" media="screen"

href="colorschemes/colorscheme1/colorscheme.css">

  <link rel="stylesheet" type="text/css" media="screen"

href="style.css">

  <script type="text/javascript" src="live_tinc.js"></script>

</head>

<body id="main_div">

<div id="container">

<div id="header">

<div id="main_nav_container">

<?php

echo $main_nav;

?>

</div>

<div style="top: 50px; height: 274px;" id="key_visual">

</div>

<div id="logo">  </div>

</div>

<div id="main_container">

<div id="left_column_container">

<div class="sub_nav_container">

<?php

echo $sub_nav;

?>

</div>

<div id="sub_container1"></div>

</div>

<div id="sub_container2">

<div id="content_container" class="content">

<?php

echo $page;

?>

 

 

 

I tried changing all the main navigation "elseif"s to only "if"s...what happens? weirder things....only 17.php is active, but all the subnavigation bars are working.

 

The website is guesscricket.com...look at it if u want to get a better idea of the main/sub navigation bars and the problem.

 

Will be really helpful if someone can point out the mistake. Or I would appreciate it even more if someone can mention a better way of achieving what I am trying to achieve.

 

Thanks guys...Will be waiting for ur reply.

 

Just as a note...I am still a novice.

That is so horribly complex!

 

I suggest you seperate out the lists and the "set active" components of your code.

 

Like this:

 

$topmenu = array(
  '4.php' => 'Statistics',
  '9.php' => 'GUESS',
);

$html = make_active($topmenu, '4.php');
print $html;

function make_active($menu, $active) {
  foreach ($menu as $p => $t) {
    if ($p == $active) {
      print "<li><a class=\"main_nav_active_item\" href=\"$p\">$t[/url]</li>";
    } else {
      print "<li><a class=\"main_nav_item\" href=\"$p\">$t[/url]</li>";
    }
  }
}

 

Does that make sense from the example alone?  The idea is to make it so you only need to put each of your menus once in your code, and then you generate the version of the menu with a specific active item (the $active argument to make_active()).

Hey btherl,

Thanks for the reply. Tried what u said, and seems like a pretty good idea. But I am getting an error...

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\GUESSCRICKET\header.inc.php on line 11

 

function make_sub_active($menu, $pag) {

foreach ($menu as $p => $n) {

if($p == $pag) {

//The line below is line 11

      echo "<li><a class=\"sub_nav_active_item\" href=\"$p\">$n[/url]</li>";

} else {

        echo "<li><a class=\"sub_nav_item\" href=\"$p\">$n[/url]</li>";

        }

  }

}

 

If u could tell me what is the problem, I will go ahead and implement it in its entirety.

Once again...thanks bro.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.