Help coding a navigation bar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jdub

    Help coding a navigation bar

    I got tired of constantly changing the navigation bar on our numerous
    top-level pages, so I finally used an include of an html-only file. Here's
    a snippet:

    <div class="navleft" >
    <a href="home.php" >Home</a>
    <div class="navspace r"></div>
    <a href="newitems. php"><b>New Items!</b></a>
    <div class="navspace r"></div>
    <a href="items1.ph p">items 1</a>
    <a href="items2.ph p">items 2</a>
    <a href="items3.ph p">items 3</a>
    <div class="navspace r"></div>
    <a href="blah blah">blah blah</a>
    <a href="contact.p hp"><b>Contac t Us!</b></a>
    </div>

    Now I want to modify the nav bar so that the active page is not a clickable
    link and appears differently. E.g., when the items 1 page is active, the
    nav bar should have
    <div id="activeselec tion">items 1</div>
    instead of the href line.

    I can turn the include file into php code and have a whole mess of
    if-then-else combos for each nav bar entry, but that's pretty messy. So php
    gurus, how can I best code a single include file?

    Thanks.

    jay dub

  • Micha³ Wo¼niak

    #2
    Re: Help coding a navigation bar

    [color=blue]
    > I can turn the include file into php code and have a whole mess of
    > if-then-else combos for each nav bar entry, but that's pretty messy. So
    > php gurus, how can I best code a single include file?[/color]

    str_replace/preg_replace, I suppose. :)

    You can simply parse your HTML file and replace the anchors to the active
    page with pure text or whatever you wish. But don't ask me about
    regexps... ;)

    Cheers
    Mike

    Comment

    • NC

      #3
      Re: Help coding a navigation bar

      jdub wrote:[color=blue]
      >
      > I got tired of constantly changing the navigation bar on our numerous
      > top-level pages, so I finally used an include of an html-only file.
      > Here's a snippet:
      >
      > <div class="navleft" >
      > <a href="home.php" >Home</a>
      > <div class="navspace r"></div>
      > <a href="newitems. php"><b>New Items!</b></a>
      > <div class="navspace r"></div>
      > <a href="items1.ph p">items 1</a>
      > <a href="items2.ph p">items 2</a>
      > <a href="items3.ph p">items 3</a>
      > <div class="navspace r"></div>
      > <a href="blah blah">blah blah</a>
      > <a href="contact.p hp"><b>Contac t Us!</b></a>
      > </div>
      >
      > Now I want to modify the nav bar so that the active page is not
      > a clickable link and appears differently. E.g., when the items
      > 1 page is active, the nav bar should have
      > <div id="activeselec tion">items 1</div>
      > instead of the href line.
      >
      > I can turn the include file into php code and have a whole mess of
      > if-then-else combos for each nav bar entry, but that's pretty messy.
      > So php gurus, how can I best code a single include file?[/color]

      I think you don't even need HTML. Let's say you have this tab-
      delimited text file called 'menu.txt':

      home.php Home

      newitems.php <b>New Items!</b>

      items1.php items 1
      items2.php items 2
      items3.php items 3

      blah blah blah blah
      contact.php <b>Contact Us!</b>

      An empty line between items means that there should be a spacer
      between those items.

      Now you can do something like this:

      $running = strtolower(path info($_SERVER['PHP_SELF'],
      PATHINFO_BASENA ME));
      $menu = file('menu.txt' );
      foreach ($menu as $item) {
      $item = trim($item);
      if ($item) == '') {
      echo "<div class=\"navspac er\"></div>\r\n";
      } else {
      list($url, $text) = explode("\t", $item);
      if (strtolower(tri m($url)) == $running) {
      echo $text;
      } else {
      echo "<a href=\"$url\">$ text</a>";
      }
      }
      }

      You can wrap this code into a function and place that function into
      a file that is included by all pages on your site.

      Cheers,
      NC

      Comment

      • R. Rajesh Jeba Anbiah

        #4
        Re: Help coding a navigation bar

        jdub wrote:[color=blue]
        > I got tired of constantly changing the navigation bar on our numerous
        > top-level pages, so I finally used an include of an html-only file.[/color]
        Here's[color=blue]
        > a snippet:
        >
        > <div class="navleft" >
        > <a href="home.php" >Home</a>
        > <div class="navspace r"></div>
        > <a href="newitems. php"><b>New Items!</b></a>
        > <div class="navspace r"></div>
        > <a href="items1.ph p">items 1</a>
        > <a href="items2.ph p">items 2</a>
        > <a href="items3.ph p">items 3</a>
        > <div class="navspace r"></div>
        > <a href="blah blah">blah blah</a>
        > <a href="contact.p hp"><b>Contac t Us!</b></a>
        > </div>
        >
        > Now I want to modify the nav bar so that the active page is not a[/color]
        clickable[color=blue]
        > link and appears differently.[/color]
        <snip>

        If you don't pick the items from the DB---I mean, if you already know
        the list items, you can put it in HTML and use the famous CSS tab
        technique <http://www.sitepoint.c om/print/css-anthology-tips-tricks-4>
        (Figure 4.20) and minor PHP to achieve what you wanted.

        --
        <?php echo 'Just another PHP saint'; ?>
        Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

        Comment

        • jdub

          #5
          Re: Help coding a navigation bar

          On 31 Mar 2005 13:22:09 -0800, "NC" <nc@iname.com > wrote:
          [color=blue]
          >I think you don't even need HTML. Let's say you have this tab-
          >delimited text file called 'menu.txt':
          >
          >home.php Home
          >
          >newitems.php <b>New Items!</b>
          >
          >items1.php items 1
          >items2.php items 2
          >items3.php items 3
          >
          >blah blah blah blah
          >contact.php <b>Contact Us!</b>
          >
          >An empty line between items means that there should be a spacer
          >between those items.
          >
          >Now you can do something like this:
          >
          >$running = strtolower(path info($_SERVER['PHP_SELF'],PATHINFO_BASEN AME));
          >$menu = file('menu.txt' );
          >foreach ($menu as $item) {
          > $item = trim($item);
          > if ($item) == '') {
          > echo "<div class=\"navspac er\"></div>\r\n";
          > } else {
          > list($url, $text) = explode("\t", $item);
          > if (strtolower(tri m($url)) == $running) {
          > echo $text;
          > } else {
          > echo "<a href=\"$url\">$ text</a>";
          > }
          > }
          >}[/color]

          Yeah, I think that will work just fine. I'm off to try it out.

          Thanks!

          Comment

          • jdub

            #6
            Re: Help coding a navigation bar

            On 31 Mar 2005 22:22:08 -0800, "R. Rajesh Jeba Anbiah"
            <ng4rrjanbiah@r ediffmail.com> wrote:

            (snip)
            [color=blue]
            > If you don't pick the items from the DB---I mean, if you already know
            >the list items, you can put it in HTML and use the famous CSS tab
            >technique <http://www.sitepoint.c om/print/css-anthology-tips-tricks-4>
            >(Figure 4.20) and minor PHP to achieve what you wanted.[/color]

            Thanks for that reference. I'm already doing much of this, but there are
            some ideas that I hadn't considered.

            jdub

            Comment

            • jdub

              #7
              Re: Help coding a navigation bar

              On Mon, 04 Apr 2005 14:49:43 GMT, jdub <jdub@doesntlik espam.net> wrote:
              [color=blue]
              >On 31 Mar 2005 22:22:08 -0800, "R. Rajesh Jeba Anbiah"
              ><ng4rrjanbiah@ rediffmail.com> wrote:
              >
              >(snip)
              >[color=green]
              >> If you don't pick the items from the DB---I mean, if you already know
              >>the list items, you can put it in HTML and use the famous CSS tab
              >>technique <http://www.sitepoint.c om/print/css-anthology-tips-tricks-4>
              >>(Figure 4.20) and minor PHP to achieve what you wanted.[/color]
              >
              >Thanks for that reference. I'm already doing much of this, but there are
              >some ideas that I hadn't considered.
              >
              >jdub[/color]

              Followup.

              This technique is working great.

              In the HTML:

              items1.php has <BODY id="items1">

              In the navbar include file:

              <A class="items1" HREF="items1.ph p">items1</A>

              In the css:

              body#items1 a.items1 {
              background-color: #FFF;
              color: #777;
              }

              etc.

              Again, thanks for the reference!

              Comment

              • Geoff Berrow

                #8
                Re: Help coding a navigation bar

                I noticed that Message-ID: <lnko411jjbms05 55rgrdpmmd7lrrt ca47j@4ax.com>
                from jdub contained the following:
                [color=blue]
                >I can turn the include file into php code and have a whole mess of
                >if-then-else combos for each nav bar entry, but that's pretty messy. So php
                >gurus, how can I best code a single include file?[/color]

                Use my menuitem function

                define("LINK_CL ASS","");
                function menuitem($link, $text){
                $url=$_SERVER['PHP_SELF'];
                if(strpos($url, $link)===false) {
                $lnk="<a class= \"LINK_CLASS
                \"href=\"$link\ ">$text</a>";
                }
                else{
                $lnk=$text;
                }
                print $lnk;
                }


                Then just call the function wherever you want it
                e.g. <?php menuitem("index .php","Home"); ?><?php
                menuitem("page1 .php","Page 1"); ?> etc.
                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • R. Rajesh Jeba Anbiah

                  #9
                  Re: Help coding a navigation bar

                  Geoff Berrow wrote:[color=blue]
                  > I noticed that Message-ID:[/color]
                  <lnko411jjbms05 55rgrdpmmd7lrrt ca47j@4ax.com>[color=blue]
                  > from jdub contained the following:
                  >[color=green]
                  > >I can turn the include file into php code and have a whole mess of
                  > >if-then-else combos for each nav bar entry, but that's pretty messy.[/color][/color]
                  So php[color=blue][color=green]
                  > >gurus, how can I best code a single include file?[/color]
                  >
                  > Use my menuitem function
                  >
                  > define("LINK_CL ASS","");
                  > function menuitem($link, $text){
                  > $url=$_SERVER['PHP_SELF'];
                  > if(strpos($url, $link)===false) {
                  > $lnk="<a class= \"LINK_CLASS
                  > \"href=\"$link\ ">$text</a>";
                  > }
                  > else{
                  > $lnk=$text;
                  > }
                  > print $lnk;
                  > }
                  >
                  > Then just call the function wherever you want it
                  > e.g. <?php menuitem("index .php","Home"); ?><?php
                  > menuitem("page1 .php","Page 1"); ?> etc.[/color]

                  IM*H*O, this is pretty "old" style. Much easier/new technique is
                  just changing the id of BODY or outer DIV container.

                  --
                  <?php echo 'Just another PHP saint'; ?>
                  Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                  Comment

                  • Geoff Berrow

                    #10
                    Re: Help coding a navigation bar

                    I noticed that Message-ID:
                    <1112841696.740 179.21390@l41g2 000cwc.googlegr oups.com> from R. Rajesh
                    Jeba Anbiah contained the following:
                    [color=blue]
                    > IM*H*O, this is pretty "old" style. Much easier/new technique is
                    >just changing the id of BODY or outer DIV container.[/color]

                    I'm not sure what you mean. This menu file is simply edited and
                    included in any page that needs a menu. It doesn't get much easier.

                    --
                    Geoff Berrow (put thecat out to email)
                    It's only Usenet, no one dies.
                    My opinions, not the committee's, mine.
                    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                    Comment

                    • R. Rajesh Jeba Anbiah

                      #11
                      Re: Help coding a navigation bar

                      Geoff Berrow wrote:[color=blue]
                      > I noticed that Message-ID:
                      > <1112841696.740 179.21390@l41g2 000cwc.googlegr oups.com> from R. Rajesh
                      > Jeba Anbiah contained the following:
                      >[color=green]
                      > > IM*H*O, this is pretty "old" style. Much easier/new technique is
                      > >just changing the id of BODY or outer DIV container.[/color]
                      >
                      > I'm not sure what you mean. This menu file is simply edited and
                      > included in any page that needs a menu. It doesn't get much easier.[/color]

                      You don't have to change the list. Change only the id of BODY or
                      outer DIV; something like:
                      <body id="<?php echo str_replace(arr ay('/', '.'), '',
                      $_SERVER['PHP_SELF']);?>">
                      ....
                      </body>

                      The technique is here
                      <http://www.sitepoint.c om/print/css-anthology-tips-tricks-4> (Figure
                      4.20). I personally think, this is a great CSS technique; OP also
                      responded positively.

                      --
                      <?php echo 'Just another PHP saint'; ?>
                      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                      Comment

                      Working...