Trongate Docs
switch to dark modeswitch to dark mode
»
»
The 'Count Where' Method

The 'Count Where' Method

The 'Count Where' method returns a numeric value representing the number of rows on a database table, where a condition is met. It accepts the following parameters:

  • $column (required) - the name of the table column referred to when results are fetched.
  • $value (required) - the value that should be matched against the target table column.
  • $operator (optional) - the comparison operator that will be used to fetch records. By default this is set to the equals symbol ('=').
  • $order_by (optional) - the name of the table column by which results should be ordered. In instances where you'd like your results to be displayed in descending order, add the characters ' desc' onto your order by argument. By default, the Model will order results by 'id'.
  • $target_table (optional) - the name of the database table to be queried. By default, the Model will assume the target table to be equal to the value on the first URL segment.
  • $limit (optional) - the maximum number of results that you would like to return. By default, the Model will assume no limit.
  • $offset (optional) - the number of rows to skip before fetching results.

Just To Let You Know
The starting point for a database results set is always assumed to have an offset of 0.

What Gets Returned?

WHEN MATCHING RECORDS ARE FOUND

If matching records are found then an integer, representing the number of rows on the table, is returned.

WHEN MATCHING RECORDS ARE NOT FOUND

If the query fails to find any matching records then the value 0 (zero) will be returned.

Example 1

The syntax below shows the simplest Count Where example possible.  Here we are counting all of the records where the 'first_name' column contains the value of 'John'.

$num_rows = $this->model->count_where('first_name', 'John');

The code above will produce the following SQL query:

SELECT * FROM tablename where first_name = 'John' order by id

Just To Let You Know
In a working example, 'tablename' would be replaced with the first segment from the URL.

Below is an example of a Members.php controller file that contains a test() method.  Here we're invoking a basic 'Count Where' command and then immediately displaying the results using Trongate's json() method:

<?php
class Members extends Trongate {

    function test() {
        $num_rows = $this->model->count_where('first_name', 'John');
        echo $num_rows;
    }


Below shows an example of the kind of output that we can expect to see from the above method, when Debug Mode is switched on:

screenshot

Example 2

Below is a more advanced example:

$num_rows = $this->model->count_where('trongate_user_id', 1, '!=', 'id desc', 'members', 99, 0);

The code above would produce the following SQL query:

SELECT * FROM members where trongate_user_id != 1 order by id desc LIMIT 0, 99

Just To Let You Know
The 'Count Where' Method works by invoking a 'Get Where Custom' command and then returning the number of rows that the query yielded.


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×