We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23094
    • 59 Posts
    i need include iteration only when PHx condition is true, i have code like this

    [+phx:if=`[[getField? &docid=`5` &field=`published`]]`:is=`1`:then=`<img src="photo[[iteration]]" />`+]
    [+phx:if=`[[getField? &docid=`10` &field=`published`]]`:is=`1`:then=`<img src="photo[[iteration]]" />`+]
    [+phx:if=`[[getField? &docid=`11` &field=`published`]]`:is=`1`:then=`<img src="photo[[iteration]]" />`+]
    

    iteration snippet:
    <?php
    $iterator+=1;
    return $iterator;
    ?>
    


    but iteration is always 1 why? when I call it [!iteration!] it is 1 all the time too
      • 3749
      • 24,544 Posts
      The variable goes out of scope at the end of the snippet so it’s always new.

      You can save it to a $_SESSION variable to make it persist.
        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
        • 23094
        • 59 Posts
        When I put it in $_SESSION, after page reload it will still count. I need variable to be 0 again when page i refreshed. Like in PHP:

        $counter = 0;
        if (some condition) $counter++;
        if (some condition) $counter++;
        if (some condition) $counter++;
        


        I need to display some blocks of content conditioned, all display must be numerated 1,2,3,4
        <DIV id="number1">some content</div>
        <DIV id="number2">some content</div>
        <DIV id="number3">some content</div>
          • 15705
          • 9 Posts
          Hi! I have the same problem. I need to iterate each <li> in phx calls. In short it’s something like:
          <ul>
          <li id=1>..
          <li id=2>..
          <li id=3>..
          </ul>

          I created two snippets:
          1. [[init]]
          <?php
          $_SESSION['iter']=1;
          return $_SESSION['iter'];
          ?>


          2. [[iteration]]
          <?php
          $_SESSION['iter']++;
          return $_SESSION['iter'];
          ?>


          And if I call them:
          [[init]]
          [[iteration]]
          [[iteration]]
          [[iteration]]

          I get:
          1
          2
          2
          2


          The magic didn’t happen. Maybe I missing something with working $_SESSION array or multiple snippet calls?
            ^^
            • 3749
            • 24,544 Posts
            Try:
            [[!iteration]]


            If you can explain exactly what you’re trying to accomplish, and why, there may be a better solution.
              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
              • 3749
              • 24,544 Posts
              @peter222: Put a snippet at the top of the page that zeros the session variable.
                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
                • 15705
                • 9 Posts
                Quote from: BobRay at Feb 28, 2011, 03:05 PM

                Try:
                [[!iteration]]


                If you can explain exactly what you’re trying to accomplish, and why, there may be a better solution.
                It doesn’t work sad

                I’m making a product sheet page for a e-shop website. Product specs are layered in <ul><li>:
                <ul>
                	<li>             Spec 1</li>
                
                	<li class="even">Spec 2</li>
                
                	<li>             Spec 3</li>
                
                	<li class="even">Spec 4</li>
                </ul>
                

                Every "even" <li> is even smiley and has it’s own style (gray background). Furthermore not all "Specs" are available for current product. For example product A has Spec1 and Spec3 listed and product B has Spec 2,Spec3,Spec4 etc.
                So I need to do 2 steps:
                1. Check if a Spec available (filled) for current product.
                2. Find every even <li> and assign a class "even" to it.

                My steps to achieve that would be something like this:
                <ul>
                [*phx:if=`[*Spec1*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec1*]</li>`:else=``*]
                [*phx:if=`[*Spec2*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec2*]</li>`:else=``*]
                [*phx:if=`[*Spec3*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec3*]</li>`:else=``*]
                [*phx:if=`[*Spec4*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec4*]</li>`:else=``*]
                </ul>
                


                where [[iterator]] is a snippet which should return an incremented value by 1 each time this snippet is called (so I can count every <li> and check if it’s even by "%2")
                But this snippet doesn’t work properly. If I call it multiple times I have the same value in each call (as I posted above). I would be grateful for any suitable solution smiley
                  ^^
                  • 3749
                  • 24,544 Posts
                  In revolution, there is a modulus output modifier that returns 0 or 1. It defaults to %2 so [[Iterator]]:modulus should be 0 or 1. I’m not sure if it exists in Evolution, but it’s worth a try.

                  The modifier in Revo can be referred to as :modulus or :mod.
                    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
                    • 15705
                    • 9 Posts
                    Quote from: BobRay at Mar 01, 2011, 09:01 PM

                    In revolution, there is a modulus output modifier that returns 0 or 1. It defaults to %2 so [[Iterator]]:modulus should be 0 or 1. I’m not sure if it exists in Evolution, but it’s worth a try.

                    The modifier in Revo can be referred to as :modulus or :mod.

                    Yeah but the problem in [[iterator]] snippet which is basically:
                    <?php
                    $_SESSION['iter']++;
                    return $_SESSION['iter'];
                    ?>
                    


                    If I call [[iterator]] snippet multiple times on a page like:
                    <ul>
                    [*phx:if=`[*Spec1*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec1*]</li>`:else=``*]
                    [*phx:if=`[*Spec2*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec2*]</li>`:else=``*]
                    [*phx:if=`[*Spec3*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec3*]</li>`:else=``*]
                    [*phx:if=`[*Spec4*]`:ne=``:then=`<li [*phx:if=`[[iterator]]%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec4*]</li>`:else=``*]
                    </ul>

                    .. the result will be equal in all calls like:
                    <ul>
                    [*phx:if=`[*Spec1*]`:ne=``:then=`<li [*phx:if=`1%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec1*]</li>`:else=``*]
                    [*phx:if=`[*Spec2*]`:ne=``:then=`<li [*phx:if=`1%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec2*]</li>`:else=``*]
                    [*phx:if=`[*Spec3*]`:ne=``:then=`<li [*phx:if=`1%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec3*]</li>`:else=``*]
                    [*phx:if=`[*Spec4*]`:ne=``:then=`<li [*phx:if=`1%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec4*]</li>`:else=``*]
                    </ul>
                    

                    and not as I expect:
                    <ul>
                    [*phx:if=`[*Spec1*]`:ne=``:then=`<li [*phx:if=`1%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec1*]</li>`:else=``*]
                    [*phx:if=`[*Spec2*]`:ne=``:then=`<li [*phx:if=`2%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec2*]</li>`:else=``*]
                    [*phx:if=`[*Spec3*]`:ne=``:then=`<li [*phx:if=`3%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec3*]</li>`:else=``*]
                    [*phx:if=`[*Spec4*]`:ne=``:then=`<li [*phx:if=`4%2`:is=`0`:then=`class="even"`:else=``*] >[*Spec4*]</li>`:else=``*]
                    </ul>


                    And using modulus won’t solve the problem because each next number given to modifier is equal to a previous one.
                      ^^
                      • 3749
                      • 24,544 Posts
                      I think you’ll need to write your own snippet with its own internal iterator. You can send a comma-delimited list of the TV names and just iterate through them, adding to the output and, for every other one, adding the class name, using if($iterator%2). It will be a lot faster than using PHX. PHX conditionals are very slow and hog a lot of resources.

                      It would look something like this

                      [!Iterator? &specs=`Spec1,Spec2,spec3,Spec4`!]


                      <?php
                      /*Iterator snippet */
                      
                      $iterator=0;
                      $allSpecs = explode(',', $specs);
                      $output='';
                      
                      foreach($allSpecs as $spec) {
                          $output .= '<li';
                          if ($iterator%2) {
                              $output .= ' class="even "';
                          }
                          $iterator ++;
                          $output .= '>[*' . $spec . '*]</li>' . "\n";
                      }
                      return $output;
                      
                        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