Yasitha
Yasitha

Reputation: 911

Count database rows using where clues

I have a MySQL table and a HTML search form. so according to a user request he can search the database using that HTML form.

I use CodeIgniter and this is the model function i'm using to query the database

public function fetch_categories($limit, $start){

    $user_info = $this->session->userdata('search_info');

    $iam            =   $user_info['iam'];
    $searching_for  =   $user_info['searching_for'];
    $age_from       =   $user_info['age_from'];
    $age_to         =   $user_info['age_to'];
    $country        =   $user_info['country'];
    $Province       =   $user_info['Province'];

    $this->db->where('sex !=', $iam);
    $this->db->where('sex', $searching_for);
    $this->db->where('Age >=' , $age_from);
    $this->db->where('Age <=' , $age_to);
    if($Province != 1){
        $this->db->where('Province' , $Province);
    }

    $this->db->limit($limit, $start);
    $query = $this->db->get("members");
    return $query->result_array();
            }

This is working properly but I need to add pagination to the result so again I use CodeIgniter's built-in pagination library.

So I want to count the result before fetch_categories() function execute so I use search_count() function as this.

public function search(){
     $user_info = $this->session->userdata('search_info');

        $iam            =   $user_info['iam'];
        $searching_for  =   $user_info['searching_for'];
        $age_from       =   $user_info['age_from'];
        $age_to         =   $user_info['age_to'];
        $country        =   $user_info['country'];
        $Province       =   $user_info['Province'];

        $this->db->where('sex !=', $iam);
        $this->db->where('sex', $searching_for);
        $this->db->where('Age >=' , $age_from);
        $this->db->where('Age <=' , $age_to);

        if($Province != 1){
            $this->db->where('Province' , $Province);
        }
            $query = $this->db->count_all('members');
            return $query;       
    }

But this thing always return the whole row count as result. so there is unwanted page numbers in the page. I know it is happening because of this.

$query = $this->db->count_all('members');

But I don't know how to count only the related things using this type of a function.

Upvotes: 1

Views: 217

Answers (2)

MJ X
MJ X

Reputation: 9054

Try this method:

 $this->db->where('sex !=', $iam);
 $this->db->where('sex', $searching_for);
 $this->db->where('Age >=' , $age_from);
 $this->db->where('Age <=' , $age_to);

 if($Province != 1)
 {
    $this->db->where('Province' , $Province);
 }
 $this->db->from('members');
 $query = $this->db->get();
 if($query)
 {
    return $query->num_rows();
 }
 else
 {
    return FALSE;
 }

Upvotes: 0

umefarooq
umefarooq

Reputation: 4574

hi if you want to use where clause with active records count then use this method count_all_results()

 $this->db->where('sex !=', $iam);
 $this->db->where('sex', $searching_for);
 $this->db->where('Age >=' , $age_from);
    $this->db->where('Age <=' , $age_to);

    if($Province != 1){
        $this->db->where('Province' , $Province);
    }
$this->db->count_all_results()

Upvotes: 1

Related Questions