Welcome Guest, Not a member yet? Register   Sign In
Help needed to Select Count,.
#1

[eluser]quest13[/eluser]
Hi, I need the following in CI, hope someone can help me.


I want to select and count one of the field as well under where condition.
How do I do in CI ?


for ex. Select (some fields here) Count(imgid) from table where productid=$productid


thanks in advance
#2

[eluser]TheFuzzy0ne[/eluser]
Are you referring to the Active Record class? If so, you can pass in FALSE as the second parameter to $this->db->select() to prevent it from escaping the select statement.
#3

[eluser]quest13[/eluser]
Sorry, I am not able to solve the problem. Can you kindly explain with an example ?

I want to count a specific id, but display all the records pertaining to some condition.


Thanks in advance
#4

[eluser]TheFuzzy0ne[/eluser]
Please paste what you have so far, so I can hopefully give you an example that will work.
#5

[eluser]Evil Wizard[/eluser]
f you execute the sql immediately after the query you should be able to return the total number of found rows even while a limit/offset is present Wink
Code:
SELECT FOUND_ROWS() AS `FoundRows`;
then just access it like any other value from the database
#6

[eluser]Michael Wales[/eluser]
Quote:f you execute the sql immediately after the query you should be able to return the total number of found rows even while a limit/offset is present
Why would you issue another request to the database server and wait for a response when mysql_num_rows() does the same thing? or num_rows() within CodeIgniter's ActiveRecord library.

To the OP:
Code:
function get_all() {
  $this->db->select('title, filename, COUNT(*) AS total', FALSE);
  $query = $this->db->get('files');
  if ($query->num_rows() > 0) {
    return $result;
  }
  return FALSE;

Code:
$files = $this->model->get_all();
echo $files->total;
#7

[eluser]Evil Wizard[/eluser]
because like I said if you put a limit on the sql the count(*) will be a count of the resultset not of the possible number of results

EDIT:
I just tested it on MySQL server and yes the count(*) tagged onto the end of the query works just as well and returns number of records that match the where clause but not impeded by the limit Smile
#8

[eluser]Michael Wales[/eluser]
If you put a limit on the SQL, num_rows() is still going to return the same thing as FOUND_ROWS().




Theme © iAndrew 2016 - Forum software by © MyBB