Previous and next link with url in array
Here's my php code
<?php
$pageOK = array('page0' => 'page0.php',
'page1' => 'content1.php',
'page2' => 'anotherContent2.php',
'page3' => 'anotherpagewithotherlink.php',
'page4' => 'blablabla.php',
'page5' => 'bleh.php',
'page6' => 'foo.php',
'page7' => 'bar.php');
if ( (isset($_GET['projet'])) && (isset($pageOK[$_GET['projet']])) ) {
include($pageOK[$_GET['projet']]);
}
?>
This will make in sort that "index.php?project=page0" will load in the url with mod_rewrite enable (http://www.example.com/page0/)
Note that the url is different. My questio开发者_运维技巧n is how I can make a Prev / Next link to go to the next or previous array and if it's the first hide the Prev link and for the last hide the Next link.
foreach($pageOK as $key => $val){
if($key == $_GET['projet']){
$next = key($pageOK);
break;
}
$prev = $key;
}
$next
and $prev
holds the array keys.
I think, just get the number from the $_GET['project'] and decrease/increase should be what you want. And if you change the 'page0' to just a number '0' it simplify this problem.
so.. actually this is bad structure. You can put your array to common file like pages.php and on each page include it. So example for page0.php
require_once ('pages.php');
$page_name = 'bar.php';
$key = array_search($page_name, $pageOK);
if ($key !== FALSE)
{
$key_arr = array_keys($pageOK);
$index = array_search($key, $key_arr);
if (!empty($index))
{
buildLink($key_arr[$index-1], 'Prev page');
}
if ($index !== count($key_arr)-1)
{
buildLink($key_arr[$index+1], 'Next page');
}
}
code for pages.php
$pageOK = array('page0' => 'page0.php',
'page1' => 'content1.php',
'page2' => 'anotherContent2.php',
'page3' => 'anotherpagewithotherlink.php',
'page4' => 'blablabla.php',
'page5' => 'bleh.php',
'page6' => 'foo.php',
'page7' => 'bar.php');
function buildLink($page, $anchor)
{
if (isset($pageOK($page)))
{
print '<a href="$root_url.$page">'.$anchor.'</a>';
}
}
But much better if you move array of page to db and create page controller
精彩评论