1

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.

luchaninov
  • 6,792
  • 6
  • 60
  • 75
Yasitha
  • 901
  • 2
  • 17
  • 42
  • possible duplicate of [Counting the number of results returned by a database query in Codeigniter](http://stackoverflow.com/a/3667834/1446005) – RandomSeed Jun 15 '13 at 07:06
  • @YaK- yes i also think i'm doing a duplicate thing using this two functions. but any how i have to send row count to create pagination. and then i have to send the values. i don't know is there is a way that i can return those two things in one function. if you need i can post here my controller codes also . – Yasitha Jun 15 '13 at 08:59
  • possible duplicate of [Counting the number of results returned by a database query in Codeigniter](http://stackoverflow.com/questions/3667715/counting-the-number-of-results-returned-by-a-database-query-in-codeigniter) – Alessandro Minoccheri Jun 15 '13 at 09:44

2 Answers2

1

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()
umefarooq
  • 4,540
  • 1
  • 29
  • 38
0

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;
 }
MJ X
  • 8,506
  • 12
  • 74
  • 99