We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    I am going thru Bob Ray's excellent tutorial on using custom database tables, and I have a question about the values returned. Here is the ShowQuotes snippet from the tutorial:

    <?php
    $path = MODX_CORE_PATH . 'components/extra/';
    $result = $modx->addPackage('extra',$path .
        'model/','bobs_extra_');
     
    if (! $result) {
        return 'failed to add package';
    } else {
        $output = '<p>added Package</p>';
    }
     
    $quotes = $modx->getCollection('Quotation');
     
    $output .= '<p>Total: '. count($quotes) . '</p>';
     
    foreach($quotes as $quote) {
        $fields = $quote->toArray();
        $output .= $modx->getChunk('ShowQuote', $fields);
    }
    
    return $output;


    Basically, what it is supposed to do is return some quotes with the related author, and that part is working great. My question is, let's say I have 8 quotes in my database, but I want to set a limit to only return 4, with a "Read more" link at the bottom to show all 8, where would I set the limit(4);? And additionally, should I add an } else { in here to display all if that link is clicked?

    Thank you.

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

    • discuss.answer
      • 3749
      • 24,544 Posts
      Try replacing this line:

      $quotes = $modx->getCollection('Quotation');


      with this:

      $query = $modx->newQuery('Quotation');
      $query->limit(4);
      $quotes = $modx->getCollection('Quotation', $query);
        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
        • 48586
        • 78 Posts
        Thank you very much, that did the trick, I really appreciate it.
          • 3749
          • 24,544 Posts
          Glad I could help. smiley
            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