Welcome Guest, Not a member yet? Register   Sign In
How to Store Multiple Query in Array
#1

Hello All,
i have been doing some practice with codeigniter ( as i am new) , actually i wanted to do something like cart,
first of all i want to pass a product id parameter  then the model would send a return query which would be displayed in cart.
the problem is i want to display multiple products, so the question is how can i store the return query and display all the selected query at once ? ( i am totally newbie , but here are my code below!!

Controller function
Code:
public function addtocart($pid)
{

$this->load->model('ecomm_model');  
$this->data['cart']=$this->ecomm_model->in_cart($pid);
$this->_render('pages/ecomm_atc');

}
 
Model Function
Code:
public function in_cart($pid)
{
$this->db->select('pid,pn,pp,cc');
$this->db->where('pid',$pid);
$this->db->from('ecomm');
$this->db->limit(1);
$query=$this->db->get();

if($query-> num_rows() ==1)
return $query->result();
else
return false;
}

View :
Code:
<?php  
$result='';
foreach($cart as $row)
{  
$result .= "<br/>".' '."<br/>Product ID: ". $row->pid . ' '."<br/>Product Name: ". $row->pn. ' '."<br/>Product Price: ". $row->pp . "<br/>Category: ".$row->cc;
}?>

<?php
echo $result;


?>

to make it clear , i wanna click on add to cart(button) so that the product is added to cart, but the thing i couldnt figure out is, how to store multiple query so that i can display it in a view.
Reply
#2

Maybe you should use the Shopping Cart Class from CI:
http://www.codeigniter.com/userguide3/li.../cart.html

Reply
#3

$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
),
array(
'id' => 'sku_567ZYX',
'qty' => 1,
'price' => 9.95,
'name' => 'Coffee Mug'
),
array(
'id' => 'sku_965QRS',
'qty' => 1,
'price' => 29.95,
'name' => 'Shot Glass'
)
);

how can i store my query results in this array ? that is my question
Reply
#4

How does your query result looks like? show me the print_r() of the result data.

Reply
#5

(This post was last modified: 11-20-2014, 02:01 PM by bclinton.)

You may be looking for something as simple as $query->result_array();

If not, just loop through your results in the model and put them in the format you want them before returning them to the controller.
Reply
#6

(11-20-2014, 01:24 PM)niires1204 Wrote: how can i store my query results in this array ? that is my question

Yes, like bclinton advises, use
PHP Code:
//return $query->result();
return $query->result_array(); 
in your model. That will return an array that looks like this.
Code:
array
  0 => 
    array
      'id' => string 'sku_567ZYX' (length=10)
      'qty' => int 1
      'price' => float 9.95
      'name' => string 'Coffee Mug' (length=10)
Then you can add that array to $data array like this
PHP Code:
$data[] = $query[0]; 
You can try this test code to see that it works.
PHP Code:
<?php
  $data 
= array(
 
   array(
 
     'id'      => 'sku_123ABC',
 
     'qty'     => 1,
 
     'price'   => 39.95,
 
     'name'    => 'T-Shirt',
 
     'options' => array(
 
       'Size' => 'L',
 
       'Color' => 'Red')
 
   ),
 
   array(
 
     'id'      => 'sku_965QRS',
 
     'qty'     => 1,
 
     'price'   => 29.95,
 
     'name'    => 'Shot Glass'
 
   )
 
 );
 
 var_dump('original $data'$data);

 
 $query[0] = array(
 
   'id' => 'sku_567ZYX',
 
   'qty' => 1,
 
   'price' => 9.95,
 
   'name' => 'Coffee Mug'
 
 );
 
 var_dump('query array'$query);

 
 $data[] = $query[0];

 
 var_dump('final $data'$data);
?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB