Jump to content

Using variables in links


evanesq

Recommended Posts

I am designing a website with several pages that are sequentially numbered, i.e., 0001.html, 0002.html, etc.

 

I'd like to be able to put a <right arrow> button on each page, which when clicked, will send the user to the next sequential page.

 

Is there a way to use a variable in the link, i.e., http://www.myweb.com/000n.html, and define the click on the button to make n=n+1?

 

In other words, if someone is looking at 0001.html and clicks the <right arrow>, 0002.html should load.

 

At the risk of being too annoying, I'd also like some sort of onerr command so that if the value of n exceeds the maximum number of pages and returns a 404, the user will be sent back to the main menu.

 

I'm told this should be done in php, but I haven't a clue where to start.

 

I'd appreciate any help.

Link to comment
https://forums.phpfreaks.com/topic/106880-using-variables-in-links/
Share on other sites

<?php

$myDomain = "http://www.myweb.com/"; //This is your base domain
$currentPage = "0001"; //This is the current page. You can substitute this with a $_SERVER variable to get the current page

$newPage = $currentPage++; //This adds 1 to the current page value

$linkAddress = $myDomain . $newPage . ".html"; //This will put it all together, the myDomain, tacks on the current page +1 the finishes it up with .html

?>

create file next.php

<?php
$url = $_SERVER['HTTP_REFERER'];
preg_match('/\d+\.html/', $url, $a);
$old_name = $a[0];
$a = explode('.',$old_name);
$a[0] = sprintf('%04d', $a[0] + 1);
$a = implode('.',$a);
$new_link = str_replace($old_name, $a, $url);
$x = parse_url($new_link);
if (!file_exists($_SERVER['DOCUMENT_ROOT'].$x['path'])) $new_link = '0001.html';
header("Location: $new_link");
?>

and link <right arrow> to it

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.