evanesq Posted May 23, 2008 Share Posted May 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/106880-using-variables-in-links/ Share on other sites More sharing options...
smc Posted May 23, 2008 Share Posted May 23, 2008 <?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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/106880-using-variables-in-links/#findComment-547898 Share on other sites More sharing options...
sasa Posted May 23, 2008 Share Posted May 23, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/106880-using-variables-in-links/#findComment-547918 Share on other sites More sharing options...
evanesq Posted May 23, 2008 Author Share Posted May 23, 2008 Thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/106880-using-variables-in-links/#findComment-548019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.