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

The 'Count Rows' Method

The 'Count Rows' method returns a numeric value representing the number of rows on a database table, where a condition is met. Count Rows is essentially a simplified version of Count Where. 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.
  • $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.

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) is returned.

Example 1

The syntax below shows the simplest Count Rows example possible.  Here we are counting all of the records where the 'last_name' column contains the value of 'Rambo'.

$num_rows = $this->model->count_rows('last_name', 'Rambo');

The code above will produce the following SQL query:

SELECT COUNT(id) as total from tablename where last_name = 'Rambo'

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 Rows' command and then immediately displaying the results using Trongate's json() method:

<?php
class Members extends Trongate {

    function test() {
        $num_rows = $this->model->count_rows('last_name', 'Rambo');
        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_rows('type', 'Mackerel', 'fish');

The code above would produce the following SQL query:

SELECT COUNT(id) as total from fish where type = 'Mackerel'



HELP & SUPPORT

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

 
×