We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41710
    • 18 Posts
    I am trying to use getPage to paginate my snippet that using xPDO. I finally have the snippet pulling the data but it isn't using the "limit" or paginating. Here is the call:

      [[!getPage?
       &elementClass=`modSnippet`
       &element=`usaNews`
     
       &limit=`10`
       &pageVarKey=`page`
    ]]
    <div class="paging">
    <ul class="pageList">
      [[!+page.nav]]
    </ul>
    </div>
    


    And here is the snippet:

    // Issue queries against the foreign database:
    $query= $xpdo->query("SELECT * FROM `modx_site_content` WHERE `parent` = 7 ORDER BY pub_date DESC"); 
    
    $i = 0;
    $output = '';
    //$properties = $query->toArray();
    foreach ( $query as $row){
     
    $properties['usaLocation'] = $row['pagetitle'];
    $properties['usaDescription'] = $row['description'];
    $properties['usaDate'] = $row['pub_date'];
    $properties['usaURL'] = $row['content'];
     
    $output = $modx->getChunk('usaNews',$properties);
    echo $output;
    $i++;
    }
      • 22303 MODX Staff
      • 10,725 Posts
      You need two things in your Snippet to use getPage for pagination:

      1. Your snippet must set a placeholder with the key specified in the &totalVar property, e.g. $modx->setPlaceholder($totalVar, $totalNumberOfRecords). This means you must issue a query which returns the count of the total number of records that would be returned by your selection criteria to get the value to set in this placeholder.

      2. Your snippet must accept a &limit and &offset property, which you will use in your existing query to select the limited results to return for a specific page request. IOW, your query must use these properties in a LIMIT/OFFSET clause.
        • 40706
        • 128 Posts
        You are not using really the benefits of XPDO when you just passing Querys to xpdo->query()

        You could use instead the "MODx way" to get this resources like that, with the Use of getCollection, getCount

        <?php
        
        // Issue queries against the foreign database:
        $conditions = $modx->newQuery('modResource');
        $conditions->where(array('parent' => 7));
        
        // getPage Stuff
        $total = $modx->getCount('modResource', $conditions);
        $modx->setPlaceholder('total', $total);
        $conditions->limit($limit, $offset);
        $conditions->sortby('pub_date', 'DESC');
        
        $output = '';
        $resources = $modx->getCollection('modResource', $conditions);
        foreach ( $resources as $resource){
            $row = $resource->toArray();
            $properties['usaLocation'] = $row['pagetitle'];
            $properties['usaDescription'] = $row['description'];
            $properties['usaDate'] = $row['pub_date'];
            $properties['usaURL'] = $row['content'];
        
            $output .= $modx->getChunk('usaNews',$properties);
        }
        return $output;
        


        just write it down, hope that there are no typos