• Making ditto_iteration consecutive across multiple pages#

  • firebot6 Reply #1, 7 months, 3 weeks ago

    Reply
    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.


  • kp52 Reply #2, 7 months, 2 weeks ago

    Reply
    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


  • flinx777 Reply #3, 7 months ago

    Reply
    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?


  • Everett Reply #4, 7 months ago

    Reply
    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.


  • flinx777 Reply #5, 7 months ago

    Reply
    thnx so much Everett...that was exactly what i needed. Gracias!


  • firebot6 Reply #6, 7 months ago

    Reply
    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.


  • flinx777 Reply #7, 7 months ago

    Reply
    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.