CodeIgniter COUNT with active record? - php

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.

Related

Cannot get data after joining from multiple tables in Codeigniter

I want to get data from two tables, the second table is for rating so i want to get rating of products simultaneosly. Below code is not working for me if i change
$this->db->select('dg_products.',', AVG(dg_rating.rating) As
averageRating');
to
$this->db->select('*');
then it is working.
Please help to sort out my issue.
public function get_rating()
{
$this->db->select('dg_products.*','*, AVG(`dg_rating.rating`) As averageRating');
$this->db->from('dg_products');
$this->db->join('dg_rating', 'dg_products.id = dg_rating.product_id','left');
$this->db->where('dg_products.is_featured_prod','1');
$this->db->group_by("dg_products.id");
$query = $this->db->get();
$result = $query->result();
return $result;
}
Try it like this :
$this->db->select('dg_products.*, AVG(`dg_rating.rating`) As averageRating');
you just have an unneeded quotes in there.

How to make a join and search in data using codeigniter and Php

I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();

Codeigniter Active Record is only selecting the first record instead of the most recent

My problem is pretty simple. I'm using codeigniter active record, and all the model class has to do is select the most recent item from a table that belongs to the user.
function get_progress($users_id)
{
$query = $this->db->get('progress');
$this->db->where('user_key', $users_id);
$this->db->order_by("id", "desc");
$this->db->limit(1);
return $query->row_array();
}
Seems simple, but for some reason, it's grabbing the lowest id that matches the user_key.
I've tried changing the where statement to
$this->db->where('id', '2');
And it works, but of course, that's just for troubleshooting. I need variables.
I've rewritten few ways, including using get_where(), and changing desc to asc. No matter what, it's grabbing the low id. How can I select the highest id where user_key is the matching number.
You can try the code below
function get_progress($users_id){
return $this->db->from('progress')
->where('user_key', $users_id)
->order_by("id", "DESC")
->get()
->row();
}
You will get the last recent row as STD Class object
function get_progress($users_id)
{
$this->db->select('*')
->from('progress')
->where('user_key',$users_id)
->order_by('id','desc')
->limit(1);
$q=$this->db->get();
return $q->result_array();
}

CodeIgniter where and like sql query statement

I am trying to do this sql query on codeigniter
SELECT* FROM person WHERE type = 'staff' AND description LIKE 'university%'
I am not sure if it is correct...
$this->db->get_where('person' , array('type'=>'staff'))
->like('description','university%')
->result_array();
does anyone have an idea about my case? thanks in advance ...
Using Active Record you can do so
$query = $this->db->select('*')
->from('person')
->where('type','staff')
->like('description','university','after')
->get();
$result = $query->result_array();
Make sure you pass after as a third parameter in like() function so active record will add the wild card i.e % after university so it will look like LIKE 'university%'
The docs has this explained very well, but their API is very simple. To produce the query you need the relevant code which looks like this:
$this->db->select('*')
->from('person');
->where('type', 'staff')
->like('description','university');
$query = $this->db->get();
$result = $query->result_array();
I never used chaining(even though i know it's possible), but breaking down your question should be easy ;
$this->db->from('person');
$this->db->where('type','staff');
$this->db->where('description', 'university%');
$query = $this->db->get();
$result = $query->result_array();
return $result;

MySql Count not Accurate. Codeigniter

I am currently using Codeigniter. As shown below, a query looks up for the count of rows and then sent it to the view. However, the view does not shows the actual count. The actual count on DB (InnoDB) is 1001 but displays up to 1000 on my view. Any ideas?
Model:
public function check_source_email($location){
$query="SELECT COUNT(*) as row_count FROM feedback WHERE location = '$location' AND source_lead = 'Email' ";
$count=$this->db->query($query)->result();
return $count[0]->row_count;
}
Controller:
$data['email'] = $this->Stats->check_source_email($location);
View:
<strong>Email Listing</strong><span class="pull-right"><?php echo $email; ?></span>
Use num_rows:
public function check_source_email($location){
$query = $this->db->get_where('feedback',array('location'=>$location,'source_lead'=>'Email'));
return $query->num_rows();
}
First, I would try and do things the codeigniter way, if you are working with codeigniter:
public function check_source_email($location) {
return $this->db
->where('location', $location)
->where('source_lead', 'Email')
->count_all_results('feedback');
}
That theoretically should return the correct result, although I'm not sure why your original didn't, but maybe this will fix it.

Categories