繁体   English   中英

php codeigniter计数行

[英]php codeigniter count rows

我是codeigniter的新手,我想计算数据库表中的所有行,但查询不能检索确切的行总数。 这是模型

public function countRow(){
$query = $this->db->query("SELECT *,count(id) AS num_of_time FROM home");
    // print_r($query->result());
    return $query->result();
}

这是控制器

public function countTotalrow(){
     $data['query'] = $this->home_model->countRow(); 
}

这是观点

foreach ($num_of_time as $row){
    <span><?php print_r($row->id);?></span>

你可以使用辅助函数$query->num_rows()

它返回查询返回的行数。 你可以像这样使用:

$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();

使用此代码:

$this->db->where(['id'=>2])->from("table name")->count_all_results();

要么

$this->db->from("table name")->count_all_results();

这就是解决同样问题的方法。 我通过创建一个返回查询结果的函数来解决它:

function getUsers(){
$query = $this->db->get('users');
return $query->result();
}

//上面的代码可以放在user_model中,也可以放在你的模型中。

这允许我使用一个函数来获得返回行的结果和数量。

在您需要计数的控制器中使用以下代码以及结果数组()。

//This gives you the user count using the count function which returns and integer of the exact rows returned from the query.
$this->data['user_count'] = count($this->user_model->getUsers());

//This gives you the returned result array.
$this->data['users'] = $this->user_model->getUsers();

我希望这有帮助。

如果你真的想要计算所有行数。 你可以在模型函数中使用它:

$this->db->select('count(*)');
$query = $this->db->get('home');
$cnt = $query->row_array();
return $cnt['count(*)'];

它返回单个值,即行数

用此替换模型函数内的查询

$query = $this->db->query("SELECT id FROM home");

在视图中:

echo $query->num_rows();

你可以试试这个

    $this->db->where('field1',$filed1);
    $this->db->where('filed2',$filed2);
    $result = $this->db->get('table_name')->num_rows();

要计算表中的所有行:

echo $this->db->count_all_results('table_name');

要计算表中的选定行:

echo $this->db->where('name', $name)->count_all_results('table_name');

要计算表中的所有行:

控制器:

function id_cont() {
   $news_data = new news_model();
   $ids=$news_data->data_model();
   print_r($ids);
}

模型:

function data_model() {
    $this->db->select('*');
    $this->db->from('news_data');
    $id = $this->db->get()->num_rows();
    return $id;
}
public function number_row()
 {
    $query = $this->db->select("count(user_details.id) as number")
                       ->get('user_details');              
      if($query-> num_rows()>0)
      {
          return $query->row();
      } 
      else
      {
          return false;
      } 
   }

试试这个:)我创建了我的计数模型所有结果

在library_model中

function count_all_results($column_name = array(),$where=array(), $table_name = array())
{
        $this->db->select($column_name);
        // If Where is not NULL
        if(!empty($where) && count($where) > 0 )
        {
            $this->db->where($where);
        }
        // Return Count Column
        return $this->db->count_all_results($table_name[0]);//table_name array sub 0
}

您的控制器将如下所示

public function my_method()
{
  $data = array(
     $countall = $this->model->your_method_model()
  );
   $this->load->view('page',$data);
}

然后简单调用模型中的库模型

function your_method_model()
{
        return $this->library_model->count_all_results(
               ['id'],
               ['where],
               ['table name']
           );
}
public function record_count() {
   return $this->db->count_all("tablename");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM