1

I have a list of filenames

aeyrt.php
nviorf.php
vyutkd.php
oiuybd.php
poiuuy.php

Suppose that the current opened page is vyutkd.php

I have two arrows < and >

My question is how using PHP or Javascript the < will link to nviorf.php and the > to oiuybd.php ?

The same scenario goes if the current page is the first one. The < button should go to the last of the list.

Thank you

2
  • Generally you assign each one a number, and then next and previous can be determined just by simple +1/-1 maths. If you store these items in a database, the assigned number is a primary key. Commented Nov 29, 2014 at 13:35
  • @halfer thank you for this but I must avoid database for this Commented Nov 29, 2014 at 13:36

2 Answers 2

1

You could put the files name into an array:

$pages= Array( 'aeyrt.php','nviorf.php','vyutkd.php','oiuybd.php','poiuuy.php');

And create a function that check the current file name position and return a link to the previous or next page.

function getLink($pages,$previous = true){
 $position = array_search(basename(__FILE__),$pages);
 if($position==0 && $previous){
  return "<a href='".$pages[count($pages)-1]."'>Previous</a>";
 }
 if($position==count($pages)-1 && !$previous){
  return "<a href='".$pages[0]."'>Next</a>";
 }
 if($previous && $position!==null && $position>0){
  return "<a href='".$pages[$position-1]."'>Previous</a>";
 }
 if($position!==null && $position<count($pages)-1){
  return "<a href='".$pages[$position+1]."'>Next</a>";
 }
}

I did not check if it work, but I think so ;)

I modified the funtion to be circular

Sign up to request clarification or add additional context in comments.

6 Comments

How to call the function ?
echo getLink($pages); for previous link echo getLink($pages,false); for next link
I also get this Parse error: syntax error, unexpected '-', expecting ']'
When I am on the first page, it is shown as next and next both with the same link. It is supposed to have again the previous that goes to the last link and the next to the next.
Sorry my friend but it does not working as it is supposed.... When I am in one page, the links are so messing I can not find a pattern
|
0

Would be something like this:

corrected code:

<?php
  $pages = array('aeyrt.php','nviorf.php','vyutkd.php','oiuybd.php','poiuuy.php');
  $current_file = basename(__FILE__);
  if(($current_id = array_search($current_file,$pages))){
     if($current_id == count($pages) - 1){
        echo "<a href=\"".$pages[$current_id -1]."\">previous</a>";
     }else{
        echo "<a href=\"".$pages[$current_id -1]."\">previous</a> <a href=\"".$pages[$current_id +1]."\">next</a>";
     }
  }else{
     echo "<a href=\"$pages[2]\">next</a>";
  }  
?>

But... I would personally put those links into database and use rewrite rules.

2 Comments

Thank you, however I have corrected the parenthesis on line 4 but there are other bugs in this..
When I am at the first link I get only a next link that goes me to the last of the list. The ideal is to still have previous and next...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.