Page 3 of 3 FirstFirst 123
Results 21 to 29 of 29

Thread: Help creating a current link

  1. #21
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, he's right in this instance, I tried to write PHP4-compatible code but failed. Shame on me. The code for PHP4 is:
    Code:
    class Page {
      var $title;
      var $url;
    
      static $instances = array();
    
      static function get_current() {
        foreach(self::$instances as $page)
          if($page->url === SITE_URL . $_SERVER['REQUEST_URI'])
            return $page;
        return false;
      }
    
      function Page($title, $url) {
        $this->title = $title;
        $this->url = $url;
        Page::$instances[] = $this;
      }
    }
    
    define('SITE_URL', 'http://localhost/');
    
    $pages = array(
      new Page('Home', SITE_URL),
      new Page('News', SITE_URL . '/news.php'),
      new Page('Forum', SITE_URL . '/forum')
    );
    
    $current_page = Page::get_current();
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  2. #22
    Join Date
    Sep 2007
    Location
    Kent, UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry to be a huge pain but I am still getting the following error:

    Parse error: syntax error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/bigbro/public_html/matthewmjscott/testingmenu.php on line 95
    Line 95:

    Code:
      static $instances = array();
    There is no PHP code in there whatsoever apart from what I have used from this thread.

    I'm a little clueless now

  3. #23
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah, static variables in PHP4 are pretty much non-existant:
    Code:
    $page_instances = array();
    
    class Page {
      var $title;
      var $url;
    
      static function get_current() {
        global $page_instances;
    
        foreach($page_instances as $page)
          if($page->url === SITE_URL . $_SERVER['REQUEST_URI'])
            return $page;
        return false;
      }
    
      function Page($title, $url) {
        global $page_instances;
    
        $this->title = $title;
        $this->url = $url;
    
        $page_instances[] = $this;
      }
    }
    
    define('SITE_URL', 'http://localhost/');
    
    $pages = array(
      new Page('Home', SITE_URL),
      new Page('News', SITE_URL . '/news.php'),
      new Page('Forum', SITE_URL . '/forum')
    );
    
    $current_page = Page::get_current();
    I hate PHP :-\
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #24
    Join Date
    Sep 2007
    Location
    Kent, UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I hate PHP too, it's frustrating...

    anyway, I'm now getting an error on line 97, which is this:

    Code:
    static function get_current() {
    It's a static variable, which is what seems to be causing the problem... hopefully this should be the last time I need help?

    I'm sorry for being a complete and utter pain!

  5. #25
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, yeah, missed that one:
    Code:
    $page_instances = array();
    
    function page_get_current() {
      global $page_instances;
    
      foreach($page_instances as $page)
        if($page->url === SITE_URL . $_SERVER['REQUEST_URI'])
          return $page;
      return false;
    }
    
    class Page {
      var $title;
      var $url;
    
      function Page($title, $url) {
        global $page_instances;
    
        $this->title = $title;
        $this->url = $url;
    
        $page_instances[] = $this;
      }
    }
    
    define('SITE_URL', 'http://localhost/');
    
    $pages = array(
      new Page('Home', SITE_URL),
      new Page('News', SITE_URL . '/news.php'),
      new Page('Forum', SITE_URL . '/forum')
    );
    
    $current_page = page_get_current();
    Now, it should be noted that this is a Ruddy Ugly Hackę, of a magnitude of ugliness rarely seen in real, decent modern programming languages. In fact, even in PHP5 this isn't necessary, although that has plenty of problems of its own. I strongly suggest upgrading: support for PHP4 ends fairly soon, if I remember correctly. Moving to another language (Python? Ruby?) would be even better, of course
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #26
    Join Date
    Sep 2007
    Location
    Kent, UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh my god thank you so much... you are totally awesome... thanks for helping me out when other people have turned me away!

    This truly is the best coding forum in the world - I will be recommending it to all my friends!

    And Twey, I will be working on upgrading/moving soon!

    Thanks a million!

    EDIT: Only problem is, the code you posted earlier on doesnt work:

    Code:
    <?php foreach($pages as $page) { ?>
    <li><a href="<?php echo $page->url ?>"<?php if($page == $this_page) { ?> class="current"<?php } ?>><?php echo $page->title; ?></a></li>
    <?php } ?>
    It doesn't change the class to current on the selected tabs for some reason...

    I should point out I need the ID of the <li> tags to be current and not the class of the links
    Last edited by matthewmjscott; 09-18-2007 at 05:30 PM.

  7. #27
    Join Date
    Sep 2007
    Location
    Kent, UK
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh wait, fixed this problem myself.
    Code:
    $this_page
    should be
    Code:
    $current_page
    Thanks again!

  8. #28
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Ah -- yes, my error.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #29
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey

    I came across this page whilst searching the web and just wanted to ask:

    Is there any easy way to mod the script so that the class for one link could change to current for two or more URLs?

    For example, I may want a "Home" button in the menu to change to a current class if the url read both http://www.example.com and http://www.example.com/index.php.

    I hope I was clear enough? Sorry but I'm a total noob =(

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
  •