$row 从哪里来?
我正在学习 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);
完整代码如下。
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');
}
}
以下是控制器的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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$row 是控制器显示方法中的一个参数,默认值为 0。
第二个和第三个参数是限制和偏移量(请参见此处:http://codeigniter.com/user_guide /database/active_record.html)。因此,第二个参数(限制)定义返回多少行(最大),第三个参数(偏移量)定义从结果集中的哪一行开始。例如,如果您有 10 行并将限制设置为 5,则偏移量 0 将返回前 5 行,偏移量 5 将返回接下来的 5(第二页)行。
显示方法的参数来自地址,它是 /display/ 之后的一段(请参见此处: http://codeigniter.com/user_guide/general/controllers.html#passinguri)。
哦,谢谢你向我指出这个框架,看起来是一个不错的框架,前段时间有人问我这样的事情,现在我知道了;)。
$row is a parameter with a default value of 0 in your Controller's display method.
The second and third parameters are limit and offset (see here: http://codeigniter.com/user_guide/database/active_record.html). So the second parameter (limit) defines how many rows (max) are returned and the third parameter (offset) defines which row from the result set to start from. So for example if you have 10 rows and set limit to be 5, then offset of 0 will return first 5, and offset of 5 will return the next 5 (second page) rows.
The parameter to your display method comes from the address, it's a segment after /display/ (see here: http://codeigniter.com/user_guide/general/controllers.html#passinguri).
Oh and thanks for pointing me at this framework, seems like a nice one, somebody asked me for such a thing some time ago, now I'll know ;).