We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36782
    • 109 Posts
    Hello,
    I'm trying to come up with a snippet for pagination of resources within a particular parent (in this case it is project pages for a theatre designer's website... like that's helpful to know..)

    Requirements are that it not rely on any JS / Ajax. No others that I can think of, other than efficient.

    I've got some test code working for incrementing pages:
    // scroll way down this post to see the new version.
    // this I have taken out for the sake of brevity, 
    // and so as not to be a bad influence on novic-er novices than me.
    // seriously, if you copied any of that, start over - it shouldn't have been posted :(
    


    I call the snippet-let by doing thusly:
    [[!paginate]]

    ... that was a waste of perfectly good 'code' tags, wasn't it?

    you can see that the "next" link has a query string appended.

    so needless to say this is just a messy test to see if I could use $_GET and get any results other than a crash tongue
    My question (what I would like some advice on) is whether this is at all a sensible way to go about it?

    If it isn't a terrible idea, then I will def. clean it up, add <prev and echo the totals / current id, etc etc.

    I foresee someone saying 'why not use getPage?' and the answer is that it needs to return several different page elements which each require different snippets.

    thanks for any wisdom you can share,
    -g [ed. note: unsub777 last edited this post 11 years, 9 months ago.]
      • 3749
      • 24,544 Posts
      I'm probably missing something, but it really seems like getPage would be the way to go.

      You could build your output in a single snippet, using various methods and various Tpl chunks and just return the total output. If you specify that snippet in the getPage tag (along with any necessary properties for your snippe), getPage will pass the properties along to your snippet and will handle the pagination for you when the output is returned from your snippet.



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 36782
        • 109 Posts
        you know, I think you are right...

        as I was typing my "reason" above, it read like a rationalization of my faulty logic. I will build the page with a snippet, and then wrap that snippet. Hopefully the overhead won't be too harsh - my hosting S.U.C.K.S. for modx.

        On the bright side, I learned a little bit more about modx in the process; wasn't at all a waste smiley

        Thnx Bob
          • 3749
          • 24,544 Posts
          If you're getting the resources directly with a good query, it should be fairly fast (and it should be faster than doing it with getResources() ). getPage will add every little overhead.

          Sometimes, getIterator() is faster than getCollection() (same arguments), so that might be worth a try, but I would use getCollection() first.

          This will get all siblings of the current resource in one query (including the current resource):

          $resources = $modx->getCollection('modResource', array('parent' => $parentId));


          Another way to do the same thing (not sure which is faster):

          $parent = $modx->resource->getOne('Parent');
          if ($parent) {
              $resources = $parent->getMany('Children');    
          }
          



          (Note the capital letter in Parent and Children)



          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 11 years, 9 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 36782
            • 109 Posts
            Both of those examples are really helpful, thanks! Both of them contain things I didn't know about yet smiley

            Good to know about getPage, re: efficiency. The page I'm working on doesn't use any getResources calls, it's all tiny custom stuff, so should be fast. Even faster than before, actually -- thanks to your code examples above!

            cheers
              • 36782
              • 109 Posts
              ok, rebuilt (mostly - just the easy stuff now, don't show prev. for [0], etc.)
              This makes a lot more sense; I am getting the other fields I needed with a different method, and this is JUST a prev.< >next nav for siblings in a parent.

              I think it might be useful for another simpleton like myself at some point, so I'm posting my progress:
              (also I do love critique... hint...)
              <?php
              // need the parent id
              $parent = $modx->resource->get('parent');
              // make an array to fill with kids
              $ids=array();
              // need the current id
              $id = $modx->resource->get('id');
              // make sure that offset is getted, or make it 0 
              isset($_GET['oft'])?$oft = $_GET['oft']:$oft=0;
              
              $children = $modx->getChildIds($parent);
              foreach($children as $key=>$val)
              	array_push($ids,$val);
              // get the current array position
              $cur = array_search($id,$ids);
              // give me a clue (temporary)
              echo '<h4>'.$oft.'</h4>';
              // $_GET nasty with big ugly links + query strings
              /* unless Bob Ray has another pearl of wisdom... :)
              */
              return '<ul><li><a href="[[~'.$ids[$cur-1].']]&oft='.($cur-1).'">PREV</a><a href="[[~'.$ids[$cur+1].']]&oft='.($cur+1).'">NEXT</a></li></ul>';
                • 3749
                • 24,544 Posts
                Just one minor suggestion:


                $oft = isset($_GET['oft']) ? $_GET['oft'] : 0;


                Also, curly braces for your foreach() would make things easier to read. wink


                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.html
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 36782
                  • 109 Posts
                  Finished, working nicely. In case it's useful to someone:
                  <prev | next> navigation for sibling resources inside a common parent
                  Benefits:

                  • doesn't require any particular thing other than itself
                  • simple as Tinkertoys
                  Drawbacks:

                  • code is left-handed
                  Snippet code:SimpleSiblingNav
                  <?php
                  // need the parent id
                  $parent = $modx->resource->get('parent');
                  
                  // set the template
                  $tpl = isset($tpl)?$tpl:'Tpl.siblingNav';
                  
                  // make an array to fill with kids
                  $ids=array();
                  
                  // init the output
                  $output = '';
                  
                  // need the current id
                  $id = $modx->resource->get('id');
                  
                  // load up the kids
                  $children = $modx->getChildIds($parent);
                  
                  // get the current array position
                  $current = array_search($id,$children);
                  
                  // make $output array
                  $output_array=array(
                  	'count'	=>	count($children),
                  	'current'	=>	$current,
                  	'prev'		=>	$children[$current-1],
                  	'next'		=>	$children[$current+1],
                  ); 
                  
                  // output = chunk
                  if(count($children)>1){
                  	$output .= $modx->getChunk($tpl,$output_array);
                  };
                  
                  // return output
                  return $output;


                  Now the template: Tpl.siblingNav
                  <div class="pageNav">
                  <!-- class 'pageNav' for consistency with GetPage template -->
                  	<ul>
                  		[[+current:gt=`0`:then=`
                  		<li>
                  			<a href="[[~[[+prev]]]]">Previous</a>
                  		</li>
                  		`:default=``]]
                  		<li>
                  			<small>
                  				[[+current:increment]] of [[+count]]
                  			</small>
                  		</li>
                  		[[+current:lt=`[[+count:decrement]]`:then=`
                  		<li>
                  			<a href="[[~[[+next]]]]">Next</a>
                  		</li>
                  		`:default=``]]
                  </ul>


                  I think it's a sensible way to do what I needed. I am always very pleased to hear of new and better ways to do what I am doing, so please do let me know if this is pants.
                  [ed. note: unsub777 last edited this post 11 years, 9 months ago.]
                    • 3749
                    • 24,544 Posts
                    Looks great! I was going to suggest a Tpl chunk and I wondered if the foreach was necessary. wink



                    ------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using.
                    MODX info for everyone: http://bobsguides.com/modx.html
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting