I am learning codeigniter and have a question. The codes are from http://www.devshed.com/c/a/PHP/Paginating-Database-Records-with-the-Code-Igniter-PHP-Framework/
The followings are model and controller. And it does not define $row but it still works. (The original had typo, so I fixed it.)
Q1. Where does $row come from? Q2. Could you explain get('users', 5, $rows);? Users must be the table in sql, and limit 5, but why do I need $rows?
In model,
// get 5 rows at a time
function getUsers($row)
{
$query=$this->db->get('users',5,$row);
if($query->num_rows()>0)
{
// return result set as an associative array
return $query->result_array();
}
}
In controller,
$data['users']=$this->Users_model->getUsers($row);
The followings are the complete codes.
Users_model.php
<?php
class Users_model extends Model
{
function Users()
{
// call the Model constructor
parent::Model();
// load database class and connect to MySQL
// $this->load->database();
}
function getAllUsers()
{
$query=$this->db->get('users');
if($query->num_rows()>0)
{
// return result set as an associative array
return $query->result_array();
}
}
function getUsersWhere($field,$param)
{
$this->db->where($field,$param);
$query=$this->db->get('users');
// return result set as an associative array
return $query->result_array();
}
// get 5 rows at a time
function getUsers($row)
{
$query=$this->db->get('users',5,$row);
if($query->num_rows()>0)
{
// return result set as an associative array
return $query->result_array();
}
}
// get total number of users
function getNumUsers()
{
return $this->db->count_all('users');
}
}
The following is users.php for controller.
<?php
class Users extends Controller{
function Users(){
// load controller parent
parent::Controller();
// load 'Users' model
$this->load->model('Users_model');
}
function display($row=0){
// load pagination library
$this->load->library('pagination');
// set pagination parameters
$config['base_url']='http://127.0.0.1/ci_day4/index.php/users/display/';
$config['total_rows']=$this->Users_model->getNumUsers();
$config['per_page']='5';
$this->pagination->initialize($config);
// store data for being displayed on view file
$data['users']=$this->Users_model->getUsers($row);
$data['title']='Displaying user data';
$data['header']='User List';
$data['links']=$this->pagination->create_links();
// load 'testview' view
$this->load->view('users_view',$data);
}
}