Jump to content

Noob question: using URL variables for "next page" and "previous page" buttons


macfall

Recommended Posts

I'm a very new PHP user (as in, I just started today) and need to make a set of navigation buttons which will take the user to "next" or "previous" pages. I assume that the $_GET function is involved, but I don't know how to do it.

 

It needs to work like this:

 

If the URL looks like "http://website.com/page1.php"

 

I need the "next" button (a href image) to change it to "/page2.php", and while on page 2, for the "previous" button to take the user to "/page1.php" and so forth. How can I cause the page number to increment or decrease respective to the href image being clicked?

you should read up on pagination:

but for a dirty way

  function stripName() //strip the page number from script name
  {
    $page = strtolower($_SERVER[php_SELF]);
    $pref = "page"; // prefix of file name to strip
    $postf = ".php"; // postfix of file to strip
    return substr($page,strlen($pref)-1,strlen($page)-(strlen($postf)+strlen($pref)));
  }
  function filec($page) //check file exists
  {
    if(file_exists($page)){return true;}else{return false;}return false;
  }
  $thispage = stripName(); // grab page number
  $prevpage = $thispage-1; // previous page
  $nextpage = $thispage+1; // nextpage
  if(!filec($prevpage)){$prevpage=$thispage;$noprev=1;} // check previous page exists
  if(!filec($nextpage)){$nextpage=$thispage;$nonext=1;} // check next page exists
  if(!isset($noprev)){$links = "<a href=\"".$prevpage."\">[ Previous Page ]</a>";} // build the link if previous exists
  if(!isset($nonext)){$links .= "<a href=\"".$nextpage."\">[ Next Page ]</a>";} // build the link if next exists
  echo $links; // display links to screen

 

hope this helps :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.