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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.