Submit Your Article Forum Rules

Results 1 to 3 of 3

Thread: Dynamic 'previous' and 'next'

  1. #1
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,826

    Question Dynamic 'previous' and 'next'

    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 ) { $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&nbsp;<span class="symbol">«</span></a>

    <!-- and -->

    <a href="<?php print_u($nex_uri); ?>" title="<?php print_t($nex_title); ?>"><span class="symbol">»</span>&nbsp;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?

  2. #2

  3. The following user agrees with ep2012:
  4. #3
    Administrator weegillis's Avatar
    Join Date
    Oct 2003
    Posts
    5,826
    To see it live, Michelle, go to any page listed in any section index in the little schools link in my sig. There are Previous and Next buttons in both navigation menus to 'turn the pages' so to speak. It's also being incorporated into photo pages that accompany each of the school stories. Subdivision 5 is changed over, but the rest need doing.

    The script needed to be refined to query both values for each key in order to re-use it with the photo page template. Here is the refined version:
    PHP Code:
    $PINS = array(". "," "); // global constant can be shared by entire library
    function prevNext() {
     global 
    $ids$dst$pre_uri$nex_uri$pre_title$nex_title$tpg$PINS;
     
    $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) { $idx 0; break; };
      if (
    str_replace($PINS,'_',$val[1])==$tpg) { $idx 1; break; };
      
    $count++;
     };
     
    $pre $count 1;
     if ( 
    $pre ) {
      
    $pre count($dst) - 1;
     };
     
    $nex $count 1;
     if ( 
    $nex == count($dst) ) {
      
    $nex 0;
     };
     
    $count 0;
     
    $pre_uri $nex_uri "";
     foreach (
    $dst as $key => $val) {
      
    $res_name = (!$idx) ? $val[$idx] : str_replace($PINS,'_',$val[$idx]);
      if (
    $pre==$count) {
       
    $pre_uri $needle[0] . $res_name $needle[1];
       
    $pre_title $val[1];
      };
      if (
    $nex==$count) {
       
    $nex_uri $needle[0] . $res_name $needle[1];
       
    $nex_title $val[1];
      };
      if (
    $pre_uri != "" && $nex_uri != "") break;
      
    $count++;
      };
    }; 
    $PINS is the needle array for required string replacement, [period-space], and [space]. The REQUEST_URI will have underscores but my table data does not, so I have to manipulate it to get a possible match. Saves me storing another piece of data, when one so similar is already stored. The dot-space is for names with 'St. ' in them, or rather the one name, but since the script is used in two templates, that makes two.

    Of the variables in the global list, only $ids, $dst and $PINS exist outside of this function. The rest are made global so they can be shared with the entire library.

    The main needle contains the URL portion of the REQUEST_URI in one value, and the MIME extension in the other. I specify the folder from data stored in the requested resource so I know it will match the URI. Once the URI is stripped of these two things, I'm left with a resource name that will match one stored in the data table, either matching the first value, or a close approximation to the second (for which I need $PINS to create the exact match).

    With this in hand I can now query the data table to pin down the actual row a match occurs in. A counter accumulates at each pass through the foreach loop. Once a match is found the loop terminates and we go on to the wraparound evaluation.

    The order of our data table dictates. If there is no previous at the top of the stack, we set a pointer to the bottom row, and vis a vis, if no next exists at the bottom the pointer is set to the top row.

    Next we cycle through the table again and count our way to both previous and next rows, copying the row data when the pointer matches the (new) counter. We re-use the $needle data to rebuild the URI of each one. This data is returned to the template to construct the set of links. If it is a story page, val[0] will used, if it is a photo page, val[1] will be used after first being edited.
    Last edited by weegillis; 05-23-2012 at 08:32 PM. Reason: typos / verbage

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •