DChiuch Posted December 13, 2009 Share Posted December 13, 2009 Basically, I have a set of pages in a folder, which have the title somephrasehere_09.php, somephrasehere_10.php, somephrasehere_11.php, etc. On each of these pages, I want to include a common code that acts as a previous and next link. So, on the 10.php page, I want some code that will link to 09.php (and one for 11.php), and on another page, say 05.php, I want to be able to use the same code, resulting in links to 04.php and 06.php. Now, my PHP knowledge is limited, but it seems to me that manipulating URLs and adding/subtracting numbers to create a new URL should be fairly simple. So, can someone help me with how to do this? Sorry if you're not supposed to post requests here. Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/ Share on other sites More sharing options...
GFXUniverse Posted December 13, 2009 Share Posted December 13, 2009 r u displaying data in these files from Database??? if yes so you dn't need to create files like page02.php, page03.php etc. it can be done in a single file. first let me know u r Displaying data from Database? then i will post he simple and very easy code for pagination. Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976404 Share on other sites More sharing options...
DChiuch Posted December 13, 2009 Author Share Posted December 13, 2009 Nah, I'm not using a database. Is it possible/easy to implement the next/previous buttons the way I have it? Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976406 Share on other sites More sharing options...
GFXUniverse Posted December 13, 2009 Share Posted December 13, 2009 i didn't get it if u mean simply link the page to next or previous page then here is how you can do it its quite simple (may u already know) for page somephrasehere_10.php <a href='somephrasehere_09.php'>Previous Page</a> | <a href='somephrasehere_11.php'>Next Page</a> for page somephrasehere_11.php <a href='somephrasehere_10.php'>Previous Page</a> | <a href='somephrasehere_12.php'>Next Page</a> in php (almost same thing) for page somephrasehere_10.php <?php echo"<a href='somephrasehere_09.php'>Previous Page</a> |"; echo"<a href='somephrasehere_11.php'>Next Page</a>"; ?> for page somephrasehere_11.php <?php echo"<a href='somephrasehere_10.php'>Previous Page</a> |"; echo"<a href='somephrasehere_12.php'>Next Page</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976409 Share on other sites More sharing options...
teamatomic Posted December 13, 2009 Share Posted December 13, 2009 Just stick this where ever you want the links. $path = $_SERVER['SCRIPT_FILENAME']; $f1 = basename($path); //$f1 = "somephrasehere_09.php"; // to test with uncomment list($f2,$ext)=explode(".",$f1); list($name,$num)=explode("_",$f2); $nn = $num+1; $nb = $num-1; $next = "$name" . "_" . "$nn"; $back = "$name" . "_" . "$nb"; $back_link="<a style=\"text-decoration:none\"; href=\"$back.$ext\"><< Last</a>"; $next_link="<a style=\"text-decoration:none\"; href=\"$next.$ext\">Next >> </a>"; echo "$back_link - - $next_link"; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976449 Share on other sites More sharing options...
DChiuch Posted December 13, 2009 Author Share Posted December 13, 2009 Thanks teamatomic, that was exactly what I'm looking for. You're awesome. Couple things though. 1. For numbers between/including 1 and 9, I want it the url to be somephrasehere_05, rather than somephrasehere_5. Is this possible? 2. Also, is it possible to not display a previous link if the page is 01? 3. Lastly, and this is probably a bit tricky. If the next page does not exist (for example, if say 54 is the last page in the folder, ie. that 55 does not exist), then can it not display a next link? Thanks heaps. Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976487 Share on other sites More sharing options...
ignace Posted December 13, 2009 Share Posted December 13, 2009 You should note that at some point in the future you may delete a file and thus this n+1, n-1 analogy doesn't any longer work while it actually should. function getFileParts($file) { return explode('_', pathinfo($file, PATHINFO_FILENAME); } function changeArrayPointerTo(&$array, $number) { while ($file = current($array)) { list(, $fn) = getFileParts($file); if ($fn === $number) { return; } } } $directory = dirname(__FILE__); $files = scandir($directory); natsort($files); list (, $number) = getFileParts(__FILE__); changeArrayPointerTo($files, $number); echo prev($files); echo next($files); Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976498 Share on other sites More sharing options...
emopoops Posted December 13, 2009 Share Posted December 13, 2009 u can use the javascript back thing Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976506 Share on other sites More sharing options...
DChiuch Posted December 13, 2009 Author Share Posted December 13, 2009 You should note that at some point in the future you may delete a file and thus this n+1, n-1 analogy doesn't any longer work while it actually should. Thanks ignace, but for some reason, when I use your code, the page doesn't load past the point where the code is. I'm not gonna delete files, so I think that teamatomic's code (if I can get those things I listed before fixed) would work fine. Thanks anyway though, and if you have any idea why the page won't load with that code, then I'll look into it. Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976508 Share on other sites More sharing options...
ignace Posted December 13, 2009 Share Posted December 13, 2009 I just wrote the code here off-the-bat probably that is the reason why it doesn't work no worry i'll look into it! Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976525 Share on other sites More sharing options...
teamatomic Posted December 13, 2009 Share Posted December 13, 2009 $path = $_SERVER['SCRIPT_FILENAME']; $f1 = basename($path); //$f1 = "somephrasehere_01.php"; // to test with uncomment list($f2,$ext)=explode(".",$f1); list($name,$num)=explode("_",$f2); $nn = $num+1; $nb = $num-1; $nb = str_pad($nb, 2, "0", STR_PAD_LEFT); $nn = str_pad($nn, 2, "0", STR_PAD_LEFT); $next = "$name" . "_" . "$nn"; $back = "$name" . "_" . "$nb"; $back_link="<a style=\"text-decoration:none\"; href=\"$back.$ext\"><<Last </a>"; $next_link="<a style=\"text-decoration:none\"; href=\"$next.$ext\"> Next>></a>"; if(file_exists("$back.$ext")) {echo "$back_link";} if(file_exists("$next.$ext")) {echo "$next_link";} HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976536 Share on other sites More sharing options...
DChiuch Posted December 14, 2009 Author Share Posted December 14, 2009 Thankyou, that code works perfectly. I'll let you know if I have any problems, but for now, I think I'm set. Thanks heaps. I'd rep you, but I don't see how? Quote Link to comment https://forums.phpfreaks.com/topic/184962-need-help-with-simple-code-back-and-forward-buttons/#findComment-976783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.