如何计算控制器中的所有数据数组

时间:2014-07-07 08:48:31

标签: codeigniter postgresql

我想使用codeigniter来计算使用postgresql获得的数据数组。我使用了foreach循环并使用了count($ row ['zero'])来计算,但它仅得到1.请指导我。

这是我的控制器

public function indicators($pcode = '', $dcode = '') {
    $this->data['p_code'] = $pcode;
    $this->data['d_code'] = $dcode;
    $this->data['pcode'] = $this->employment_model->get_project();

        $this->data['attend_household'] = $this->employment_model->household_absent_all($dcode);
        var_dump($this->data['attend_household']);
        die();
        foreach($this->data['attend_household'] as $row){
            if($row['zero']>0){
                      $this->data['zerroo']=count($row['zero']);              
            }
        }
        if (count($this->data['attend_household']) > 0) {
            $this->data['attend_household'] = $this->data['attend_household'][0];
        }

    // var_dump($this->data);
    $this->load->view("employment_info", $this->data);
}

这是我的模特

function household_absent_all($dcode) {
    $dcode_query = "";
    if ($dcode != '') {
        if ($dcode == 'all') {
            $dcode_query = "";
        } else {
            $dcode_query = "where t.dcode='$dcode'";
        }
    }
    $sql = "select (100-(sum(days_worked)/sum(days_offered))*100) as zero,regno,sum(days_offered) as days_offered,sum(days_worked) as days_worked
        from(
            select*
            from assets.tblprojects a
            join(
            select pcode as project,hh_jobcard_no
            from employment.work_group_member
            )wg on a.pcode=wg.project
            join(
            select regno,at.days_offered,at.days_worked                                  
            from employment.registration_hh_member

             join(
            select pcode as a_pcode,hh_jobcard_no as jobcard,mem_id,
            sum((shift_1*0.5)+(shift_2*0.5)) as days_worked,count(adate) as days_offered
            from employment.attendence
            group by a_pcode,jobcard,mem_id
            )at on regno=at.jobcard and s_no=at.mem_id
            )rhm on wg.hh_jobcard_no=rhm.regno                
            )t
            group by regno
            $dcode_query
            ";
    $result = $this->db->query($sql)->result_array();
    return $result;
}

2 个答案:

答案 0 :(得分:0)

如何使用num_rows()作为你的计数器。

if($num = $result->num_rows() >=1){
    $result = array('result_set'=>$result->result_array(),'result_count'=>$num);
}else{
    $result = array('result_set'=>FALSE,'result_count'=>FALSE);
}
return $result;

然后在您的控制器上,您可以将$variable['result_count']用于计数,将$variable['result_set']用作数据结果。

答案 1 :(得分:0)

试试这个

$i=0;
 foreach($this->data['attend_household'] as $row){
            if($row['zero']>0){
                     $i++;           
            }
        }
 $this->data['zerroo']=$i;