php codeigniter count rows


php codeigniter count rows

我是代码点火器的新手,我想计算数据库表中的所有行,但查询剂量没有检索到确切的行总数。这是模型

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();

你可以试试这个

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

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

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

它返回单个值,即行计数

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

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();

我希望这有所帮助。

public function record_count() {
   return $this->db->count_all("tablename");
}

模型函数中的查询替换为以下内容

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

在视图:

echo $query->num_rows();

对表中的所有行进行计数:

控制器:

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']
           );
}