EllisLab text mark
Advanced Search
     
Display Two Query Values In A View
Posted: 07 October 2012 11:07 PM   [ Ignore ]
Avatar
Joined: 2012-08-24
214 posts

Hey guys just want to ask how can i run retrieve two queries in one function? Here’s my model function below.

public function getSalesByDateRange(){
//total sales by date range
$from $this->input->post('datefrom');
$to $this->input->post('dateto');
            
$sql "select product_name,date_format(date_of_sales,'%Y/ %M /%d') as       date_of_sales,qty_purchased,subtotal from tbl_product
right join sales_line on tbl_product.product_id = sales_line.fkproduct
right join sales on sales_line.fksales = sales.sales_id 
where date_of_sales >= '{
$from}' and date_of_sales <= '{$to}
order by date_of_sales desc"
;
return 
$this->db->query($sql);   
                   
$subtotal "select sum(subtotal) as total from sales_line
right join sales on sales_line.fksales = sales.sales_id 
where date_of_sales >= '{
$from}' and date_of_sales <= '{$to}'";         
return 
$this->db->query($subtotal);            

Here’s my view code below:

<?php
foreach($product->result_array() as $row){
echo "<tr>";
echo 
"<td>{$row['product_name']}  </td>";
echo 
"<td>{$row['date_of_sales']}</td>";
echo 
"<td>{$row['qty_purchased']}</td>";
echo 
"<td>{$row['subtotal']}</td>";
echo 
"</tr>";
}
?>

<?php
foreach($product->result_array() as $row)//wont show the total why?
echo "<tr>";
echo 
"<td colspan='4'>{$row['total']}</td>";
echo 
"</tr>";
}
?> 

Ive got an error when i tried to get the value of the total from my second query. How can i display the total as well? Please help me.

 Signature 

Our greatest glory is not in never falling, but in rising every time we fall.

cheese
Email: .(JavaScript must be enabled to view this email address)
Mobile Number: +639216015372

 
Posted: 08 October 2012 12:01 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-11-26
63 posts

You can’t have two(2) returns in the same function (getSalesByDateRange()).  The first return will get you out of that function and back into your controller.  Your best bet, is to get your results from your first query and store it in a variable and then get the results from your second query.  Loop through each of the results and put them into one variable and return that to your controller. 

Regards,

 Signature 

Christian D.

 
Posted: 08 October 2012 01:09 PM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2009-02-19
3800 posts
public function getSalesByDateRange(){
  $data 
= array();
  
//total sales by date range
  
$from $this->input->post('datefrom');
  
$to $this->input->post('dateto');
            
  
$sql "select product_name,date_format(date_of_sales,'%Y/ %M /%d') as         date_of_sales,qty_purchased,subtotal from tbl_product
right join sales_line on tbl_product.product_id = sales_line.fkproduct
right join sales on sales_line.fksales = sales.sales_id 
where date_of_sales >= '{
$from}' and date_of_sales <= '{$to}
order by date_of_sales desc"
;
  
$data['sales'$this->db->query($sql);   
                   
  
$q "select sum(subtotal) as total from sales_line
right join sales on sales_line.fksales = sales.sales_id 
where date_of_sales >= '{
$from}' and date_of_sales <= '{$to}'";         
  
$data['subtotal'$this->db->query($q);

  return 
$data;          

Controller

$data['sales_data'$this->your_model->getSalesByDateRange();
$this->load->view('some_view'$data); 

View

foreach($sales_data['sales'as $row){
//... 
foreach($sales_data['subtotal'as $row){
//... 
 Signature 
 
Posted: 08 October 2012 09:35 PM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2012-08-24
214 posts

Thanks i will try that.

 Signature 

Our greatest glory is not in never falling, but in rising every time we fall.

cheese
Email: .(JavaScript must be enabled to view this email address)
Mobile Number: +639216015372

 
Posted: 09 October 2012 06:31 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2012-08-24
214 posts

Thanks it works!!

 Signature 

Our greatest glory is not in never falling, but in rising every time we fall.

cheese
Email: .(JavaScript must be enabled to view this email address)
Mobile Number: +639216015372