Welcome Guest, Not a member yet? Register   Sign In
How to get the number of rows after database result ?
#1

[eluser]ludo31[/eluser]
Hello ;
I would like to know the number of rows of database research , I need it for pagination
here is my controller
Code:
public function gestionHomme()
        {
            $data['rows'] = $this->client_model->getHomme();
            
                  
            
              $config['base_url']='http://localhost/MonSite/index.php/client/gestionHomme';
              
            

           [b] $config['total_rows']= $this->client_model->getRow(1);[/b]
                        
            $config['num_links']=5;  
            $config['per_page']=5;
            $config['full_tag_open']='<div id="pagination">';
             $config['full_tag_close']='</div>';
             $config['next_link']='>>';
              $config['prev_link']='<<';
              $this->pagination->initialize($config);
              
          $data['page']=  $this->db->get('chaussure',  $config['per_page'],  $this->uri->segment(3));
            
           // $data['page'] = $this->pagination();
            
            // var_dump($data); exit;
            
            $this->load->view('homme_view',$data);
        }

the problem is in :

Code:
$config['total_rows']=
in normal we add this in :
Code:
$config['total_rows']=  $this->db->get('gnr_convenir')->num_rows();

Code:
but in my case I like to have the number of rows in table  gnr_convenir where identifiant_genre (attribute) = 1

I try to create a function for that in model

Code:
$query = $this->db->get('gnr_convenir');
           $query = $this->db->where('identifiant_genre', $genre);
          
           return $query;
and call this function like that in view

Code:
$config['total_rows']=  $this->clientModel->getRow(1);

but I have error

I try like this

Code:
return  $query->num_rows();

Code:
Fatal error: Call to undefined method CI_DB_mysql_driver::num_rows() in C:\wamp\www\MonSite\application\models\client_model.php on line 24

so in final I try to make like this

Code:
public function getRow($genre)
       {
$query = $this->db->query('SELECT identifiant_genre FROM gnr_convenir WHERE identifiant_genre=\'.$genre.'\'');
return $query;
}

Code:
Parse error: syntax error, unexpected T_NS_SEPARATOR in C:\wamp\www\MonSite\application\models\client_model.php on line 22

as do you know the problem and how to resolve this problem

thanks
#2

[eluser]Rolly1971[/eluser]
why not use CI Active Record:

Code:
public function getRow($genre)
{
$this->db->select('identifiant_genre');
$this->db->from('gnr_convenir')';
$this->db-where('identifiant_genre', $genre);
$query = $this->db->get();

return $query;
}
#3

[eluser]ludo31[/eluser]
[quote author="Rolly1971" date="1330268600"]why not use CI Active Record:

Code:
public function getRow($genre)
{
$this->db->select('identifiant_genre');
$this->db->from('gnr_convenir')';
$this->db-where('identifiant_genre', $genre);
$query = $this->db->get();

return $query;
}
[/quote]

but here
Code:
[b] $config['total_rows']= $this->client_model->getRow(1);[/b]

$query here return an object as array not the number of line
so I would like to know the number of line of the result of your query so if we make

Code:
$data['result'] = $this->client_model->getRow(1);
$config['total_rows']= $data.????num_rows????

probably we must choose sizeof($array) to know the number

[code]

$query['result'] = $this->client_model->getRow(1);
     $nombre_ligne = sizeof($query);
     echo $nombre_ligne;
     var_dump($query);exit;
array
'result' =>
object(CI_DB_mysql_driver)[13]
public 'dbdriver' => string 'mysql' (length=5)
public '_escape_char' => string '`' (length=1)
public '_like_escape_str' => string '' (length=0)
public '_like_escape_chr' => string '' (length=0)
public 'delete_hack' => boolean true
public '_count_string' => string 'SELECT COUNT(*) AS ' (length=19)
public '_random_keyword' => string ' RAND()' (length=7)
public 'use_set_names' => boolean false
public 'ar_select' =>
array
[/code]

I think it's an error because there are a lot of rows in phpAdmin when I see it
#4

[eluser]CroNiX[/eluser]
Well, you never retrieve the result, so it can't calculate the rows retrieved.
Code:
public function getRow($genre)
{
  $query = $this->db
    ->select('identifiant_genre')
    ->where('identifiant_genre', $genre)
    ->get('gnr_convenir')
    ->result();  //need to get a result first!!

  return $query->num_rows();
}
#5

[eluser]Aken[/eluser]
Or, with less code...

Code:
public function getRow($genre)
{
  return $this->db
    ->from('gnr_convenir')
    ->where('identifiant_genre', $genre)
    ->count_all_results();
}
#6

[eluser]ludo31[/eluser]
[quote author="Aken" date="1330325834"]Or, with less code...

Code:
public function getRow($genre)
{
  return $this->db
    ->from('gnr_convenir')
    ->where('identifiant_genre', $genre)
    ->count_all_results();
}
[/quote]

thanks a lot it works

Code:
public function getRow($genre)
       {
           // genre 1 homme 2 femme 3 enfant
            $query =  $this->db->select('identifiant_chaussure');
    $query =  $this->db->from('gnr_convenir');
           $query = $this->db->where('identifiant_genre', $genre);
          
   $query = $this->db->count_all_results();
  
   echo $query ; exit ;
  
   //return $query ;
  
          
        
       }




Theme © iAndrew 2016 - Forum software by © MyBB