Announcement

Collapse
No announcement yet.

Filter a list/array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Filter a list/array

    I've been working on a feature that tests for affiliate codes in the referring URL, and reached a point where I'm satisfied with the results -- The Thread

    Now I have the list of affiliates loading into a variable and I'm wanting to make a new list/array of the Affiliate Codes based on "Status". I know I can read the value, but I'm stuck pushing the Affiliate Code into a new list/array.

    Here's my code so far. http://paste.mozilla.org/6QEWgVYg
    Last edited by cpp-dev; 07-10-20, 10:36 AM.

    #2
    Naturally I've been digging around in the Miva Docs, and came across this listing for miva_array_filter -- https://docs.miva.com/mivascript/miva_array_filter

    It looks like it will do what I want, but I'm not certain which tokens/variables it wants to operate correctly.

    Comment


      #3
      I can load the affiliates without issue --
      Code:
          <mvt:comment> load the affiliates into a list </mvt:comment>
          <mvt:do file="g.Module_Feature_AFF_DB" name="l.success" value="AffiliateList_Load_All(l.settings:affiliate_list)" />
      But then doing something with the above array/list is the stumbling block
      Code:
          <mvt:comment> Add the affiliate code to an array/list for futher use </mvt:comment>
          <mvt:foreach iterator="affiliate" array="affiliate_list">
            <mvt:if expr="l.settings:affiliate:status EQ 'Approved'">
              <mvt:assign name="l.settings:approved_affiliate" value="l.settings:affiliate:code" />
            </mvt:if>
          </mvt:foreach>

      Comment


        #4
        Originally posted by cpp-dev View Post
        Naturally I've been digging around in the Miva Docs, and came across this listing for miva_array_filter -- https://docs.miva.com/mivascript/miva_array_filter

        It looks like it will do what I want, but I'm not certain which tokens/variables it wants to operate correctly.
        mivascript.com generally has more info on syntax and parameters

        http://www.mivascript.com/item/miva_array_filter.html

        Bruce Golub
        Phosphor Media - "Your Success is our Business"

        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
        phosphormedia.com

        Comment


          #5
          When I try to save the following I get an error:
          Code:
          <div class="wrap">
          <mvt:comment> load the affiliates into a list </mvt:comment>
          <mvt:do file="g.Module_Feature_AFF_DB"name="l.success"value="AffiliateList_Load_All(l.settings:affiliate_list)" />
          <mvt:comment> get the affiliate code </mvt:comment>
          <mvt:assign name="g.affiliate_string"value="s.query_string" />
          <mvt:assign name="g.affiliate_code"value="substring( g.affiliate_string, 12, len(g.affiliate_string) )" />
          &mvte:global:affiliate_code;
          <mvt:eval expr="miva_array_filter( l.affiliate_list, 1, l.affiliate:status, 'l.affiliate:status EQ 'Approved'', l.approved_affiliate_list )" />
          
          
          ...</div>
          The error text is
          Error compiling template: Line 10: Expression compilation failed: Found additional argument 'A' while looking for operator
          The variables and selectors should be valid, as I get output when I use the following:
          Code:
          <mvt:comment> Writing out the values to check if it's working </mvt:comment>
          <ul>
          <mvt:foreach iterator="affiliate"array="affiliate_list">
          <li>
                  Affiliate: &mvte:affiliate:code; - &mvte:affiliate:status;
          </li>
          </mvt:foreach>
          </ul>
          When I look at the docs for the "miva_array_filter" it seems to be correct. -- http://www.mivascript.com/item/array...ay_filter.html

          I'm really not sure where I'm going wrong.

          The "filter_expression" near as I can tell should be correct, as that is how I've used the 'expression' syntax before.

          === 20200713-1237 ===

          Hold the phone ...
          Code:
          <mvt:eval expr="miva_array_filter( l.settings:affiliate_list, 1, l.settings:affiliate:status, 'l.affiliate:status EQ Approved', l.settings:approved_affiliate_list )"/>
          When I used the above I get output from this:
          Code:
          <ul>
          <mvt:foreach iterator="affiliate"array="approved_affiliate_list">
          <li>
                 Affiliate: &mvte:affiliate:code; - &mvte:affiliate:status;
          </li>
          </mvt:foreach>
          </ul>
          But I'm still not seeing the information be filtered.
          Last edited by cpp-dev; 07-13-20, 11:46 AM.

          Comment


            #6
            Try using a different iterator other than 'affiliate', also are you sure the status is literally 'Approved'?
            Bruce Golub
            Phosphor Media - "Your Success is our Business"

            Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
            phosphormedia.com

            Comment


              #7
              I'm pretty sure about the status:
              img 1 -- https://cloudup.com/iZPg01Gnahr
              img 2 -- https://cloudup.com/iw1PePO6HrD

              img 3 -- the current code - since the code blocks don't like something about my VSode setup -- https://cloudup.com/iP78NB-BBBE

              Comment


                #8
                try this:

                Code:
                <mvt:eval expr="miva_array_filter( l.settings:affiliate_list, 1, l.settings:affiliate, 'l.affiliate:status EQ Approved', l.settings:approved_affiliate_list )"/>
                l.settings:affilate:status is the target of what you are filtering. l.settings:affiliate is what you are using as the structure of the expression. (e.g., its use is similar to using an "iterator" within a foreach loop.)
                Bruce Golub
                Phosphor Media - "Your Success is our Business"

                Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                phosphormedia.com

                Comment


                  #9
                  I had a slightly crazy idea.

                  Instead of these fancy array filters, could I do a double test of variables?

                  Code:
                  something like
                  
                  <mvt:ifexpr="g.affilate_code AND l.affiliate:status">
                      do the things I want
                    </mvt:if>
                  The idea would be to have a pair of simple "if" tests, that checks to see if the affiliate code exists, then see if the stats comes back "Approved". With a large data set this would be a very long process but currently we have only and handful of affiliate accounts. Ideally by the time this would become a problem I'll have figure out how to make it more performant.

                  Comment


                    #10
                    You should be able to do this within a simple foreach loop. Array Filter is indeed indented for faster (somewhat cleaner) code, but running through a foreach loop of even several hundred items will take less than a second or two.
                    Bruce Golub
                    Phosphor Media - "Your Success is our Business"

                    Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                    phosphormedia.com

                    Comment


                      #11
                      I have the code reliably working in Firefox, but it seems to fail on Chromium. Are the tools for getting global variables and cookies different?
                      Code:
                      <divclass="wrap">
                        <mvt:ifexpr="g.affiliate_session_id">
                        <mvt:ifexpr="l.settings:category:code EQ 'bravecto-flea-tick-chewable-tablets'">
                        <mvt:itemname="readytheme"param="banner( 'affiliate_special_bravecto' )"/>
                        </mvt:if>
                        <mvt:ifexpr="l.settings:category:code EQ 'Bravecto-Topical'">
                        <mvt:itemname="readytheme"param="banner( 'affiliate_special_bravecto' )"/>
                        </mvt:if>
                        </mvt:if>
                        </div>

                      Comment


                        #12
                        no. the only reason this could happen is an HTML/JS error that kills the display.
                        Bruce Golub
                        Phosphor Media - "Your Success is our Business"

                        Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
                        phosphormedia.com

                        Comment

                        Working...
                        X