SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member steviehype's Avatar
    Join Date
    May 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple next and previous links from an array (help please)

    Hi there,

    I'm still very new to PHP and wondered if you could help.

    Heres the situation...

    I have 8 web pages. index.php, page2.php, page3.php etc..
    These include a header.html and footer.html (in an "includes" folder).

    I'd like to have a simple "next page" and "previous page" links at the top of every page.

    So I created a functions.php and put that in the "includes" folder.
    In the header.html I call the function np()

    Now this is the way I thought I could do it (probably the wrong way!)....

    * find the page we're on
    * locate position in the array (The site will never be very big so I can just add the page to this array if I create more in the future)
    * make next and previous buttons +1 and -1 of that position
    * if at the end of array, loop to the beginning.

    PHP Code:
    function np(){
        
    $url = array(
            
    'index' => 'index',
            
    'page2' => 'page2',
            
    'page3' => 'page3',
            
    'page4' => 'page4',
            
    'page5' => 'page5',
            
    'page6' => 'page6',
            
    'page7' => 'page7',
            
    'page8' => 'page8'
        
    );
        foreach (
    $url as $key => $value){
            if(
    $_SERVER['PHP_SELF'] == "$key.php"){
                
    /*Here's where I thought I could state that 
    $next would be the $key +1 of the array index and 
    $previous would be the $key -1 of the array index

    I'd also like it to loop around when the user pressed 
    next on page8.php it would go to index.php*/

            
    }
            else{
                
    //not sure what I'd put here!
            
    }
        }
        

    I've been trying various different ways of doing this, but I''m not very good.

    So this is my last resort, please could someone help me write this code?

    Any help is much appreciated.

    Steve

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make the array indexed by number

    PHP Code:
        $url = array(
                
    'index',
                
    'page2',
                
    'page3',
                
    'page4',
                
    'page5',
                
    'page6',
                
    'page7',
                
    'page8'
            
    ); 
    then use array_search(page, $url) to get the index for the current page. then you can add and subtract 1 to get the prev and next pages.

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I created a functions.php and put that in the "includes" folder.
    In the header.html I call the function np()
    You can only include files from php pages. rename header.html to header.php

  4. #4
    SitePoint Member steviehype's Avatar
    Join Date
    May 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks,

    I've got this so far, but at the moment its printing whats indexed at position 1 in the array.

    PHP Code:
    function fl(){
        
    $url = array('index.php''page2.php''page3.php''page4.php''page5.php''page6.php''page7.php''page8.php'); 
        
        
    $path basename($_SERVER['PHP_SELF']);
        
        foreach (
    $url as $page){
            if (
    $path == $page){
                
    $next $url[+1];
            }
            else{
                
            }
        }
        echo 
    "<a href=\"$next\">Next</a>";
        

    how would I get $next to actually be +1 in the array?

    Thanks again,

  5. #5
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this...

    Code PHP:
    function fl(){
     
        $url = array('index.php', 'page2.php', 'page3.php', 'page4.php', 'page5.php', 'page6.php', 'page7.php', 'page8.php'); 
     
        $path = basename($_SERVER['PHP_SELF']);
     
       foreach ($url as $key => $page){
            if ($path == $page){
                  if ($prev = $url[$key-1])
                        $prev_link = "<a href=\"{$prev}\">prev</a>";
                  if ($next = $url[$key+1])
                        $next_link = "<a href=\"{$next}\">next</a>";
            }
            else{
     
     
            }
     
        }
     
        echo $prev_link . ' - ' . $next_link;
     
    }

    You gotta test for the existence of the prev/next pages.

  6. #6
    SitePoint Member steviehype's Avatar
    Join Date
    May 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank-you so much for your help. I many more lines of code than that!

    Thanks again,

    Steve


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
  •