我正在学习 codeigniter 并有一个问题。代码来自http://www.devshed.com/c/a/PHP/Paginating-Database-Records-with-the-Code-Igniter-PHP-Framework/
以下是模型和控制器。它没有定义 $row 但它仍然有效。(原文有错别字,所以我修正了。)
Q1。$row 来自哪里?Q2。你能解释一下 get('users', 5, $rows); 吗?users 必须是 sql 中的表,并且限制为 5,但是为什么我需要 $rows?
在模型中,
// 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();
}
}
在控制器中,
$data['users']=$this->Users_model->getUsers($row);
以下是完整的代码。
用户模型.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');
}
}
以下是控制器的 users.php。
<?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);
}
}