Welcome Guest, Not a member yet? Register   Sign In
multiple table issue
#1

[eluser]sith4life[/eluser]
Hi,
Just to begin I am fairly new to Codeigniter and MVC Programming. That being said I am having an issue and I was wondering if anyone had any ideas on how to fix it.
Background: The application is an online store users can visit to purchase items etc.
Problem: when i add items to the cart an ID is assigned to the current item in the database. however when I try and pull the information from the database the IDs are incorrect and therefore I cannot update quantities or delete them from the cart. (I hope that made sense).
function inside controller:
Code:
/*
     * Cart Page for displaying the users cart contents inside the store controller
     *
     */
    public function cart()
    {
        $this->load->model('Navigation_model');
        $this->load->model('Products_model');
        if($this->input->post('update'))
        {
            $this->Products_model->update_cart($this->input->post('cart_id'),$this->input->post('qty'));
        }
        $data['carts'] = $this->Products_model->view_cart($this->session->userdata('email'));
        $data['links'] = $this->Navigation_model->get_navigation();
        $data['current'] = 0;
        $this->load->view('header_view');
        $this->load->view('cart_view',$data);
        $this->load->view('nav_view',$data);
        $this->load->view('footer_view');
    }
view:
Code:
<?php
            $total = 0; // initialize the total so it can be calulated properly later
            foreach($carts as $cart):
        ?>
        <div class="prod_info">
            <h4>&lt;?php echo $cart->name; ?&gt;</h4>
                &lt;?php
                    $image = $cart->image;
                ?&gt;
                <img src="&lt;?php echo base_url() . $image; ?&gt;"
                     height="75" width="75"
                     alt="&lt;?php echo $cart-&gt;name; ?&gt;" />
                &lt;?php
                    echo $cart->description;
                ?&gt;<br />
              &lt;?php
              
                    echo '$' . $cart->price;
                    echo form_open('store/cart');
                    $options = array('name' => 'qty','value' => $cart->qty, 'maxlength' => '3', 'size' => '2');
                    echo form_input($options);
                    echo form_hidden('cart_id',$cart->id);
                    echo form_submit('update','Update Quantity');
                    echo form_close();
              
               ?&gt;
            </div>
            &lt;?php
            
                $total += ($cart->price * $cart->qty); // add the price to each item to create the sub total
                endforeach;
                echo '<p>Total: ' . $total . '</p>';
            ?&gt;
Cart functions inside Products_model:
Code:
function view_cart($email)
    {
        $query = $this->db->query('SELECT carts.*, products.* FROM carts, products WHERE carts.product_id = products.id');
        return $query->result();
    }
    }
function update_cart($id,$qty,$delete = false)
    {
        if($delete === false)
        {
            $query = '
            UPDATE carts
            SET carts.qty = "'.$qty.'"
            WHERE carts.id = "'.$id.'"
            ';
        }
        else
        {
            $query = '
            DELETE FROM carts
            WHERE carts.id = "'.$id.'"
            ';
        }
        $this->db->query($query);
    }
Application Flow:
1) user visits /store/products
2) clicks add to cart
3) redirected to /store/cart
4) user tries to update item quantity in the cart (page just refreshes with no changes successfully applied)

I would be happy to elaborate in places if necessary Smile

Any help would be greatly appreciated.
Thanks in advance!
#2

[eluser]Clooner[/eluser]
How are the id's generated wrong? Do you have to code that selects the id's? Are the product id's shown in stead of the cart id's?
#3

[eluser]sith4life[/eluser]
the following checks to see if the update button is pressed
Code:
if($this->input->post('update'))
        {
            $this->Products_model->update_cart($this->input->post('cart_id'),$this->input->post('qty'));
        }
if so run this query
Code:
function update_cart($id,$qty,$delete = false)
    {
        if($delete === false)
        {
            $query = '
            UPDATE carts
            SET carts.qty = "'.$qty.'"
            WHERE carts.id = "'.$id.'"
            ';
        }
        else
        {
            $query = '
            DELETE FROM carts
            WHERE carts.id = "'.$id.'"
            ';
        }
        $this->db->query($query);
    }
EXCEPT. $id is being populated with the products.id (or carts.product_id) and not the carts.id field so the query just returns with no rows affected.
$id is passed from a hidden field with the value set to what is supposed to be the correct ID, but it's not.
Code:
echo form_open('store/cart');
                    $options = array('name' => 'qty','value' => $cart->qty, 'maxlength' => '3', 'size' => '2');
                    echo form_input($options);
                    echo form_hidden('cart_id',$cart->id);
                    echo form_submit('update','Update Quantity');
                    echo form_close();
Here is the ENTIRE Products_model.php file
Code:
&lt;?php

class Products_model extends CI_Model {
    
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_all_products()
    {
        $query = $this->db->query('SELECT * FROM products');
        return $query->result();
    }

    function add_to_cart($info)
    {
        $result = $this->db->insert('carts',$info);
        if($result)
         {
             return true;
         }
         else
         {
             return false;
         }
    }
    function view_cart($email)
    {
        $query = $this->db->query('SELECT carts.*, products.* FROM carts, products WHERE carts.product_id = products.id');
        return $query->result();
    }
    /*
     * takes 2 parameters id, qty and 1 optional
     * boolean, delete
     *
     * if delete is set the item is deleted from the table
     * otherwise update the qty field for the select cart item
     *
     *
     */
    function update_cart($id,$qty,$delete = false)
    {
        if($delete === false)
        {
            $query = '
            UPDATE carts
            SET carts.qty = "'.$qty.'"
            WHERE carts.id = "'.$id.'"
            ';
        }
        else
        {
            $query = '
            DELETE FROM carts
            WHERE carts.id = "'.$id.'"
            ';
        }
        $this->db->query($query);
    }
    function add_product($info)
    {
         $result = $this->db->insert('products',$info);
         if($result)
         {
             return true;
         }
         else
         {
             return false;
         }
    }

}
#4

[eluser]sith4life[/eluser]
I think the problem is query related but i'm at a bit of a loss. Thanks for the help so far
#5

[eluser]Clooner[/eluser]
Code:
$query = $this->db->query('SELECT carts.*, products.* FROM carts, products WHERE carts.product_id = products.id');

This is how you get the data, am I right? You select carts.* and products.* and both have an id? try changing the select statement to
Code:
SELECT carts.*, products.*, carts.id AS cart_id, products.id AS product_id
and then use cart_id instead of the id!
#6

[eluser]sith4life[/eluser]
omg it works, you sir are a genius. something so simple Sad

thanks so much!




Theme © iAndrew 2016 - Forum software by © MyBB