Welcome Guest, Not a member yet? Register   Sign In
Multiple table load
#1

[eluser]sharea[/eluser]
Hi guys,

I'm loading content from 2 tables for product info(Nettuts cart tutorial). I use this standard function to load from one table
Code:
$query = $this->db->get
. Then i use this line of code to add data into an array and put it into the cart
Code:
if($query->num_rows > 0){
        
            foreach ($query->result() as $row)
            {
                $data = array(
                       'id'      => $id,
                       'qty'     => $cty,
                       'price'   => $row->price,
                       'name'    => $row->name
                );

                $this->cart->insert($data);
                
                return TRUE;
            }
. However i would like data from not only one table but from two. Is there a way to do this by maybe changing the get function or is there a way around?

I hope you can help me,
Thanks.
#2

[eluser]jdav3579[/eluser]
Could you not join the tables using $this->db->join();
(http://ellislab.com/codeigniter/user-gui...ecord.html)
before:
$query = $this->db->get(...)
#3

[eluser]sharea[/eluser]
Thanks for the quick response. It seems the right option. I have the two tables (products en products 2 in database 'form') in a join:
Code:
$this->db->where('id', $id);
        $this->db->select('*');
        $this->db->from('form');
        $this->db->join('products', 'products2');
        $query = $this->db->get('products', 1);
        if($query->num_rows > 0){
        
            foreach ($query->result() as $row)
            {
                $data = array(
                       'id'      => $id,
                       'qty'     => $cty,
                       'price'   => $row->price,
                       'name'    => $row->name
                );

                $this->cart->insert($data);
I'm probably doing something stupid.. In what line do i combine the 2 and give it a new name?

Hope you can help me.
#4

[eluser]jdav3579[/eluser]
Hi I think your code should really look something like:

Code:
$this->db->select('price,name');
$this->db->from('products');
$this->db->join('products2', 'products.id = products2.id');
$this->db->where('products.id', $id);
$query = $this->db->get();
$rs=$query->row();

$data=array('id'=>$id,'qty'=>$qty,'price'=>$rs->price, 'name'=>$rs->name);
$this->cart->insert($data);


You will need to adjust the 'id' keys to what ever you have called them..

let me know if this helps and if not I will see what I can do..

Cheers
John
#5

[eluser]sharea[/eluser]
Hi John,

Thank you for your help. Amazing!

I tried to implement it with the if function to return a true. Maybe with your solution this isn't necessary any more?

This is my function with your join:

Code:
function validate_add_cart_item(){
        
        $id = $this->input->post('product_id');
        $cty = $this->input->post('quantity');
        
        $this->db->select('$id,$cty');
        $this->db->from('products');
        $this->db->join('products2', 'products.id = products2.id');
        $this->db->where('products.id', $id);
        $query = $this->db->get();
        $rs=$query->row();
    if($query->num_rows > 0){
        
            foreach ($query->result() as $row)
            {        $data=array('id'=>$id,'qty'=>$qty,'price'=>$rs->price, 'name'=>$rs->name);
        $this->cart->insert($data);
                
                return TRUE;
            }
        

        }else{
            return FALSE;
        }
    }

When submitting an item i get no response. Php does not give an error.
#6

[eluser]jdav3579[/eluser]
Hi,
For the record this line returns a single array:
Code:
$rs=$query->row();
Could you tell me what the purpose of the above function is??
Is it to check to see if a particular item is in the cart etc?
#7

[eluser]sharea[/eluser]
The function is to check if something is in the database. Return a true to add the item to the cart. Initially i had one table of products, and it worked fine. However, now i want to be able to add another table and let it return a true.
#8

[eluser]jdav3579[/eluser]
Is it something like below:
Note you have called the quantity $cty so I have used it too. You will need to change the table keys though in the sql, as I am only using them for indication.

Code:
$id = $this->input->post('product_id');
        $cty = $this->input->post('quantity');
        
        $this->db->select('id','price','name'); #selects the data about the product
        $this->db->from('products');
        $this->db->join('products2', 'products.id = products2.id');
        $this->db->where('products.id', $id);
        $query = $this->db->get();
        $rs=$query->row(); #the product data that has the id in $id is here or false if nothing matches

        if($rs){
          #there is a product matching the specified id at this point
          
           #build an array of only data we need
           $data=array('id'=>$id,'cty'=>$qty,'price'=>$rs->price, 'name'=>$rs->name);

           #do the insert...
           $this->cart->insert($data);
        }
Is that any help to you??
#9

[eluser]sharea[/eluser]
Thanks for your help! The database structure is simple: 2 tables (products and products 2) with id name and price.

Maybe to get a better look, my original model:

Code:
<?php

class Cart_model extends Model {

    function retrieve_products(){
        $query = $this->db->get('products');
        return $query->result_array();
    }
    
    function retrieve_products2(){
        $query = $this->db->get('products2');
        return $query->result_array();
    }
    

    
    function validate_update_cart(){
        
        $total = $this->cart->total_items();
        
        $item = $this->input->post('rowid');
        $qty = $this->input->post('qty');

        for($i=0;$i < $total;$i++)
        {
            $data = array(
               'rowid' => $item[$i],
               'qty'   => $qty[$i]
            );
            
            $this->cart->update($data);
        }

    }
    
    function validate_add_cart_item(){
        
        $id = $this->input->post('product_id');
        $cty = $this->input->post('quantity');
        
        $this->db->where('id', $id);
        $query = $this->db->get('products', 1);
        

        if($query->num_rows > 0){
        
            foreach ($query->result() as $row)
            {
                $data = array(
                       'id'      => $id,
                       'qty'     => $cty,
                       'price'   => $row->price,
                       'name'    => $row->name
                );

                $this->cart->insert($data);
        }
                
                return TRUE;
            }
        
        }else{
            return FALSE;
        }
    }
    


}

With:
Code:
$query = $this->db->get('products', 1);
As the line where i wanted to contact not 1 products table but two.

With all the help i came up with:

Code:
&lt;?php

class Cart_model extends Model {

    function retrieve_products(){
        $query = $this->db->get('products');
        return $query->result_array();
    }
    
    function retrieve_products2(){
        $query = $this->db->get('products2');
        return $query->result_array();
    }
    

    
    function validate_update_cart(){
        
        $total = $this->cart->total_items();
        
        $item = $this->input->post('rowid');
        $qty = $this->input->post('qty');

        for($i=0;$i < $total;$i++)
        {
            $data = array(
               'rowid' => $item[$i],
               'qty'   => $qty[$i]
            );
            
            $this->cart->update($data);
        }

    }
    
    function validate_add_cart_item(){

        
        $id = $this->input->post('product_id');
        $cty = $this->input->post('quantity');
        
        $this->db->select('id','price','name'); #selects the data about the product
        $this->db->from('products');
        $this->db->join('products2', 'products.id = products2.id');
        $this->db->where('products.id', $id);
        $query = $this->db->get();
        $rs=$query->row(); #the product data that has the id in $id is here or false if nothing matches

        if($rs){
          #there is a product matching the specified id at this point
          
           #build an array of only data we need
           $data=array('id'=>$id,'cty'=>$qty,'price'=>$rs->price, 'name'=>$rs->name);

           #do the insert...
           $this->cart->insert($data);
        }
                
                return TRUE;
            }
        
        }else{
            return FALSE;
        }
    }
    


}

Somehow clicking to add an item generates no response.
#10

[eluser]danmontgomery[/eluser]
Code:
function validate_add_cart_item(){
        function validate_add_cart_item(){

Is this a typo?




Theme © iAndrew 2016 - Forum software by © MyBB