Welcome Guest, Not a member yet? Register   Sign In
Cart Library - Multi dimensional array
#1

[eluser]Ray G[/eluser]
I am using the cart library and having a problem inserting a multi-dimensional array. The array seems fine but only the first will insert. Here is the code to create the array and insert items
Code:
// Add an item to the cart
function validate_add_cart_item($postdata){
    //print_r($postdata['pnpattr']);
    $qty = 1;                 // Assign posted quantity to $cty
  $pkg = $postdata['pkg'];
    $i=0;
    foreach($postdata['pnpattr'] as $items)
    {
      //echo 'hello<br>';
    $item = explode('|', $items);
    $id = $item[0];     // Assign posted product_id to $id
    $code = $item[1];   //
    $color = $item[2];

    $this->db->where('id', $id); // Select where id matches the posted id
      $this->db->where('qoh !=', '0'); // Select where id matches the posted id
    $query = $this->db->get('inventory', 1); // Select the products where a match is found and limit the query by 1
      $rows = $query->num_rows();
      $row = $query->row();
      $query->free_result();
      //echo "<br>$rows<br>";
      //echo $this->db->last_query();
    // Check if a row has been found
      $error = array();
    if($rows > 0){
      //echo 'hello<br>';
        // Create an array with product information
        if($pkg == '1')
        {
          $data[] = array('id' => $id, 'qty' => $qty, 'price' => '14.95', 'name' => $row->desc, 'options' => array('pcode' => $code, 'pcolor' => $color, 'pkg' => $pkg));
          //$this->cart->insert($data);
        } else {
          if($pkg == '2' && $i == 0)
         {
           //echo "<br>$i";
          $data[] = array('id' => $id, 'qty' => $qty, 'price' => '19.95', 'name' => $row->desc, 'options' => array('pcode' => $code, 'pcolor' => $color, 'pkg' => $pkg));
            //$this->cart->insert($data);
            $i++;
         } else {
           if($pkg == '2' && $i == 1)
            {
              //echo "<br>$i";
              $data[] = array('id' => $id, 'qty' => $qty, 'price' => '0', 'name' => $row->desc, 'options' => array('pcode' => $code, 'pcolor' => $color, 'pkg' => $pkg));
              //$this->cart->insert($data);
           }
          }
        }
      // Nothing found! Return FALSE!
    }else{
      $error[] = 1;
    }
    }
    $this->cart->insert($data);
    print_r($data);
  }

Variable $postdata['pnpattr'] is formatted as "2|PNPCAR|Candy Apple Red"

Which produces this array
Array ( [0] => Array ( [id] => 2 [qty] => 1 [price] => 19.95 [name] => Prep N Pop (Candy Apple Red) [options] => Array ( [pcode] => PNPCAR [pcolor] => Candy Apple Red [pkg] => 2 ) ) [1] => Array ( [id] => 1 [qty] => 1 [price] => 0 [name] => Prep N Pop (Licorice) [options] => Array ( [pcode] => PNPLB [pcolor] => Locorice Black [pkg] => 2 ) ) )

Any help appreciated

Thanks
#2

[eluser]Ray G[/eluser]
Figured out what was wrong. I was setting the price of a free item to 0. The class trims leading zeros before it inserts the price. so if the price is zero there is no price and therefore does not get inserted. May want to check that out.

I am unable to edit the main post so maybe a moderator can mark this solved.

Ray




Theme © iAndrew 2016 - Forum software by © MyBB