Siva Shanker
Siva Shanker

Reputation: 133

How to update the cart in the codeigniter

I am using the cart library , and displaying the quantity (in textbox),price,description,subtotal,total in the shipping page. Below is the update link, pressing the update should go to the controller cart->update

<a href="<?php echo BASE_INDEX_URL; ?>/cart/update/<?php echo $items['rowid'];?>">Update </a>

Below is the update action , if I give the default quantity value as 3, the cart class is updated(means quantity,price,description)

public function update($rowid)
{
    $data=$this->cart->update(array(
        'rowid'=>$rowid,
        'qty'=>3
    ));

    $this->cart->update($data);  

    redirect('cart/shipping');
}

But I want to get the quantity value from the textfield in the shipping page of the particular item and get updated

Upvotes: 0

Views: 10858

Answers (2)

Ankit Prajapati
Ankit Prajapati

Reputation: 445

I know this is late but might be helpful to others , You need to do something like this ,

Add this in your view file

 <?php echo form_input(array('name' =>'qty[]', 'type'=>'number','class'=>'form-control', 'style'=>'width:75px' ,'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
    <?php echo form_input(array('name' =>'rowid1[]', 'type'=>'hidden', 'value' => $items['rowid'], 'maxlength' => '3', 'size' => '5')); ?>

And this in you controller file

  public function updateCart()
{
    $i = 0;
    foreach ($this->cart->contents() as $item) {

        $qty1 = count($this->input->post('qty'));
        //    print_r($qty1);
        //$i=1;
        for ($i = 0; $i < $qty1; $i++) {
            echo $_POST['qty'][$i];
            echo $_POST['rowid1'][$i];
            $data = array('rowid' => $_POST['rowid1'][$i], 'qty' => $_POST['qty'][$i]);
            $this->cart->update($data);
        }
       // $this->load->view('cart');

    }
    redirect('Shoppingcart/viewcart');
}

Upvotes: 0

swatkins
swatkins

Reputation: 13630

You're not passing the textbox value to the new page. You'll either need to POST data to the page via a form, or pass the value from the textbox in the url.

add this to your view:

<form action="" method="POST">
  <input type="text" name="quantity" value="" />
  <!-- OTHER FORM FIELDS HERE -->
  <input type="submit" name="submit" value="UPDATE" />
</form>

and your controller:

public function update($rowid)
{
    $data=$this->cart->update(array(
        'rowid'=>$rowid,
        'qty'=> $this->input->post('quantity');
    ));

    $this->cart->update($data);  

    redirect('cart/shipping');
}

Upvotes: 2

Related Questions