<?php

class Cart_model extends CI_Model
{

		// Function to retrieve an array with all product information
		public function retrieve_products()
			 {
			  $query = $this->db->get('products');
			  return $query->result_array();
			 }
		  
		 // Updated the shopping cart
		public function validate_update_cart()
			 {
			   
			  // Get the total number of items in cart
			  $total = $this->cart->total_items();
			   
			  // Retrieve the posted information
			  $item = $this->input->post('rowid');
			     $qty = $this->input->post('qty');
			 
			  // Cycle true all items and update them
			  for($i=0;$i < $total;$i++)
			  {
			   // Create an array with the products rowid's and quantities. 
			   $data = array(
			               'rowid' => $item[$i],
			               'qty'   => $qty[$i]
			            );
			             
			            // Update the cart with the new information
			   $this->cart->update($data);
			  }
			 
			 }
		  
		 // Add an item to the cart
		public function validate_add_cart_item()
			{
			   
			  $id = $this->input->post('product_id'); // Assign posted product_id to $id
			  $cty = $this->input->post('quantity'); // Assign posted quantity to $cty
			   
			  $this->db->where('id', $id); // Select where id matches the posted id
			  $query = $this->db->get('products', 1); // Select the products where a match is found and limit the query by 1
			   
			  // Check if a row has been found
			  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;
			   }
			   
			  // Nothing found! Return FALSE! 
			  }else{
			   return FALSE;
			  }
			 }
			public function search($keyword)
			    {
			        $this->db->like('name',$keyword);
			        $query  =   $this->db->get('products');
			        return $query->result();
			    }
			public function productdetails()

			    {

			    /* $id = $this->input->post('product_id'); */
			    /* $query  =   $this->db->select('*')
     						 ->from('productdetails')     						 			      						 
     						 ->get(); */
			       
     			$query  =   $this->db->select("t1.price, t1.name, t1.image, t2.details, t2.p_id");
  							$this->db->from('products as t1');
  							$this->db->join('productdetails as t2', 't1.id = t2.p_id');
  							$query = $this->db->get();

			    /* $query  =   $this->db->select('t1.price, t1.name, t1.image, t2.details')
     						 ->from('products as t1')
     						 ->where('t1.id', $id)
     						 ->join('productdetails as t2', 't1.id = t2.p_id', 'INNER')     						 
     						 ->get(); */
			       
			        return $query->result();
			    }
			public function storedetailsmodel($id)
				{

				$query  =   $this->db->select("t1.price, t1.name, t1.image, t2.details, t2.p_id");
							$this->db->where(array('t2.p_id'=>$id));
  							$this->db->from('products as t1');  							
  							$this->db->join('productdetails as t2', 't1.id = t2.p_id');
  							$query = $this->db->get();
  							
					return $query->result();
				}
}