Hi all,

I have looked around at all the SMARTY related psots but can't find a solution.

I have a form with a field:

<input type="text" readonly="readonly" class="agentformbl" name="couponid" value="{foreach from=$coupons item=c}{$c.code}
{/foreach}" size="32" />

What I need to do is capture the content of the "{foreach from=$coupons item=c}{$c.code}{/foreach}" and store it in a $_SESSION var.

I have tried all ways with no correct result.

Can anyone advise please.

    Hi all,

    I am still having problems with this code for SMARTY, and I should say now that I am rubbish with SMARTY.

    I have a field in my form that produces the coorect data.

    {foreach from=$coupons item=c}{$c.code}{/foreach}

    I then need to assign that data to a PHP $_SESSION var

    {$smarty->assign('$c.code',$_SESSION['c_code']);}

    and then echo the $_SESSION var

    <? echo $_SESSION['c_code']; ?>

    I know I am doing this wrong, but where do I start. Can someone please point me in the right direction.

      If you are trying to assign it from smarty to php and back to smarty in the same page request, you will have problems.

      Now, if you are trying to take their form submission and store it in a session var, that can probably be handled. Can you paste some PHP code attached to your form handler?

        Hi zabmilenko, and thanks for your fast reply.

        Did you intend to attach a sample code for me to look at.

          No... I don't fully understand what you are doing. I was hoping you could paste something.

          What is the output from the template? Can you paste a list of things in the coupons array? Help me understand what you are trying to do.

            Hi zabmilenko, sorry I misunderstood.

            Below is the code I am working with:

            We have a form and inthe form there is the following line:

            <input type="text" readonly="readonly" class="agentformbl" name="couponid" value="{foreach from=$coupons item=c}{$c.code}
            {/foreach}" size="32" />
            

            I then need to extract the content of "{foreach from=$coupons item=c}{$c.code}
            {/foreach}" example "DA7206F346" each time the script is run the data will be different, its randomly generated.

            I then need to assign the "DA7206F346" to a $_SEESION var, and this is where I am having problems.

            The FORM is called from a script, here is the relative section:

            function display_generate_form(){
                global $db, $t, $vars;
                global $start, $count, $all_count;
            
            $t->assign('discount_types', array('%' => '%'));
            $products = array("26");
            $products[ $p['product_id'] ] = $p['title'];
            $t->assign('products', $products);
            $t->assign('action', 'generate');
            $t->assign('vars', $vars);
            $t->display('coupon_gen_tier.html');
            }
            
            function generate_coupons(){
                global $db, $t, $vars;
                $vars['discount'] = trim("$vars[discount_v] $vars[discount_t]");
            
            set_date_from_smarty('begin_date', $vars);
            set_date_from_smarty('expire_date', $vars);
            $batch_id = $db->generate_coupons($vars);
            $coupons = $db->get_coupons('batch_id', $batch_id);
            $t->assign('coupons', $coupons);
            $t->display('coupon_generated_tier.php');
            }
            

            This script display the script with the FORM on it.

            So what I need to end up with is $_SESSION['foo'] = the content of {foreach from=$coupons item=c}{$c.code}{/foreach}

            I hope this makes sense.

              Ahh!

              Question: Do you need to add the coupon code to the session BEFORE the user sees that form, or AFTER the user submits the form?

                Hi zabmilenko,

                Before the user sees the form so it is already there when the form loads.

                Hey, thanks for your time looking at this for me.

                  If you want to do it before the user sees the form, you will have to build the coupon code in the php function "generate coupons":

                  function generate_coupons(){
                      global $db, $t, $vars;
                      $vars['discount'] = trim("$vars[discount_v] $vars[discount_t]");
                  
                  set_date_from_smarty('begin_date', $vars);
                  set_date_from_smarty('expire_date', $vars);
                  $batch_id = $db->generate_coupons($vars);
                  $coupons = $db->get_coupons('batch_id', $batch_id);
                  
                  $_SESSION['c_code'] = implode("", $coupons);
                  
                  $t->assign('coupons', $coupons);
                  $t->display('coupon_generated_tier.php');
                  } 
                  

                  Does that look kind of like what you are trying to do?

                    Hi zabmilenko

                    Again thanks for your time.

                    I inserted the code into my script but when the result is displayed it shows the word "array"

                    What I am trying to do is display the content of the array.

                    Sorry to be a pain.

                      Hi all

                      I am struggling with this post.

                      does anyone have any idea on how to make this work.

                        What is the contents of the array? Debug a bit in the php function with:

                        print '<pre>' . print_r($coupons, true) . '</pre>';
                        // $_SESSION['c_code'] = implode("", $coupons); 
                        
                          Write a Reply...