Jump to content

Need help with simple code, back and forward buttons.


Recommended Posts

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.

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.

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>";
?>

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

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.

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);

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.

$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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.