php codeigniter count rows

喜夏-厌秋 提交于 2020-01-30 04:09:57

问题


I am new in codeigniter, I want to count all rows from database table but the query dose not retrieve the exact total of rows. Here is Model

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

and this is the controller

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

this is the view

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

回答1:


You could use the helper function $query->num_rows()

It returns the number of rows returned by the query. You can use like this:

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



回答2:


Use this code:

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

or

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




回答3:


If you really want to count all rows. You can use this in model function:

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

It returns single value, that is row count




回答4:


This is what is did that solved the same problem. I solved it by creating a function that returns the query result thus:

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

//The above code can go in the user_model or whatever your model is.

This allows me to use one function for the result and number of returned rows.

Use this code below in your contoller where you need the count as well as the result array().

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

I hope this helps.




回答5:


replace the query inside your model function with this

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

in view:

echo $query->num_rows();



回答6:


You may try this one

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



回答7:


To count all rows in a table:

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

To count selected rows from table:

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



回答8:


To count all rows in a table:

Controller:

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

Model:

function data_model() {
    $this->db->select('*');
    $this->db->from('news_data');
    $id = $this->db->get()->num_rows();
    return $id;
}



回答9:


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;
      } 
   }



回答10:


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



回答11:


Try This :) I created my on model of count all results

in 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
}

Your Controller will look like this

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

Then Simple Call The Library Model In Your Model

function your_method_model()
{
        return $this->library_model->count_all_results(
               ['id'],
               ['where],
               ['table name']
           );
}


来源:https://stackoverflow.com/questions/28958307/php-codeigniter-count-rows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!