My project involves sections with a number of individual pages, each linked to the previous and next in the section series, however dynamically, from an array.
My array looks like this:
PHP Code:
<?php
$dst = array(
'1723' => array('dist1723_orbindale','Orbindale'),
'1743' => array('dist1743_roseberry','Roseberry'),
'1960' => array('dist1960_batt','Batt'),
'2042' => array('dist2042_ross','Ross'),
'2849' => array('dist2849_education_point','Education Point'),
'2358' => array('dist2358_albert','Albert'),
'3160' => array('dist3160_alma_mater','Alma Mater'),
'3360' => array('dist3360_lynx','Lynx'),
'3840' => array('dist3840_passchendale','Passchendale'),
'3975' => array('dist3975_avonglen','Avonglen'),
'3090' => array('dist3090_battle_heights','Battle Heights'),
'3034' => array('dist3034_rodino','Rodino'),
'3510' => array('dist3510_willow_view','Willow View'),
'1967' => array('dist1967_echo','Echo'),
'3677' => array('dist3677_plaxtol','Plaxtol')
);
?>
I've come up with a brute force counting method to suss out the previous and next in the array and am wondering if there is a more elegant, (or abstract) way to accomplish this? Here is my method ($ids[0] is the folder for the current page's section; $tpg is the resource name of the current page after being stripped of extraneous path details.):
PHP Code:
function prevNext() {
global $ids, $dst, $pre_uri, $nex_uri, $pre_title, $nex_title, $tpg;
$needle = array("/little_schools/$ids[0]/",".html");
$tpg = str_replace($needle,'',$_SERVER['REQUEST_URI']);
$count = 0;
foreach ($dst as $key => $val) {
if ($val[0]==$tpg) break;
$count++;
};
$pre = $count - 1; if ( $pre < 0 ) { $pre = count($dst) - 1; };
$nex = $count + 1; if ( $nex == count($dst) ) { $nex = 0; };
$count = 0;
$pre_uri = $nex_uri = "";
foreach ($dst as $key => $val) {
if ($pre==$count) { $pre_uri = $needle[0] . $val[0] . $needle[1]; $pre_title = $val[1]; };
if ($nex==$count) { $nex_uri = $needle[0] . $val[0] . $needle[1]; $nex_title = $val[1]; };
if ($pre_uri != "" && $nex_uri != "") break;
$count++;
};
};
The links are dynamically created and called up by the template:
PHP Code:
<a href="<?php print_u($pre_uri); ?>" title="<?php print_t($pre_title); ?>">Previous <span class="symbol">«</span></a>
<!-- and -->
<a href="<?php print_u($nex_uri); ?>" title="<?php print_t($nex_title); ?>"><span class="symbol">»</span> Next</a>
The print_u and print_t functions as simply echo statements:
PHP Code:
function print_u($uri) { echo $uri; };
function print_t($title) { echo $title; };
Is this about as good as it gets? Or is there a better, more refined method?