PDA

View Full Version : simple? php help




ghostchild
Sep 23, 2008, 12:13 PM
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.



angelwatt
Sep 23, 2008, 12:20 PM
Are you wanting a navigation that only goes forward and back rather than to any page? Seems odd. Your description is a little confusing.

ghostchild
Sep 23, 2008, 12:28 PM
back and forth, sorry for the confusion.

angelwatt
Sep 23, 2008, 12:52 PM
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

$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.

ghostchild
Sep 23, 2008, 12:58 PM
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...?

angelwatt
Sep 23, 2008, 01:37 PM
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...?

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 (http://us2.php.net/manual/en/function.scandir.php). 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 (http://us3.php.net/readdir) might be better. See example #2. There's also the glob function (http://us3.php.net/manual/en/function.glob.php) that can help.

angelwatt
Sep 23, 2008, 09:26 PM
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
$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> ";
}
?>