Kedar B
Kedar B

Reputation: 784

how to update item list like shopping cart?

I have purchase order form where I am storing product details in session i.e product name, quantity, rate etc. It is working fine. But when I edit that purchase order I get already stored session data correctly but when user enter another product , it overrides first records. Means only newly added record is displayed. Old record disappear. Following is my code for purchase order form add to cart button..

function index(){       

    $data= array('product_id'=>$this->input->post('product_id'),
        'quantity'=>$this->input->post('quantity'),
        'unit'=>$this->input->post('unit'),
        'unit_rate'=>$this->input->post('unit_rate'));


    //Get the cart

    $cart = $this->session->userdata('data');
    //Add data to this temporary variable
    $cart[] = $data;
    //Set back the data
    $this->session->set_userdata('data', $cart);
    $post_array['cart']=$this->session->userdata('data')
    ?>

    <tr>
        <th>Product  Name</th>
        <th>Quantity</th>
        <th>Unit</th>
        <th>Unit Rate</th>
        <th>Action</th>
    </tr>
    <?php
    $i=0;
    foreach($post_array['cart'] as $item){

        $query = $this->db->query("SELECT name FROM phppos_items WHERE 
 item_id='".$item['product_id']."'");
        foreach($query->result() as $row){
            $product_name=$row->name;

        }
        echo "<tr>";
        echo "<td>".$product_name."</td>";
        echo "<td>".$item['quantity']."</td>";
        echo "<td>".$item['unit']."</td>";
        echo "<td>".$item['unit_rate']."</td>";
        echo "<td><a href='javascript:void(0)' rownum='".$i."' 
    class='remove_from_cart'><img src='images/close.png'/></a></td>";
        echo "</tr>";
        $i++;
    }
    ?>

    <?php
 }

I am getting old session data array in this.

 $this->session->userdata('sess_products');

I want to know how do I update that session array with new data? I am calling this same while editing.

Upvotes: 0

Views: 159

Answers (3)

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

use separators to connect multiple things

$item=$this->session->userdata('item');
$item=$update."+"."New item";
$price=$this->session->userdata('price');
$price=$price."+"."New price"; //add as much as u want(but you can store only 4kb in CI session. so restrict add multiple things in cart)
$this->session->set_userdata('item',$item);
$this->session->set_userdata('price',$price);

this might help you

Upvotes: 0

jeky pan
jeky pan

Reputation: 36

Why dont you use the CODEIGNITER INBUILT SHOPPING LIB.

Check Link

Upvotes: 0

jeky pan
jeky pan

Reputation: 36

hey it is the same way you are adding session data for update.

$this->session->set_userdata('some_name', 'some_value');

Click this Link

Upvotes: 1

Related Questions