Smarty Foreach Help Needed

sharingsunshine
sharingsunshine used Ask the Experts™
on
I have a need to parse the contents of the cart for a particular productid.  I have found the exact foreach code I am using in a smarty .tpl that is being included above my code in the same .tpl file.  So I am confused why it won't work.

{foreach from=$products item=product name=products}
    {$product.productid}
     {if $product.productid eq 357}
     {include file="promotions/cleanstart_promo.tpl"}
     {/if}
     {/foreach}

Open in new window


When I use this code I get this error:

Invalid argument supplied for foreach()

The table has the key productid.  What am I doing wrong.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
can you do a var_dump($products) in your php code and show the output?
Sandeep KothariProject Lead

Commented:
it also depends on the content of promotions/cleanstart_promo.tpl .. does it contain any smarty foreach loop ???

Author

Commented:
the vardump is

array(4) { [0]=> array(69) { ["cartid"]=> int(1) ["productid"]=> string(3) "226" ["amount"]=> float(5) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(5) "993-5" ["product"]=> string(30) "LBS II Vegi-Tabs (100 tablets)" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(4) "0.20" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(12.5) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(62.5) ["subtotal"]=> string(5) "62.50" ["display_price"]=> string(5) "12.50" ["display_discounted_price"]=> float(62.5) ["display_subtotal"]=> string(5) "62.50" ["qv"]=> string(5) "12.50" ["list_price"]=> string(5) "18.75" ["descr"]=> string(30) "(100 tablets), Stock No. 993-5" ["fulldescr"]=> string(5340) "

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
the content of promotions/cleanstart_promo.tpl consists of javascript and html in it.

Author

Commented:
Looking deeper into this.  It appears a better array to use is $cart.  

The debugging console shows the following

{$cart}      Array (26)
max_cartid => 1
products => Array (1)
  0 => Array (26)
    cartid => 1
    productid => "357"

Productd 357 is in the cart.

I tried a section since I wasn't sure if this was considered an associative array

{section name=products loop=$cart}   
  {$cart[products].productid}     // just trying to get it to print out the contents 
if $cart[products].productid eq 357}     
  {include file="rjw/promotions/cleanstart_promo.tpl"}     
  {/if}    
  {/section}

Open in new window


Nothing prints out so it looks like I am not referencing it correctly.  Please tell me how to  parse the cart and then do the if test.

here is the $cart array
array(26) { ["max_cartid"]=> int(1) ["products"]=> array(1) { [0]=> array(26) { ["cartid"]=> int(1) ["productid"]=> string(3) "357" ["amount"]=> float(101) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(6) "3993-8" ["product"]=> string(46) "CleanStart Cleanse / Detox Program - Wildberry" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(5) "10.00" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(45.95) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(4640.95) ["subtotal"]=> string(7) "4640.95" ["display_price"]=> string(5) "45.95" ["display_discounted_price"]=> float(4640.95) ["display_subtotal"]=> string(7) "4640.95" } } ["total_cost"]=> float(4660.95) ["shipping_cost"]=> string(5) "20.00" ["taxes"]=> array(0) { } ["tax_cost"]=> string(4) "0.00" ["discount"]=> string(4) "0.00" ["discount_info"]=> string(0) "" ["coupon"]=> string(0) "" ["coupon_discount"]=> string(4) "0.00" ["discount_coupon_info"]=> string(0) "" ["subtotal"]=> string(7) "4640.95" ["display_subtotal"]=> string(7) "4640.95" ["discounted_subtotal"]=> string(7) "4640.95" ["display_shipping_cost"]=> string(5) "20.00" ["display_discounted_subtotal"]=> string(7) "4640.95" ["orders"]=> array(1) { [0]=> array(16) { ["total_cost"]=> string(7) "4660.95" ["shipping_cost"]=> string(5) "20.00" ["taxes"]=> array(0) { } ["tax_cost"]=> string(4) "0.00" ["discount"]=> string(4) "0.00" ["discount_info"]=> string(0) "" ["coupon"]=> string(0) "" ["coupon_discount"]=> string(4) "0.00" ["discount_coupon_info"]=> string(0) "" ["subtotal"]=> string(7) "4640.95" ["display_subtotal"]=> string(7) "4640.95" ["discounted_subtotal"]=> string(7) "4640.95" ["display_shipping_cost"]=> string(5) "20.00" ["display_discounted_subtotal"]=> string(7) "4640.95" ["products"]=> array(1) { [0]=> array(26) { ["cartid"]=> int(1) ["productid"]=> string(3) "357" ["amount"]=> float(101) ["options"]=> NULL ["free_price"]=> string(4) "0.00" ["distribution"]=> string(0) "" ["wishlistid"]=> NULL ["variantid"]=> NULL ["productcode"]=> string(6) "3993-8" ["product"]=> string(46) "CleanStart Cleanse / Detox Program - Wildberry" ["provider"]=> string(8) "1294dahl" ["weight"]=> string(5) "10.00" ["shipping_freight"]=> string(4) "0.00" ["free_shipping"]=> string(1) "N" ["discount_avail"]=> string(1) "Y" ["product_type"]=> string(1) "N" ["price"]=> float(45.95) ["taxes"]=> array(0) { } ["product_options"]=> bool(false) ["discount"]=> int(0) ["coupon_discount"]=> int(0) ["discounted_price"]=> float(4640.95) ["subtotal"]=> string(7) "4640.95" ["display_price"]=> string(5) "45.95" ["display_discounted_price"]=> float(4640.95) ["display_subtotal"]=> string(7) "4640.95" } } ["provider"]=> string(8) "1294dahl" } } ["payment_surcharge"]=> int(0) ["display_cart_products_tax_rates"]=> string(1) "N" ["product_tax_name"]=> string(0) "" ["giftcert_discount"]=> int(0) ["applied_giftcerts"]=> array(0) { } ["shippingid"]=> string(1) "1" ["shipping_warning"]=> NULL ["delivery"]=> string(10) "UPS Ground" ["paymentid"]=> string(2) "20" }

Open in new window


Thanks,

Randal

Sandeep KothariProject Lead

Commented:
have you smarty->assigned cart array .... ??? ...

Author

Commented:
Sorry I didn't make myself clearer.  I was on a Smarty page when I was reading the debug console.  In what I have learned it seems, it has been assigned or it wouldn't be in the debug console.

Sandeep KothariProject Lead

Commented:
oh ... in that case it seems you had some smarty error...........looking at your code again I can see a curly brace is missing for if statement... try this...

{section name=products loop=$cart}   
  {$cart[products].productid}     // just trying to get it to print out the contents 
  {if $cart[products].productid eq 357}     
  {include file="rjw/promotions/cleanstart_promo.tpl"}     
  {/if}    
  {/section}

Open in new window

Author

Commented:
I don't have any response and I am on the .tpl page where this array detail is in the console.


{$cart}	Array (25)
max_cartid => 1
products => Array (1)
  0 => Array (26)
    cartid => 1
    productid => "357"
    amount => 1
    options => null
    free_price => "0.00"
    distribution => ""
    wishlistid => null
    variantid => null
    productcode => "3993-8"
    product => "CleanStart Cleanse / Detox Program - ..."
    provider => "1294dahl"
    weight => "10.00"
    shipping_freight => "0.00"
    free_shipping => "N"
    discount_avail => "Y"
    product_type => "N"
    price => 45.95
    taxes => Array (0)
    product_options => false
    discount => 0
    coupon_discount => 0
    discounted_price => 45.95
    subtotal => "45.95"
    display_price => "45.95"
    display_discounted_price => 45.95
    display_subtotal => "45.95"
total_cost => 53.45
shipping_cost => "7.50"
taxes => Array (0)
tax_cost => "0.00"
discount => "0.00"
discount_info => ""
coupon => ""
coupon_discount => "0.00"
discount_coupon_info => ""
subtotal => "45.95"
display_subtotal => "45.95"
discounted_subtotal => "45.95"
display_shipping_cost => "7.50"
display_discounted_subtotal => "45.95"
orders => Array (1)
  0 => Array (16)
    total_cost => "53.45"
    shipping_cost => "7.50"
    taxes => Array (0)
    tax_cost => "0.00"
    discount => "0.00"
    discount_info => ""
    coupon => ""
    coupon_discount => "0.00"
    discount_coupon_info => ""
    subtotal => "45.95"
    display_subtotal => "45.95"
    discounted_subtotal => "45.95"
    display_shipping_cost => "7.50"
    display_discounted_subtotal => "45.95"
    products => Array (1)
      0 => Array (26)
        cartid => 1
        productid => "357"
        amount => 1
        options => null
        free_price => "0.00"
        distribution => ""
        wishlistid => null
        variantid => null
        productcode => "3993-8"
        product => "CleanStart Cleanse / Detox Program - ..."
        provider => "1294dahl"
        weight => "10.00"
        shipping_freight => "0.00"
        free_shipping => "N"
        discount_avail => "Y"
        product_type => "N"
        price => 45.95
        taxes => Array (0)
        product_options => false
        discount => 0
        coupon_discount => 0
        discounted_price => 45.95
        subtotal => "45.95"
        display_price => "45.95"
        display_discounted_price => 45.95
        display_subtotal => "45.95"
    provider => "1294dahl"
payment_surcharge => 0
display_cart_products_tax_rates => "N"
product_tax_name => ""
giftcert_discount => 0
applied_giftcerts => Array (0)
shippingid => "1"
shipping_warning => null
delivery => "UPS Ground"

Open in new window

Commented:
here you go this should work

{foreach from=$cart.products item=curr_id}
    id: {$curr_id.productid}<br>
{/foreach}

Author

Commented:
that got the productid to display but my original question was how to parse the cart for a specific productid say 357.

I have a need to parse the contents of the cart for a particular productid.
Commented:
Use the if condition as follows

{foreach from=$cart.products item=curr_id}
{if $curr_id.productid eq 357}    
  {include file="rjw/promotions/cleanstart_promo.tpl"}    
  {/if}    
{/foreach}

Author

Commented:
works great, I thought I had to have quotes around the numbers but your code worked.  So I learned something about that too.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial