We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44195
    • 293 Posts
    Hi,

    I'm trying to paginate the results from one of my tables into different panes of the jquery widget "liquid slider".

    Getting the results into the panes isn't a problem but I'd like to calculate how many slides are required and dynamically generate them.

    The only way I can think of to do this is to use a snippet to calculate the total (I need six on each pane) output the divs through a chunk/tpl which in turn calls another snippet which will output the content for each pane..

    Is there a better way? If not, maybe someone can spot where I'm going wrong.

    Here's my snippet that gets the results from the database - it's called 'getLatestGallery':
    <?php
    $mycomponent = $modx->getService('mycomponent','MyComponent',$modx->getOption('mycomponent.core_path',null,$modx->getOption('core_path').'components/mycomponent/').'model/mycomponent/',$scriptProperties);
    if (!($mycomponent instanceof MyComponent)) return '';
    
    /* setup default properties */
    $tpl = $modx->getOption('tpl',$scriptProperties,'currentGalleryFirstPage');
    $sort = $modx->getOption('sort',$scriptProperties,'id');
    $dir = $modx->getOption('dir',$scriptProperties,'ASC');
    $limit = $modx->getOption('limit',$scriptProperties,6);
    $offset = $modx->getOption('offset',$scriptProperties,0);
    $totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
    
    $record = $modx->query("SELECT MAX(id) FROM {$modx->getTableName('galleries')} WHERE phase=0");
    $highestId = (integer) $record->fetch(PDO::FETCH_COLUMN);
    $record->closeCursor();
    
    $galleryId = $highestId;
    // Get total number of records
    $c = $modx->newQuery('artworks');
    if(!empty($galleryId)) {
        $c->where(array(
            'gallery_id' => $galleryId
        ));
    }
    $total = $modx->getCount('artworks',$c);
    $modx->setPlaceholder($totalVar,$total);
    
    $c->limit($limit,$offset);
    $c->sortby($sort,$dir);
    $artworks = $modx->getCollection('artworks',$c);
    
    $output = '';
    foreach ($artworks as $artwork) {
        $artworkArray = $artwork->toArray();
        $output .= $sovereign->getChunk($tpl,$artworkArray);
    }
    return $output;
    

    This snippet populates the first pane nicely, but basically if there are more than 6 artworks I'd like to generate the other panes dynamically.

    Here's the tpl that goes with it 'currentGalleryFirstPage'
    <a href="#" data-id="[[+id]]" class="gallery_item fancybox-gallery fancybox.inline" rel="gallery">
        <img src="[[++assets_url]]components/mycomponent/galleries/[[+gallery_id]]/thumbnails/[[+filename]]_medium.jpeg" alt="[[+art_title]]" />
        <div class="item_curtain">
            <h1>[[+art_title]]</h1>
            <h2>[[+title]] [[+first_name]] [[+surname]]</h2>
        </div>
    </a>
    



    Now here's where things start to get complicated:
    This is a snippet I've been playing around with to try and divide the total number of artworks by 6 to get the required number of panes. I then tried to create an array with the appropriate number of elements(same as panes) and output a template that many times which calls my original snippet to populate the pane. Only thing is I think I'm mucking up the getChunk call or my logic is off.
    <?php
    $mycomponent = $modx->getService('mycomponent','MyComponent',$modx->getOption('mycomponent.core_path',null,$modx->getOption('core_path').'components/mycomponent/').'model/mycomponent/',$scriptProperties);
    if (!($mycomponent instanceof MyComponent)) return '';
    
    /* setup default properties */
    $tpl = $modx->getOption('tpl',$scriptProperties,'currentGallerySplitSlides');
    
    $record = $modx->query("SELECT MAX(id) FROM {$modx->getTableName('galleries')} WHERE phase=0");
    $highestId = (integer) $record->fetch(PDO::FETCH_COLUMN);
    $record->closeCursor();
    
    $galleryId = $highestId;
    // Get total number of records
    $c = $modx->newQuery('artworks');
    if(!empty($galleryId)) {
        $c->where(array(
            'gallery_id' => $galleryId
        ));
    }
    $total = $modx->getCount('artworks',$c);
    
    //Divide total by 6 and round up to get the number of slides required
    if($total < 7) {
        $numOfSlides = 1;
    } else {
        $numOfSlides = ceil($total/6);
    }
    
    // now I need to somehow set placeholders for [[+slideNum]] and [[+offset]] in the tpl below but they need to be different for each iteration!
    
    



    Here's an example of where I'm calling my original snippet but I need some placeholder data which I'm not too sure how to generate:
    <div>
        <h2 class="title">Slide [[+slideNum]]</h2>
        [[!getLatestGallery? &limit=`6` &offset=`[[+offset]]` &tpl=`currentGalleryOtherPage`]]
    </div>
    
    



    So basically I think my question boils down to: How can I set placeholders that are different for each iteration?
    Or am I going about this completely the wrong way?

    I'd love some insight if anyone has any! Cheers.

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

      I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
      Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com
    • discuss.answer
      • 4172
      • 5,888 Posts
      if you would use pdoResources you could try something like that:

      [[pdoResources?
        &loadModels=`mycomponent`
        &class=`artworks`
        &where=`{"gallery_id":"[[getTheGalleryIDhere]]"}`
        &tpl=`galleryItemTpl`
        &tpl_n6=`galleryItemTpl_6`
        &tpl_n7=`galleryItemTpl_7`
        &tplLast=`galleryItemTpl_last`
      ]]


      galleryItemTpl
      [[+idx:is=`1`:then=`
      <div>
          <h2 class="title">Slide</h2>
      `:else=``]]  
      
      [[$galleryItem? &idx=`[[+idx]]`]]
      


      galleryItemTpl_6
      [[$galleryItem? &idx=`[[+idx]]`]]
      </div> 
      


      galleryItemTpl_7
      <div>
          <h2 class="title">Slide</h2>
      [[$galleryItem? &idx=`[[+idx]]`]]
      


      galleryItemTpl_last
      [[+idx:is=`1`:then=`
      <div>
          <h2 class="title">Slide</h2>
      `:else=``]]  
      
      [[$galleryItem? &idx=`[[+idx]]`]]
      </div>
      



      galleryItem
      <a href="#" data-id="[[+id]]" class="gallery_item fancybox-gallery fancybox.inline" rel="gallery">
          <img src="[[++assets_url]]components/mycomponent/galleries/[[+gallery_id]]/thumbnails/[[+filename]]_medium.jpeg" alt="[[+art_title]]" />
          <div class="item_curtain">
              <h1>[[+art_title]]</h1>
              <h2>[[+title]] [[+first_name]] [[+surname]]</h2>
          </div>
      </a>
      


      or put the same functionality into your snippet, see also getImageList, where addional [[+_first]] and [[+_last]] - placeholders are availiable, which I was missing in pdoTools


        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 44195
        • 293 Posts
        Cheers Bruno!
        I'll have a look into pdotools and getimagelist, then give this a go!

        So this would allow me to page output with different offsets on dynamically generated parts of the page?
        What is the 'idx' referring to?
          I'm lead developer at Digital Penguin Creative Studio in Hong Kong. https://www.digitalpenguin.hk
          Check out the MODX tutorial series on my blog at https://www.hkwebdeveloper.com