我不能作为数组获取

时间:2015-07-14 09:58:38

标签: php codeigniter

$sql=SELECT COUNT( * ) FROM attendance3 WHERE name = 'Hema' AND attend = 'Absent' AND MONTH = 'July' AND year = '2015' AND user = 'mars'.

在php中我们可以获得计数

$res=mysql_query($sql);
$row=mysql_fetch_array($res);    
$count=$row['COUNT(*)'];////say 4;

我们如何在codeigniter中获得这个?

controller Erp_c
$result['count']= $this->erp_m->selectattend1($user,$en,$year,$month);

模型erp_m

function selectattend1($user,$en,$year,$month)
{
  $query2=$this->db->query(" SELECT COUNT(*)
  FROM attendance3
  WHERE name = '$en'
  AND attend = 'Absent'
  AND MONTH = '$month'
  AND year = '$year'
  AND user = '$user' ");

//return $query2->result(); 
  $result = $query2->result_array();
        $count = count($result);
        return $count;  
  }

在视图页面中,我们将如何获得$ count // $ row重复的次数是我想要的..

我做了这个

foreach($query2 as $row)
{

$count=$row->COUNT(*);////SHOWING ERROR;
}

我想要学生在场或缺席多少天

1 个答案:

答案 0 :(得分:1)

您需要更改模型功能。

function selectattend1($user,$en,$year,$month)
 {
       $where=array('name'=>$en,'attend'=>'Absent','MONTH'=>$month,'year'=>$year,'user'=>$user);
$this->db->where($where);
$res=$this->db->get('attendance3');
$num=$res->num_rows();
 return $num;


}

它将返回找到的记录数,并将分配给
       控制器中$result['count']= $this->erp_m->selectattend1($user,$en,$year,$month);。现在你需要在加载视图时加载这个$ result变量      $this->load->view('filename',$result);

现在您可以直接在视图中使用$count变量。