php codeigniter 计数行
php codeigniter count rows
我是 codeigniter 的新手,我想计算数据库 table 中的所有行,但查询没有检索到确切的行总数。
这是型号
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>
如果你真的想计算所有行数。您可以在模型函数中使用它:
$this->db->select('count(*)');
$query = $this->db->get('home');
$cnt = $query->row_array();
return $cnt['count(*)'];
是returns单值,即行数
用这个
替换模型函数中的查询
$query = $this->db->query("SELECT id FROM home");
可见:
echo $query->num_rows();
您可以使用辅助函数 $query->num_rows()
它returns查询返回的行数。你可以这样使用:
$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();
试试这个 :)
我创建了计算所有结果的模型
在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']
);
}
计算table中的所有行:
控制器:
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;
}
这就是解决同样问题的方法。我通过创建一个 returns 查询结果的函数来解决它:
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 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;
}
}
public function record_count() {
return $this->db->count_all("tablename");
}
我是 codeigniter 的新手,我想计算数据库 table 中的所有行,但查询没有检索到确切的行总数。 这是型号
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>
如果你真的想计算所有行数。您可以在模型函数中使用它:
$this->db->select('count(*)');
$query = $this->db->get('home');
$cnt = $query->row_array();
return $cnt['count(*)'];
是returns单值,即行数
用这个
替换模型函数中的查询$query = $this->db->query("SELECT id FROM home");
可见:
echo $query->num_rows();
您可以使用辅助函数 $query->num_rows()
它returns查询返回的行数。你可以这样使用:
$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();
试试这个 :) 我创建了计算所有结果的模型
在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']
);
}
计算table中的所有行:
控制器:
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;
}
这就是解决同样问题的方法。我通过创建一个 returns 查询结果的函数来解决它:
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 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;
}
}
public function record_count() {
return $this->db->count_all("tablename");
}