-1

I want to show image in cart system in codeigniter. But I failed to do that. I am new in codeigniter. So I need some help from you.

here is my controller:::

class Cart extends CI_Controller {

public function add_to_cart($product_id) { // adds a product to cart 
    $this->load->model('cart_model');
    $result = $this->cart_model->selectProductByProductId($product_id);

    $insert = array(
        'id' => $product_id,
        'qty' => 1,
        'price' => $result->product_price,
        'name' => $result->product_name,
    'image' => $result->product_image
    );
    //$this->load->library('Cart');
    $this->cart->insert($insert);
    redirect("cart/show_cart");
}

View:::

<?php echo $items['image'] ?>

This is not working in cart view page .. Maybe I need some change with cart library but I do not know where I edit or update. Please help me out .

Selim Reza
  • 372
  • 8
  • 20

3 Answers3

1

Try this

class Cart extends CI_Controller {

public function add_to_cart($product_id) { // adds a product to cart 
    $this->load->model('cart_model');
    $result = $this->cart_model->selectProductByProductId($product_id);

    $insert = array(
        'id' => $product_id,
        'qty' => 1,
        'price' => $result->product_price,
        'name' => $result->product_name,
    'image' => $result->product_image
    );
    //$this->load->library('Cart');
    $this->cart->insert($insert);
    $data['image'] = $result->product_image;
    $this->load->view("cart/show_cart",$data);
}

And in your View page just

echo $image; //gives the path of the image

Note:

If you want to use redirect in your code the store the image path in session

vijaykumar
  • 4,658
  • 6
  • 37
  • 54
0

I hope cart/show_cart is your view, instead of using redirect method. use $this->load->view('view_name', $items); here view_name is your show_cart and pass data that you wants to display in your view

check this link for more info...http://ellislab.com/codeigniter/user-guide/general/views.html

ghmulchandani
  • 304
  • 3
  • 19
0
$insert = array(
    'id' => $product_id,
    'qty' => 1,
    'price' => $result->product_price,
    'name' => $result->product_name,
'img' => $result->product_image
);

$this->cart->insert($insert);

  • 1
    You might want to provide a little *commentary* along with your code to explain what it is doing. – Jack Aug 29 '14 at 13:13