简体   繁体   中英

Where does $row come from?

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);
}
}

$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 ;).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM