Codeigniter get_where query - php

public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return $this->db->count_all_results();
}
This is the function I have in my model that counts all rows in my 'tasks' table where the column 'task_status' is equal to 1. It basically returns an integer that I use for my pagination. The query is not working as it doesn't return any data even though I know I have 4 rows that match that requirement in my table.
I was gonna use the returned value for my pagination. Please help. Thanks

Try
return $q->num_rows();
Or
$this->db->where('task_status', '1');
$query = $this->db->get('tasks');
return $query->num_rows();

$this->db->count_all_results(); is not working after the get function, so do this instead:
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return return $q->num_rows();
}

Related

Codeigniter next_row() returns null

Im trying to get the next row everytim ei run the function but it always returns 0
Model
function getNextMuziek($id)
{
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get('muziek')->num_rows() == 1;
return $query->next_row();
}
According to the documentation, next_row() is to be used to navigate through a result set. This will not serve your purposes. If you want your function to return the next row after the row specified by $id, the easiest thing would be to use a greater than comparison and return the first row. You could modify your code to
$query = $this->db->where('id >', $id)->limit(1)->get('muziek');
if ($query->num_rows() == 1) {
return $query->row();
}

count the not null value using where condition in codeigniter

I am writing a function to count the null column with where condition but there is a problem in this function
protected function _get_mcq_attept_count2( $mcq_id){
$this->load->model('museranswer');
return $this->museranswe>count_by(array('mcq_id'=>$mcq_id,'bookrefrence!='=>" "));
}
this function made the query
SELECT COUNT(*) AS `numrows`
FROM `user_answer`
WHERE `mcq_id` = '321'
AND `bookrefrence` != ' '
this query return the empty column value
I hope this code work for it bcz in code-igniter i always use like this .
protected function _get_mcq_attept_count2($mcq_id)
{
$this->load->model('museranswer');
$where = array('mcq_id'=>$mcq_id);
return $this->museranswe>count_by($where);
}
/******************* FOR MODEL *********************/
public function count_by($where)
{
$this->db->select('count(mcq_id) as numrows');
$this->db->from('user_answer');
$this->db->where($where);
$this->db->where('bookrefrence !=',' ');
$qry = $this->db->get();
return $qry->result_array();
}
Change your query like this array("mcq_id" => "$mcq_id", "bookrefr‌​ence IS NOT NULL" => null). Hope you will get right answer. If it does not work, share your model with us.
return $this->museranswer->count_by(array('mcq_id'=>$mcq_id,'length(bookrefrence)>2'));

what's wrong with my sum query in model php codeigniter?

here's my model code :
function total($id)
{
$this->db->select_sum('score');
$q = $this->db->get('my_table');
$this->db->where('id',$id);
$this->db->group_by('id');
return $q->row()->score;
}
why the output still sum all of row not the specific row with id?
$this->db->get() actually runs the query. You need to call that last.
function total($id)
{
$this->db->select_sum('score');
$this->db->where('id',$id);
$this->db->group_by('id');
$q = $this->db->get('my_table');
return $q->row()->score;
}

CodeIgniter COUNT with active record?

I am working in codeigniter.. I want to count the no. of rows having the same order_no.. below is my code.
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
$this->db->select('*');
$this->db->from('order_details');
$this->db->where('email', $email);
$this->db->group_by('order_no');
$this->db->order_by('order_no', 'DESC');
$query = $this->db->get();
return $query->result();
}
Pls help..
You can use $this->db->count_all_results()
public function get_my_orders() {
$user_data = $this->session->all_userdata();
$email = $user_data['user_email'];
//$this->db->select('*');// no need select as you only want counts
$this->db->from('order_details');
$this->db->where('email', $email);
//$this->db->group_by('order_no');//no need group_by as you only want counts
//$this->db->order_by('order_no', 'DESC');//no need order_by as you only want counts
return $this->db->count_all_results();;
}
Try changing your select line to look like this:
$this->db->select('COUNT(*) as count');
Rather than having all fields accessible like they currently are, you will instead only have access to one variable called count. To keep all variables accessible and add the count in as well, use this instead:
$this->db->select('*, COUNT(*) as count');
Feel free to change the lowercase name for count, I'm just using that as an example.
Check Codeigniter Manual
$query = $this->db->get();
return $query->num_rows(); //Return total no. of rows here
$query->num_rows(); will count the total active record return by your query.

How to use limit correctly in Codeigniter Framework PHP

In my project I want to show 3 rows, for which i use limit('3'), but when my result is greater then 3, content is not showing.
if i limit increase from 3 to 5 or 6 then items again start to show.
how to fixed this problem, so that if item count is more, it shows just 3?
Here is database code, written into Model:
public function select_all_contenta()
{
$this->db->select('*');
$this->db->from('content');
$this->db->order_by('date_add','DESC');
$this->db->limit('3');
$query_result=$this->db->get();
$allcontent_infoa=$query_result->result();
return $allcontent_infoa;
}
public function select_all_contenta() {
$this->db->from('content');
$this->db->order_by('date_add','DESC');
$this->db->limit('3');
$query_result = $this->db->get();
$allcontent_infoa = $query_result->result_array();
return $allcontent_infoa;
}
change
$this->db->limit('3');
into
$this->db->limit ( 3 );
It should be number not string value (limit the number of rows you would like returned by the query)
Try to change this code
$this->db->limit(2,0); //$this->db->limit(3,0);
your code is not optimised.
try this instead
public function select_all_contenta()
{
$q = $this->db->get('content' , 3)->order_by('date_add','DESC');
return $q->result();
}

Categories