SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Is this efficient?

    I need a different menu for many pages on my site. I'm using an include file on each page to include the menu. Rather than have a separate menu file to include for each page could I do something like this:
    PHP Code:
     <div id="menu">
    <a href="./" title="home" class="alt">home</a>
    <?php
    $url 
    $HTTP_SERVER_VARS["HTTP_HOST"] . $HTTP_SERVER_VARS["REQUEST_URI"];
    if (
    $url "www.site.com/page1.php") {
    ?> 
    <font class="alt">page1 &raquo; </font>
    <a href="link1.php">link1</a> 
    <a href="link2.php">link2</a> 
    <a href="link3.php">link3</a>     
     
    <?php
    }
    if (
    $url "www.site.com/page2.php") {
    ?> 
    <font class="alt">page2 &raquo; </font>
    <a href="link4.php">link4</a> 
    <a href="link5.php">link5</a> 
    <a href="link6.php">link6</a> 
    <?php
    }
    ?> 
    </div>
    If there are eventually many pages on the site, would this be an in
    efficient

    method? Also, why are both menus being included if I include this file on a page?

    Thanks!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    if ($url = "www.site.com/page2.php")

    must be

    if ($url == "www.site.com/page2.php")


    Using a single '=' gives the new value to $url, and it's result is always true

  3. #3
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toma
    I need a different menu for many pages on my site. I'm using an include file on each page to include the menu. Rather than have a separate menu file to include for each page could I do something like this:
    PHP Code:
     <div id="menu">
    <a href="./" title="home" class="alt">home</a>
    <?php
    $url 
    $HTTP_SERVER_VARS["HTTP_HOST"] . $HTTP_SERVER_VARS["REQUEST_URI"];
    if (
    $url "www.site.com/page1.php") {
    ?> 
    <font class="alt">page1 &raquo; </font>
    <a href="link1.php">link1</a> 
    <a href="link2.php">link2</a> 
    <a href="link3.php">link3</a>     
     
    <?php
    }
    if (
    $url "www.site.com/page2.php") {
    ?> 
    <font class="alt">page2 &raquo; </font>
    <a href="link4.php">link4</a> 
    <a href="link5.php">link5</a> 
    <a href="link6.php">link6</a> 
    <?php
    }
    ?> 
    </div>
    If there are eventually many pages on the site, would this be an in
    efficient

    method? Also, why are both menus being included if I include this file on a page?

    Thanks!
    I used a similar method on a site before and it always worked well enough for me

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    And yes, i can immagine it becoming an inefficient method if the number of pages, and different menus, grows.
    You might put the menu-links in a table (columns 'page' and 'link'). Then you just extract the links for the page you're on and display them.

  5. #5
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004
    if ($url = "www.site.com/page2.php")

    must be

    if ($url == "www.site.com/page2.php")


    Using a single '=' gives the new value to $url, and it's result is always true
    Of course...my oversight. Thanks guido2004, that did the trick!

  6. #6
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004
    And yes, i can immagine it becoming an inefficient method if the number of pages, and different menus, grows.
    You might put the menu-links in a table (columns 'page' and 'link'). Then you just extract the links for the page you're on and display them.
    Hmmm...I assume you mean a database table for pages and corresponding links. I'll have to think a bit about how to extract these as needed....

    Thanks for the good suggestion.

  7. #7
    SitePoint Member AlexBrina's Avatar
    Join Date
    Jul 2004
    Location
    office.bh.mg.br
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would do something like this, because if you need to change the menu format someday, you change only the buildMenu function.

    PHP Code:
    function buildMenu$page$links )
    {
      
    $html "<font class=\"alt\">$title</font>";
      foreach( 
    $links as $href => $text )
      {
        
    $html .= "<a href=\"$href\">$text</a>";
      }
      
      return 
    $html;
    }

    switch ( 
    $url )
    {
      case 
    "www.site.com/page1.php":
        
    $page 'page1';
        
    $links = array( "link1.php"=>"link1""link2.php"=>"link2" );
        break;
      case 
    "www.site.com/page2.php":
        
    $page 'page2';
        
    $links = array( "link4.php"=>"link4""link5.php"=>"link5" );
        break;
    }

    echo 
    buildMenu$page$links ); 
    Alex Brina
    "...sempre q eu tirar a cabeça fora d'agua eu dou um alô..." JC

  8. #8
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AlexBrina
    I would do something like this, because if you need to change the menu format someday, you change only the buildMenu function.

    PHP Code:
    function buildMenu$page$links )
    {
    $html "<font class=\"alt\">$title</font>";
    foreach( 
    $links as $href => $text )
    {
    $html .= "<a href=\"$href\">$text</a>";
    }
     
    return 
    $html;
    }
     
    switch ( 
    $url )
    {
    case 
    "www.site.com/page1.php":
    $page 'page1';
    $links = array( "link1.php"=>"link1""link2.php"=>"link2" );
    break;
    case 
    "www.site.com/page2.php":
    $page 'page2';
    $links = array( "link4.php"=>"link4""link5.php"=>"link5" );
    break;
    }
     
    echo 
    buildMenu$page$links ); 
    This looks like a good method too. I wonder if it's any more efficient when used with many pages and menus than the original version with "if" statements.

  9. #9
    SitePoint Member AlexBrina's Avatar
    Join Date
    Jul 2004
    Location
    office.bh.mg.br
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    umhh... more efficient... all right

    PHP Code:
    function buildMenu$menuSource ) {
      
    $html "<font class=\"alt\">{$menuSource['page']}</font>";
      foreach( 
    $menuSource['links'] as $href => $text ) {
        
    $html .= "<a href=\"$href\">$text</a>";
      }
      return 
    $html;
    }

    function 
    getMenuSource$url ) {
      
    $page substr$urlstrpos$url'/' ) + );
      
    $links = include( 'menu_' $page );
      return array( 
    'page'=>$page'links'=>$links );
    }

    echo 
    buildMenugetMenuSource$url ) ); 
    create one menu file for each page :
    PHP Code:
    //file menu_page1.php
    return array( "link1.php"=>"link1""link2.php"=>"link2" ); 
    PHP Code:
    //file menu_page2.php
    return array( "link4.php"=>"link4""link5.php"=>"link5" ); 
    now you can have as many menus as you like, without touching your code, all you need to do is create another file corresponding to the page it belongs to.
    Alex Brina
    "...sempre q eu tirar a cabeça fora d'agua eu dou um alô..." JC

  10. #10
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AlexBrina
    umhh... more efficient... all right

    PHP Code:
    function buildMenu$menuSource ) {
    $html "<font class=\"alt\">{$menuSource['page']}</font>";
    foreach( 
    $menuSource['links'] as $href => $text ) {
    $html .= "<a href=\"$href\">$text</a>";
    }
    return 
    $html;
    }
     
    function 
    getMenuSource$url ) {
    $page substr$urlstrpos$url'/' ) + );
    $links = include( 'menu_' $page );
    return array( 
    'page'=>$page'links'=>$links );
    }
     
    echo 
    buildMenugetMenuSource$url ) ); 
    create one menu file for each page :
    PHP Code:
    //file menu_page1.php
    return array( "link1.php"=>"link1""link2.php"=>"link2" ); 
    PHP Code:
    //file menu_page2.php
    return array( "link4.php"=>"link4""link5.php"=>"link5" ); 
    now you can have as many menus as you like, without touching your code, all you need to do is create another file corresponding to the page it belongs to.
    This looks great. I just worked vey hard to implement the other version, but I'll certainly have a closer look - AFTER Thanksgiving. Thanks much!

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i suggest to insert into database the links with ids..that way u can easly edit the links from database in real time and update or delete it!
    so when u go to some page use $_GET variable and switch the id of the links u need for that page!
    cheers

  12. #12
    SitePoint Zealot toma's Avatar
    Join Date
    Jul 2001
    Location
    Tempe, AZ
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    i suggest to insert into database the links with ids..that way u can easly edit the links from database in real time and update or delete it!
    so when u go to some page use $_GET variable and switch the id of the links u need for that page!
    cheers
    Yes, thank you. I was just realizing the problem with _GET info passed along and I posted a new question about how to strip this out of my $url variable.
    PHP Code:
    $url $HTTP_SERVER_VARS["HTTP_HOST"] . $HTTP_SERVER_VARS["REQUEST_URI"]; 
    How to remove the ? character and anything after?

  13. #13
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u havent understand me!
    when u pass a variable with get method then make a query...
    PHP Code:
    $a $_GET["url"];
    $ret mysql_query("SELECT *
                            FROM url_table
                            WHERE sub_id = '
    $a'
                            ORDER BY id DESC"
    );
    while(
    $row mysql_fetch_array($ret)) {
    echo 
    $row["url"];

    obviously u have to store all anchor links to db
    cheers

    ps $_SERVER not http_server_vars couse its deprecated!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •