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

The 'Count' Method

The 'Count' method returns a numeric value representing the number of rows on a database table. It accepts the following optional parameter:

  • $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 will be returned that represents the number of rows that the query yielded.

WHEN MATCHING RECORDS ARE NOT FOUND

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

Example 1

The syntax below shows the simplest Count example possible.  As you can see, no arguments are required to make this work.

$num_rows = $this->model->count();

The code above will produce the following SQL query:

SELECT COUNT(id) as total FROM tablename

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

<?php
class Members extends Trongate {

    function test() {
        $num_rows = $this->model->count();
        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:

$rows = $this->model->count('fish');

The code above would produce the following SQL query:

SELECT COUNT(id) as total FROM fish



HELP & SUPPORT

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

 
×