Welcome Guest, Not a member yet? Register   Sign In
Can not insert shopping cart athough name have not special character
#1

[eluser]misakitran[/eluser]
I use jquery for my shopping cart (tutorial on http://net.tutsplus.com), but $this->cart->insert($data) is not working.
Any one help me ?

IN my .js file :
Code:
$(".CartForm").submit(function(event) {

        event.preventDefault();

        var id = $(this).find('input[name=id]').val();

        var add_cart = $.post(link + "order/add_to_cart", {product_id: id, ajax: '1'},
            function( data ) {
                if(data != "false") {
                                    
                    
                        $("#CartTotal").html(data);

                }
                else {  
                    alert("error insert to cart!");
                }

            });

    });

Code:
function add_to_cart()
    {
        $id = $this->input->post('product_id');
        $product = $this->Products_model->get_product($id);
        
        if (count($product) >= 1) {
            $item = $product[0];

            $data = array('id' => $id,
                        'name' => "shirt",
                        'price' => $item['price'],
                        'qty' => 1
                        );        

            $this->cart->insert($data);
            
            if ($this->input->post('ajax') != '1') {
                redirect(base_url()."order");
            }
            else {
                print_r($this->cart->contents());  
            }
        }
        else {
            print_r('false');
        }        
    }
Contents in cart is not showed. Why ? Help me please !
#2

[eluser]InsiteFX[/eluser]
You need to read the CodeIgniter Users Guide on Active Record!

Look at your add_tocart() method.
Not tested but should be something like below...
Code:
function add_to_cart()
    {
        $id = $this->input->post('product_id');
        $query = $this->Products_model->get_product($id);
        
        if ($query->num_rows() == 1) {
            $product = $query->result();

            $data = array('id'  => $id,
                        'name'  => $product->name,
                        'price' => $product->price,
                        'qty'   => 1
                        );        

            $this->cart->insert($data);
            
            if ($this->input->post('ajax') != '1') {
                redirect(base_url()."order");
            }
            else {
                print_r($this->cart->contents());  
            }
        }
        else {
            print_r('false');
        }        
    }
#3

[eluser]misakitran[/eluser]
i see but in my Model, my function return an array :
Code:
function get_product($id)
    {
        $this->db->select('*');
        $this->db->from('products');
        $where = array('active' => '1', 'id' => $id);
        $this->db->where($where);
        
        $query = $this->db->get();
        return $query->result_array();
    }
#4

[eluser]InsiteFX[/eluser]
If you are only returning one record then why are you using result_array()?

Use row() for only one record.
#5

[eluser]misakitran[/eluser]
I see that. Thank you !
And..I also fixed it. Reason is price = 0 Big Grin
Thanks for your reply !




Theme © iAndrew 2016 - Forum software by © MyBB