We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1331
    • 129 Posts
    Ok, I know this should be super easy, but I'm at the very beginning stages of PHP programming. At the moment, I'm using Rowboat to grab the value of a column in my MODX db. But I know that this is probably going to cause performance issues when I start looping through tons of rows.

    So, here's the scenario: I have set up a custom table that has an ID and Name columns. The ID is part of a query string that gets passed to the browser like mysweetsite.com/customers/?customer=12.

    I'm trying to write a snippet that I can simply place on the page that will set the placeholder [[+customer_name]], which will grab the value from the name column based on that param in the URL.

    Rowboat (plus some other things) is working. But what would be the simplest PHP/MODX or XPDO way to do this?

    This question has been answered by BobRay. See the first response.

    • discuss.answer
      • 3749
      • 24,544 Posts
      This should be a lot faster than RowBoat. Change 'modWhatever' to the name of your table's class.

      <?php
      $id = $modx->getOption('customer', $_GET, null);
      if ($id !== null) {
          $obj = $modx->getObject('modWhatever', $id);
          if ($obj) {
              $modx->setPlaceholder('customer_name', $obj->get('Name') ); // 'name' ?
          } else {
              $modx->log(MODX::LOG_LEVEL_ERROR, 'Customer Not Found');   
         }
      } else {
         $modx->log(MODX::LOG_LEVEL_ERROR, 'No get parameter');   
      }
      


      This assumes that you've set up your custom table to work with xPDO. If not, see this: http://bobsguides.com/custom-db-tables.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
        • 1331
        • 129 Posts
        Thanks Bob,

        I'll try this out after I get the table set up with XPDO. Could MIGBdb help speed that process up?
          • 3749
          • 24,544 Posts
          I'm not that familiar with MIGXdb, but I believe it would be a whole different approach. You would not use the code I posted above.

          It might be easier, but I don't know because I haven't used it. I also don't know about its speed compared to straight xPDO.
            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
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            MIGx provides the migxLoopCollection snippet that can fetch just about any rows of whatever you want. I still haven't worked through all of its possible properties yet.
              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
              • 1331
              • 129 Posts
              Hey @bob, thanks for the code snippet and also for the link. After reading your primer on xPDO, I was able to make sense of the example you have here and really get things moving! The xPDO version i 16 times faster than what I was doing.

              Also, I was able to use the package that I had created earlier with the MIGXdb component. But reading your information helped me understand what all was going on there.

              Very exciting stuff!
                • 3749
                • 24,544 Posts
                I'm glad you got it working, and that it's so much faster.
                  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