如何使用codeigniter进行计数评论,

时间:2015-08-06 06:30:15

标签: php codeigniter

我正在从事一个项目。如何按身份计算评论?

例如

CONTROLER:

public function comments() {

$id_alat = $this->db->where('id_alat');
$com = $this->mcrud->getComent($id_alat);
$com = $this->mcrud->getComent($id_alat);
$data = array (
                'com' => $com,
                'content' => 'instrument/instrument');
$this->load->view('layouts/wrapper', $data);
 }

模型:

public function getComent($id_alat) {

$sql = "SELECT count (*) as num FROM WHERE tbcoment $id_alat tbcoment.id_alat = {}";
$this->db->query($sql);
} 

查看:

comments: <?php echo $com; ?>

3 个答案:

答案 0 :(得分:1)

  

注意:不要在php标记和变量中使用空格。

     

Ex01:$ id_alat应来$id_alat
  Ex02:$ this-> mcrud-> getComent ($ id_alat);应该$this->mcrud-> getComent($id_alat);

代码示例

在控制器中

function comments () {

    $id_alat = '';//Asign data to here
    $data['com'] = $this->Model_name->getComent($id_alat);
    $data['content'] = 'instrument / instrument';

    $this->load->view ('layouts/wrapper', $data);
}

在模型中

function getComent($id_alat) {

    $query =$this->db->query("SELECT * FROM table_name WHERE tbcoment='$id_alat'");//cahnge table name, and argument that you want
    $result = $query->result_array();
    $count = count($result);
    return $count;
}

在视图中

comments: <?php echo $com; ?>

答案 1 :(得分:1)

使用以下代码进行模型

您的模型

public function getComent($id_alat) 
{
    $sql = "SELECT count (*) as num FROM WHERE tbcoment.id_alat = '$id_alat'";
    $res=$this->db->query($sql)->row_object();
    return $res->num; 
} 

答案 2 :(得分:0)

您也可以使用此代码:

$this->db->where('id',$id)
         ->from('table_name')
         ->count_all_results();

这可以在MVC(模型,视图,控制器)上使用;

您可以在用户指南中找到此代码