php - Function has no effect for the first item in the array -


i have function change quantity of product , store in session.

public static function updateproduct($product_code, $newqty) {        foreach ($_session["products"] $cart_itm) //loop through session array     {         if($cart_itm["code"] == $product_code) //the item exist in array         {             $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$newqty);         }         else         {             //item doesn't exist in list, retrieve old info , prepare array session var             $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"]);         }     }     //found user item in array list, , increased quantity     $_session["products"] = $product; return; } 

but if there 2 or more products in array, function works last added product. have idea?

ps: function adding product cart working fine. there aren't big differences.

code comment:

$cart_items = 0;  foreach ($_session["products"] $cart_itm) {        echo '<input type="hidden" name="product_code" value="'.$product_code.'" />';            $cart_items ++; }  if(isset($_session["products"])) {     echo '<form method="post" name="updateproduct">';      echo '<ul>';     $cart_items = 0;      foreach ($_session["products"] $cart_itm)     {        $product_code = $cart_itm["code"];         // product_name         $db = jfactory::getdbo();         $query = $db->getquery(true);         $query->select($db->quotename('product_name'))          ->from($db->quotename('#__osmaf_products'))          ->where($db->quotename('product_code') . ' = '. $db->quote($product_code));         $db->setquery($query);         $result = $db->loadrow();          echo '<li class="cart-itm">';         echo '<h4>'.$result['0'].' (artikelnr.:'.$product_code.')</h4>';         echo '<label style="display:inline">menge: </label><input type="number" style="width:50px;margin-top:9px" name="newqty" value="'.$cart_itm["qty"].'" />';         echo ' <input type="submit" class="btn btn-primary" name="updateproduct" value="&#x21bb;" />';         echo '<input type="hidden" name="product_code" value="'.$cart_itm["code"].'" />';         echo '</li>';         $cart_items ++;      } } 

when send form everytime product want update, last name , qty of last product gets send function.

i think in way not add items in $product array each time re-initiate it. recommend use array_push instead.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -