We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38666
    • 98 Posts
    After three days GetPage has me stumpted. Before I walk away from getPage altogether, does anyone have a complete working example of getPage usage with a custom database table (getPage tags +snippet + output tpl) that they would be willing to share? If I can see a working example perhaps I can make it work for me.

    Thanks
      Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.
      • 33968
      • 863 Posts
      It might be better if you show us what you're doing and we'll try to point out where it's falling over. Post your getPage snippet call for starters, as well as your custom snippet if possible.
        • 38666
        • 98 Posts
        Thanks Lucas

        I have a custom table (Members) that I would like to output with a custom snippet (ShowMembersPages) and getPage. This should be quite simple but I cannot see the forest for the trees here!

        My original snippet which gets the data from the Members table using xpdo looks like this.

        $csaac = $modx->getCollection('Members');
        foreach($csaac as $member) {
            $output .= '<p>' . $member->get('ID').':  '. $member->get('MemberName').' '.$member->get('Surname'). '</p>';
        }
        return $output;



        And gives me the output I want to see:

        1: Kazi Khairul Alam
        2: Lee-Anne Alde
        3: Harold Armstrong
        4: Mansour Asgahari
        5: Doug Ashton
        6: Shirley Baraniski


        But how do I do this using getPage? I have revised my snippet as shown by Mark Hamstra etal Preparing custom snippets for pagination with getPage http://www.markhamstra.com/modx-blog/2011/12/preparing-custom-snippets-for-getpage/ to look like this
        $tpl = $modx->getOption('tpl', $scriptProperties, '');
        $limit = $modx->getOption('limit',$scriptProperties,6);
        $offset = $modx->getOption('offset',$scriptProperties,0);
        $addToOffset = $modx->getOption('addToOffset',$scriptProperties,0);
        $offset = $offset + $addToOffset;
         
        $c = $modx->newQuery('Members');
         
        $total = $modx->getCount('Members',$c);
        echo $total;
        $total = $total - $addToOffset;
        $total = $total < 0 ? 0 : $total;
        
        $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
        $modx->setPlaceholder($totalVar,$total);
        
        $c->limit($limit,$offset);
        
        // echo "Found {$total} CSAAC members.";
        $collection = $modx->getCollection('Members',$c);
          
        foreach($collection as $item) {
            $vars = $item->toArray();
            $out[] = $modx->getChunk($tpl, $vars);
        }
          
        return implode("\n", $out);


        It is here that things begin to fall apart for me. How to I configure the call to getPage to output my data correctly?
        I have tried using various examples such as this on the forum to no avail.

        [[!getPage?
        &elementClass='modSnippet'
        &element='ShowMembersPages'
        &totalVar='total' 
        &tpl=`PagedMembers`
        ]]
        <div class="paging">
        <ul class="pageList">
          [[!+page.nav]]
        </ul>
        </div>


        I think my problem rests in my waking up to how to move the array $vars into a tpl chunk to give the output as in the original unpaginated query. Hope you can give me a nudge and help me see the preoblem.

        Thanks


        Thanks
          Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.
          • 33968
          • 863 Posts
          Before looking at this any further, you have some syntax errors in your getPage call. Change the apostrophes to backticks (`):

          [[!getPage?
            &elementClass=`modSnippet`
            &element=`ShowMembersPages`
            &totalVar=`total`
            &tpl=`PagedMembers`
          ]]
          <div class="paging">
          <ul class="pageList">
            [[!+page.nav]]
          </ul>
          </div>
          
            • 38666
            • 98 Posts
            Thanks Lucas, that is looking much better. I'll play with the formatting a bit but I think you have solved my problem.
              Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.