Welcome Guest, Not a member yet? Register   Sign In
Get foreach data result as a variable
#1

Hello,
I have a controller function to execute a model function, but it seems doesn't work well, i hope you'd like to help me.

Here is my query:
Code:
select round(avg(cast(t_dtl.hasil_m as decimal(6,4))),4) as hasil_m from t_hdr JOIN  t_dtl ON t_hdr.headerid = t_dtl.headerid where id_solvent ='NaOH' AND status='ok' AND concentrate='0.0100' AND date_start <='2016-05-01' AND date_finish >='2016-05-01'

and the result would be like:
Code:
| hasil_m     |
| ----------- |
| 0.0100      |

Then i create the model function:
Code:
function getdata_naoh($datetest2){
   $result = $this->db1->query("select round(avg(cast(t_dtl.hasil_m as decimal(6,4))),4) as hasil_m from t_hdr JOIN  t_dtl ON t_hdr.headerid = t_dtl.headerid where id_solvent ='NaOH' AND status='ok' AND concentrate='0.0100' AND date_start <='$datetest2' AND date_finish >='$datetest2'");
   if($result->num_rows()>0){
       return $result->result_array();
   }else{
       return array();
   }
}

and a controller function to execute it:
Code:
function get_NaOH(){
   $date_analysis = trim($this->input->post('date_analysis'));
   $datetest = trim($this->input->post('date_analysis'));
   $datetest2 = substr($datetest,6,4).'-'.substr($datetest,3,2).'-'.substr($datetest,0,2);

   $dtnaoh = $this->M_tambahan->getdata_naoh($datetest2);

   $data1 = 0;

   foreach($dtnaoh as $row){
       $data1 += floatval($row['hasil_m']);
   }

   $data =  $data1;
   echo $data;
}

I want to get hasil_m column value, according to the controller function, i set variable data1 = 0

then in foreach statement change the value as a result from hasil_m but when i echo variable $data i still getting data1 value = 0.
Reply
#2

What is the debug value of $dtnaoh as you enter the foreach?
Reply
#3

(05-25-2016, 11:07 PM)skunkbad Wrote: What is the debug value of $dtnaoh as you enter the foreach?

I'm sorry, my code is works fine, i've just used a wrong database (development database) it was my bad, thats why the result keep showing 0 because dev database doesn't contain any records. Thank you.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB