We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36760
    • 136 Posts
    Running Modx Evo 1.0.5, and Ditto 2.1.0

    I'm trying to help someone with a gallery they've setup with Ditto. The simplest solution would be if there is a way to make the ditto_iteration call be consecutive across pages.

    For example, there are four elements per page on the gallery. So the iterations are 0-3 on the first page, and 0-3 on the second page. Is there a way to make it 0-3 on the first page, and then 4-7 on the second page and so on?

    I know in the documentation ditto_iteration specifically says "within current page." Though I was hoping there may be a way to modify it.

    Here is what the gallery code looks like. I need the numbers to be consecutive so that when a user clicks the link it brings them to the correct gallery page.

    			<li>
    			<a href="[~[+id+]~]?gallery_start=[+ditto_iteration+]"><img src="[+patient_image1+]" width="291" class="thumbnail" title="[+pagetitle+]" alt="[+pagetitle+]" /></a>
    			<p style="width:291px;">[+pagetitle+]</p>
    			</li>


    I'm not sure if this was the best solution for a gallery, but since most of the work is already done besides this small problem, I thought I would try to salvage it.

    I appreciate any help.
      • 16278
      • 928 Posts
      You can do some arithmetic using the PHx math modifier acting on |+currentPage+|, |+ditto_iteration+| and the value in &display. My test template looked like this for pagination in pages of three items:
      Iteration = [+ditto_iteration+], current page = [+currentPage+],
      overall position = [+currentPage:math=`(?-1)*3+[+ditto_iteration+]+1`+]<br>
      

      Output for second page:
      Iteration = 0, current page = 2, overall position = 4
      Iteration = 1, current page = 2, overall position = 5
      Iteration = 2, current page = 2, overall position = 6


      You can use a TV for the &display parameter to make it more general-purpose and maintainable. E.g. with &display=`|*perPage*|`:
      Iteration = [+ditto_iteration+], current page = [+currentPage+],
      overall position = [+currentPage:math=`(?-1)*[*perPage*]+[+ditto_iteration+]+1`+]
      

      :) KP
      • Hey kp52,

        I'm working on the same problem. I used your solution:

        Iteration = [+ditto_iteration+], current page = [+currentPage+],
        overall position = [+currentPage:math=`(?-1)*3+[+ditto_iteration+]+1`+]


        But unfortunately [+ditto_iteration+] does not output in that PHx math equation you wrote. Know how to get around that?
          Precision Web Development ... SmashStack.com
        • The problem is not with the [+ditto_iteration+] placeholder, it's with the [+currentPage+] placeholder... that's coming out blank.... in the source code, the placeholder looks like it's actually supposed to include a prefix of the &id parameter, e.g. if your call was
          [[Ditto? &id=`something` ... ]]
          then the current page placeholder is uses the &id as a prefix, e.g. [+somethingcurrentPage+], but it's still not parsing... so that looks like a bug.

          Just as a note, here's the line from the ditto class file, around line 1130:
          $modx->setPlaceholder($dittoID."currentPage", $display);


          A workaround here is to tie into the URL parameter that's passed in the pagination links and just use a Snippet to read that number and add the local iteration from [+ditto_iteration+] to it. The basic problem here is that on each page of results, [+ditto_iteration+] returns only the local iteration, e.g. if you are displaying only 4 results per page, the [+ditto_iteration+] will be 0, 1, 2, or 3. What you are trying to achieve here is the global iteration, i.e. on the first page, you want 0,1,2, or 3, but on the second page, you want 4,5,6 or 7.

          Since Ditto's placeholder for this is either borked or incorrectly documented, we need to work around it with a Snippet.

          Here's my Snippet, named getGlobalIteration:

          <?php
          /*------------------------------------------------------------------------------
          NAME: getGlobalIteration
          
          DESCRIPTION:
          This grabs a parameter from the url, e.g. &gallery_start, and then does simple 
          math to determine the iteration number of an item in the grand scheme of results, 
          i.e. the "global iteration"
          
          See http://forums.modx.com/thread/70785/making-ditto-iteration-consecutive-across-multiple-pages
          
          AUTHOR: Everett Griffiths www.fireproofsocks.com
          
          @param string  $key: the key in the $_GET array that contains the current offset
          @param integer $i: the current iteration (from [+ditto_iteration+]
          @return integer the iteration for this item amongst *all* results
          ------------------------------------------------------------------------------*/
          if (!isset($key)) {
            $key = 'gallery_start';
          }
          $local_iteration = 0;
          if (isset($i)) {
             $local_iteration = (int) $i;
          }
          
          $start = 0;
          if (isset($_GET['gallery_start'])) {
             $start = (int) $_GET['gallery_start'];
          }
          
          return $start + $i;
          ?>


          So I can just put that into my chunks referenced by Ditto's &tpl parameters. E.g. here's my tpl chunk:

          <li>
          	<a id="link[+id+]" href="[~[+id+]~]?gallery_start=[!getGlobalIteration? &i=`[+ditto_iteration+]`!]"><img src="[+patient_image1+]" width="291" class="thumbnail" title="[+pagetitle+]" alt="[+pagetitle+]" /></a>
          	<p style="width:291px;">[+pagetitle+]</p>
          </li>


          Note that the URL parameter "gallery_start" was used because the Ditto call used &id=`gallery`. If you are using a different &id parameter for ditto, you can adjust the call to getGlobalIteration by specifying the &key parameter.

          Hope this helps. [ed. note: Everettg_99 last edited this post 12 years, 6 months ago.]
          • thnx so much Everett...that was exactly what i needed. Gracias!
              Precision Web Development ... SmashStack.com
              • 36760
              • 136 Posts
              The project I was working on this for originally kind of stalled out. Though I'm glad someone else was able to get use out of the discussion! If the project starts back up again, I'll be back.
              • Hey Firebot...I think we were working on the same project. Your code in the example above was the exact code down to the "width:291px" and all. I just spoke with the client and they're pushing it live this weekend.
                  Precision Web Development ... SmashStack.com
                  • 9995
                  • 1,613 Posts
                  I can't get this to work on my page.

                  I show paging with 9 items per page.
                  On page one the items have a link with ?start=0,
                  on the second page the item links need to have ?start=9 etc.

                  My ditto call:
                  [!Ditto?
                  &parents=`5`
                  &total=`99`
                  &showPublishedOnly=`1`
                  &orderBy=`menuindex ASC`
                  &tpl=`detail`
                  &tplCurrentDocument=`detail-actief`
                  &summarize=`9`
                  &paginate=`1`
                  &paginateAlwaysShowLinks=`1`
                  !]
                   
                  <div class="ditto_pages">Page [+pages+]</div>


                  any cluess?


                  ###
                  When I use: [[getGlobalIteration? &i=`[+ditto_iteration+]`]]
                  i do get better counting, item have: 1,2,3,4,5,6,7,8,9|page2|10,11,etc instead of [+ditto_iteration+] which has 1,2,3,4,5,6,7,8,9|page2|1,2,3,4,5etc [ed. note: fourroses666 last edited this post 10 years, 11 months ago.]
                    Evolution user, I like the back-end speed and simplicity smiley