We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10525
    • 247 Posts
    I am trying to use a php class for pagination of db query results.

    The class constructor requires a db connection to be passed in.
    class pagination {
    	var $conn = false;
    	...
    
    	function pagination($connection, $sql, $rows) {
    		$this->conn = $connection;
    		...
    	}
    	
    	...
    }

    I would like to use the existing db connection that already persists in my instance of modx.

    I was thinking either:

    1. alter the class so that I somehow use "global $modx;" from inside it (as I do in all my snippets). Or,

    2. just get the connection from the current $modx and pass that into the constructor.

    Can anyone tell me how I should be doing this?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I would have my snippet that returns the rows from the database, then use getPage or pdoPage to do the paging.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 10525
        • 247 Posts
        Thanks for that Susan. pdoTools looks interesting. Any idea where I can find user instructions (in English)? As far as I can see these tools are for calling fron chunks. Can I access them from within php?
          • 10525
          • 247 Posts
          OK, I think getPage is designed more for dealing with modx resources as opposed to database queries (which are then displayed in a table on a page), so I think I need to stick to my original question (unless I can find clear docs for pdoTools).

          So, how to access the modx db connection which is already open? Can I do this for the class above?
            • 4172
            • 5,888 Posts
            you can use getPage or pdoPage not only for Resources, but also to paginate items of any custom-tables together with rowboat, pdoResources, migxLoopCollection and others.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Indeed. getPage and pdoPage don't care what listing snippet you are using. All they do is run the specified snippet with the appropriate offset and limit properties.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 24,544 Posts
                FYI, the $modx variable is a DB handle to the MODX database, so you should be able to do something like this:

                $res = $modx->query("SELECT * FROM some_table");
                $records = $res->fetch(PDO::FETCH_ASSOC);  
                

                  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
                  • 10525
                  • 247 Posts
                  Thanks Bruno, Susan. I will take another look at getPage and pdoPage, but I find (as I say about a lot of stuff) the getting started docs a bit thin/impenetrable for a quick-start tryout. Know of any tutorials/docs which describe initial setup of either? I'm just querying a custom table of items (products or orders) in the modx db in order to list them.

                  Bob, yes I thought that and did try simply passing $modx to the class constructor. There is probably something else in the code which is tripping it.
                    • 10525
                    • 247 Posts
                    OK I think I'm rolling. I was trying to do
                    var $conn = $modx;
                    first thing in the class, where variables are declared. However, simply sticking that in the constructor works fine, meaning no need to explicitly pass $modx.

                    It's such a long time since I developed in Java that the 'OOP' part of my brain seems to have atrophied to 'OOPS'... ;-)