We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    Background: I've been using Mark's excellent guide (https://www.markhamstra.com/xpdo/2011/preparing-custom-snippets-for-getpage) on how to paginate queried items from a custom snippet for quite some time now and have no problem paginating items that are in their own snippet.

    The issue I'm running into now is that I have a snippet that contains several complex queries, all being output to placeholders that we're using on-page.

    Right now, I'm trying to figure out how I can tie one of these queries in with getPage so that the placeholder can be paginated (or any variation of that).

    Any ideas?
      • 3749
      • 24,544 Posts
      One way would be to put that query in a separate snippet and put that in a dedicated getPage tag, but if that's not practical, it might work to call getPage() with $modx->runSnippet('getPage') in your snippet (with a custom snippet to do the query as an argument) when that query is made and write the return value to the placeholder.
        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
        • 33969
        • 60 Posts
        If you are writing the code for the snippet and the queries why not just do the pagination as well.

        I use this to setup a pagination function to display all the items in Gallery.:
        $numPhotos = $this->modx->getCount('galItem',array(
        	'active' => '1'
        ));
        		
        if($numPhotos > PHOTOS_PER_PAGE) {
             // $page is from the query string 
             // The '1' switches the pagination code to the photo page
        	$pagination = $this->getPagination($page, $numPhotos, $uri, '1');
        	$offset = ($page * PHOTOS_PER_PAGE) - PHOTOS_PER_PAGE;
        }


        The $offset is used in the query:
        $query->limit(PHOTOS_PER_PAGE,$offset);


        The pagination code is used by both my Photo and Video page - so setting it up for multiple queries isn't a problem. It isn't fancy - but it works. ( I don't use the search feature with the photos - so $issearch defaults to '0'. )

        private function getPagination($page, $numVideos, $uri, $isVideo = '0', $issearch = '0') {
        	if($isVideo == '0') {
        		$numPages = ceil(intval($numVideos)/VIDEOS_PER_PAGE);
        	} else {
        		$numPages = ceil(intval($numVideos)/PHOTOS_PER_PAGE);
        	}
        	
        	$pagination = 'Page: ';
        	for($idx=1; $idx <= $numPages; $idx++) {
        		if($idx == $page) {
        			$pagination .= "<b>" . $idx ."</b> | ";
        		} else if($issearch != '0') {
        			$pagination .= "<a href='" . $uri . "?page=" .$idx .
        				"&search_field=" . $issearch .  "'>" . $idx . "</a> | ";
        		} else {
        			$pagination .= "<a href='" . $uri . "?page=" .$idx . "'>" . $idx ."</a>  ";
        		}
        	}
        	
        		return "<p id='pagination'>" . $pagination . "</p>";
        }
        
          Michael Regan
          TIMR Web Services
          Phone: 250.218.5284
          Eamil: [email protected]
          Website: timr.ca
          • 1331
          • 129 Posts
          This approach looks like it should work with what I'm doing. I'll give it a shot and report back. Thanks!
            • 1331
            • 129 Posts
            @timrca it's not working out thus far. I've got all kinds of PHP errors for some of the code you're using here. Private function as well as using $this. I'll keep tweaking based on things that I know to be available to me in code.
              • 3749
              • 24,544 Posts
              tinrca's code is inside a class file. It should work if you remove the two instances of 'this->', remove 'private' in front of the function, and make sure the function is above the code that calls it.
                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