Announcement

Collapse
No announcement yet.

Need Help Writing Code in a Foreach Loop

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

    Need Help Writing Code in a Foreach Loop

    I’m building a multi-page form. The first page of the form uses a foreach loop to display products in a category. The shopper increases the quantities of the products that they want to add to the basket, and then hit continue. However, I’m not committing the items to the basket just yet, I’m just storing them in an array.

    I’d like to have a “Back” button, so the shopper can have the opportunity to go back to the last page and adjust the quantities, and I’d like the quantity text fields to be auto-populated with the quantities that they’ve already selected, rather than them all being set back to zero. So when they click the “Back” button, I’m passing variables like: prod1=5, prod2=3, prod3=1, where the variable name is the Miva product ID, and the value is the quantity they’ve selected.

    But now I’m stuck on auto-populating the quantity fields with those passed variables.

    Logically, I think it needs to be something like this:

    Code:
    <mvt:assign name=“l.settings:currentID" value="'prod' $ l.settings:product:id" />
    That would be placed within the foreach loop, and would create a variable of the current product named "CurrentID" with a value of prod1, prod2, prod3, etc...

    Then I need to say:

    IF a global variable exists with the same name as the value of g.CurrentID
    {

    set the quantity field to its value

    } ELSE {

    set the quantity field to zero

    }

    But, I cannot for the life of me figure out how to write this code. Or maybe I’m going about it the wrong way, altogether.

    If anyone can assist me with this, I’d really appreciate it!
    Thank you

    #2
    Not sure why you are messing with Product IDs. From the front side of Miva, they are not very useful. But if the end goal is to add multiple products to the basket at the same time, you'll want to start by examing the new ADPM action (add mulitple products to basket).

    Here's a couple of posts on that subject:
    http://www.miva.com/forums/forum/onl...490#post655490
    http://www.miva.com/forums/forum/onl...547#post649547

    That will give you the form post fields you'll need and some examples of SMT code to generate them.
    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


      #3
      Thanks Bruce.

      I'm familiar with ADPM and have it working in other areas of my site. This is more of a question about how to populate a field based on if a POST variable, who's name includes a Product ID, matches the current Product ID in a foreach loop.

      Thanks

      Comment


        #4
        Ah, ok then (sometimes coding issues are easier solved by heading in the right direction...i guess that wasn't the case here). I think you're going to need to use
        the miva_variable_value( ) function:

        This is for Mivascript, but basically the SMT version is the same.

        http://www.mivascript.com/mm5/mercha...&Search_Type=P
        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
          Thank you, Bruce! That was exactly the function I needed. Thank you so much for the help!

          And here's the solution, for anyone who might find this helpful:

          First, you link to the page and pass the desired variables:
          http://www.example.com?prod1=11&prod4=14&prod6=16

          Code:
          <mvt:assign name="l.settings:passed_variable" value="miva_variable_value('g.prod' $ l.settings:product:id)" />
          This creates the variable named "passed_variable" with a value of "g.prod" + the current product ID in the foreach loop.

          Code:
          <mvt:if expr="l.settings:passed_variable GT 0">  &mvt:passed_variable; <mvt:else>  0  </mvt:if>
          Then you can check if a POST/GET global variable with a name of "prod1", "prod2", etc. has been passed to the page, and display it's value within the loop iteration of the product that has the matching product ID.
          Last edited by allclear; 07-13-16, 12:09 PM.

          Comment

          Working...
          X