Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 29

Thread: Help creating a current link

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

    Default

    Thanks Boogyman!

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

    Default

    Is there some other code required to make that work?

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

    Default

    I found this Smarty-related PHP code which if re-written might help the other code posted by boogyman:

    Code:
    define('SITE_URL' = 'http://localhost/');
    
    $smarty->assign('current_section', current_section());
    $smarty->assign('primary_links', build_primary_links();
    
    $smarty->display('index.tpl');
    
    function current_section() {
       // For the purpose of this code, we'll keep this simple:
       return 'Home';
    }
    
    function build_primary_links() {
      // this is also far more complex, but I'll keep this simple as well
      $primary_links = array('Home' => SITE_URL,
                                         'News' => SITE_URL.'/?op=news',
                                        );
      return $primary_links;
    }

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

    Default

    boogyman broke the code. Without Smarty, it's:
    Code:
    <?php foreach($pages as $page) { ?>
      <a href="<?php echo $page->url ?>"<?php if($page == $this_page) { ?> class="current"<?php } ?>>
        <?php echo $page->title; ?>
      </a>
    <?php } ?>
    ... assuming $pages is an array of "page" objects with "url" and "title" properties. It was mostly example pseudo-code though.

    I really do recommend using a templating system for any reasonably-sized sites. PHP's default mix of data, logic, and presentation is a maintainability nightmare.
    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!

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

    Default

    This is my array... doesn't appear to be working though:

    Code:
    <?php
    
    define('SITE_URL', 'http://localhost/');
    
    // array(page_title => page_url)
    
    $pages = array('Home' => SITE_URL,
                   'News' => SITE_URL.'/news.php',
                   'Forum' => SITE_URL.'/forum',
                   ); 
    ?>

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

    Default

    I said "objects," not "arrays."
    Code:
    class Page {
      public $title;
      public $url;
    
      private static $instances = array();
    
      public static get_current() {
        foreach(self::$instances as $page)
          if($page->url === SITE_URL . $_SERVER['REQUEST_URI'])
            return $page;
        return false;
      }
    
      public 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!

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

    Default

    Thanks Twey, but using that I get the following error message:

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

    EDIT: I think the problem is that my server runs PHP4 and not PHP5 but I'm not entirely sure...
    Last edited by matthewmjscott; 09-18-2007 at 11:45 AM.

  8. #18
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by matthewmjscott View Post
    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/bigbro/public_html/matthewmjscott/testingmenu.php on line 95
    refers to a missing curly brace.. the error occured on line 95. you are going to need to check all of your open curly braces above that point to figure out which one(s) has not been closed.

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

    Default

    Yeah. There's no missing curly braces, I think it's because I don't have PHP5. Thanks for your help anyway, I will recommend DD to my friends

  10. #20
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    the php interpretter doesnt lie; it has nothing to do with which version of php you have installed
    if you cannot find the mistake but would like help post your code encapsulated in [code][/code] tags
    Last edited by Twey; 09-18-2007 at 02:42 PM. Reason: Removed dots.

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
  •