simple? php help

Discussion in 'Web Design and Development' started by ghostchild, Sep 23, 2008.

  1. ghostchild macrumors 6502

    ghostchild

    Joined:
    Jun 17, 2007
    #1
    Ok all, I need your help on something that is probably and hopefully really simple. So let's say I have an index full of pages or .htmls and on my main page I have a previous and next link and I obviously I want them to go to the next page in the index when i click next and back to the previous page. How do I do this?

    Thanks.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Are you wanting a navigation that only goes forward and back rather than to any page? Seems odd. Your description is a little confusing.
     
  3. ghostchild thread starter macrumors 6502

    ghostchild

    Joined:
    Jun 17, 2007
  4. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    This is untested PHP code that I believe will give you what you want. You'll of course have to do some tweaking for your layout. Fill the $nav array with pages that you want to be able to get to. This code will only output a previous/next link if it exists. That could be modified if you want to do something else.

    PHP:
    <?php

    $nav 
    = array('page1.php''page2.php''page3.php');
    // get index of current page in array
    $key array_search($_SERVER['SCRIPT_FILENAME']);
    // output previous link if there is a previous
    if (array_key_exists($nav[$key-1])) {
      echo 
    "<a href='$nav[$key-1]'>Previous</a>";
    }
    // out next link if exist
    if (array_key_exists($nav[$key+1])) {
      echo 
    "<a href='$nav[$key+1]'>Previous</a>";
    }

    ?>
    Edit: The code above was untested and turned out to be wrong so don't try to use it. See good code at post #7 below.
     
  5. ghostchild thread starter macrumors 6502

    ghostchild

    Joined:
    Jun 17, 2007
    #5
    for my array is there a short hand way to tell it just to grab all the pages in my index without listing them out like page1, page2...?
     
  6. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #6
    I don't have the time to create the code for it, but if you want to find out what it'll take, check out the manual for PHP's scandir function. It's likely what you'll need to use to grab all of the file names and put them into an array. There may be better ways, but should be a decent start.

    Edit: Actually, the readdir might be better. See example #2. There's also the glob function that can help.
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    OK, I have some code done to help you out. I tested it out too and it works as I expect it to. Give it a try to see if it does what you expect. You'll have to add this PHP to any page that you want to links to show up in.

    PHP:
    <?php
    $files 
    = Array(); // to hold file names
    $tmp glob("*"); // grabs all file/directories in current directory
    $t2 preg_split("/\//"$_SERVER['PHP_SELF']);
    $currFile $t2[count($t2)-1]; // grabs just the filename and no path

    // grab all of the files and leave behind the directories
    foreach ($tmp as $f) {
      if (!
    is_dir($f)) { $files[] = $f; }
    }
    // get index of current page in array
    $key array_search($currFile$files);
    // output previous link if there is a previous
    if (array_key_exists($key-1$files)) {
      echo 
    "<a href='"$files[$key-1] ."'>Previous</a> ";
    }
    // output next link if exist
    if (array_key_exists($key+1$files)) {
      echo 
    "<a href='"$files[$key+1] ."'>Next</a> ";

    ?>
     

Share This Page