⚠️ Urgent! Active Attacks on MODX Revolution Sites Below Revolution 2.6.5
Subscribe: RSS
  • Revo 2.6.1

    Using pdoResources / pdoPage (and pagination controls) to show a listing of resources is there a way to allow a visitor to select the number of results to display using a picker?

    Right now I'm using the &limit=`10` property but it's static, I would prefer the value be set from the front-end. For example, the default value might be 10 with additional picker options of 15, 20, Show All. And changing the value would automatically refresh the page.
      Todd
    • I think you could do it with a user setting called results_to_show. You could put a mini form on the page with the number of results to show, and when it's submitted, save the value to a User Setting.

      Then use this in the pdoResources call:

      &limit=`[[!++results_to_show]]`


      The code to save the value would look something like this:

      if (isset($_POST['results_to_show']) && isset($_POST['submitValue'])) {
          $userId = $modx->user->get('id');
          $key = 'results_to_show';
          $value = $_POST['results_to_show'];
      
          $setting = $modx->getObject('modUserSetting', array('user' => $userId, 'key' => $key));
      
          if (! $setting) {
              $setting = $modx->newObject('modUserSetting');
              $setting->set('user', $userId);
              $setting->set('key', 'results_to_show');
          }
      
          $setting->set('value', $value);
          $setting->save();
      }
      return;



      The value could also be stored in an extended field of the user profile, or in an unused user or profile field, but that would require using another snippet or tag to retrieve the value.
        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
      • Thanks @BobRay, I'll take a run at this and see how I fare.
          Todd
        • @BobRay, I'm curious as to why this would need to be stored as a user-setting at all. While it's likely I would use this on pages accessible only to registered users, it would mostly be used on public-facing pages.
            Todd
          • The user setting doesn't seem needed to me either. Just add a simple form, grab the value ($_POST['results_to_show'] in Bob's example), and apply the limit to your snippet call.
              Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with remote management features to make the MODX world a little better.

              Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
            • I assumed that you wouldn't want to reload the page every time the user changed the value or have the value revert to the default on every visit.
                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
              • Oh, that's a nice touch. I could use that on adifferent site. Thank you.

                I seem to be having trouble getting the stripped-down version Mark suggested to work.

                I created a basic form:
                <form method="post">
                    <select>
                        <option value="10">10</option>
                        <option value="20">20</option>
                        <option value="0">View All</option>
                    </select>
                </form>

                I set &limit=`[[!++results_to_show]]` and created a snippet named "results_to_show" then added the code you provided but nothing happens. Since I don't need to store anything I'm unsure how to modify the code you provided.

                Looking at Mark's example above is this all that's required?
                ($_POST['results_to_show'])
                  Todd
                • Looking at Mark's example above is this all that's required?
                  ($_POST['results_to_show'])

                  Yes, but you need an input field with name="results_to_show" in order for that value to end up in the $_POST array.
                    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
                  • I added the input field but it's still not working. Not sure what I'm doing wrong.

                    Thanks BobRay and Mark H., I'll take another look at it when I have more time.
                      Todd
                    • In your snippet you'd need to do something like this:

                      <?php
                      $value = 10;
                      
                      if (array_key_exists('results_to_show', $_POST) && is_numeric($_POST['results_to_show'])) {
                        $value = (int)$_POST['results_to_show'];
                      }
                      
                      // make it available as [[!+results_to_show]]
                      $this->setPlaceholder('results_to_show', $value);
                      // OR
                      // if you want to use the snippet inside the call directly
                      return $value;
                      
                      
                        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with remote management features to make the MODX world a little better.

                        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.