[SOLVED] How many rows does a query return?

#1
[eluser]alejandronanez[/eluser]
Hey!

I got a problem, I need to know how many rows does a query return in order to using that value in a FOR cycle on my view file.

I got this code in my model file.

Code:
function obtener_todos(){
$query = $this->db->get('Mytable');
        if($query->num_rows>0){
            foreach($query->result() as $fila){
                $data[]=$fila;
            }
            return $data;
        }
}

this in my Controller file:

Code:
function index(){
    $data['main_content'] = 'area_en_nom_vista';
    $data['filas'] = $this->area_en_nom_model->obtener_todos();
    $data['title'] = 'Area ensenanza nombrado';
        
    $this->load->view('area_en_nom_vista',$data);
}

How do I use the number of rows returned on the query on my view file?

I need help!!!

Thaks a lot!

#2
[eluser]WebsiteDuck[/eluser]
Model
Code:
function obtener_todos(){
$query = $this->db->get('Mytable');
        if($query->num_rows>0){
            $data['num_rows'] = $query->num_rows();
            foreach($query->result() as $fila){
                $data['results'][]=$fila;
            }
            return $data;
        }
}

Controller
Code:
function index(){
    $data['main_content'] = 'area_en_nom_vista';

    $filas = $this->area_en_nom_model->obtener_todos();
    $data['num_filas'] = $filas['num_rows'];
    $data['filas'] = $filas['results'];

    $data['title'] = 'Area ensenanza nombrado';
        
    $this->load->view('area_en_nom_vista',$data);
}

#3
[eluser]alejandronanez[/eluser]
Thanks!!! Thanks!!! Thanks!!!

You saved my life Smile

It works 100% Big Grin

#4
[eluser]Twisted1919[/eluser]
Code:
function obtener_todos()
{
   $query = $this->db->get('Mytable');
   return ($query->num_rows() > 0 ) ? $query->result() : FALSE ;
}
Code:
$array = $this->model->obtener_todos();

if( $array != FALSE && count($array) > 0 )
{
for($i=0;$i<=$array;++$i)
   {
   //do the magic here ....
   }
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.