Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere does $row come from?
    text
    copied!<p>I am learning codeigniter and have a question. The codes are from <a href="http://www.devshed.com/c/a/PHP/Paginating-Database-Records-with-the-Code-Igniter-PHP-Framework/" rel="nofollow noreferrer">http://www.devshed.com/c/a/PHP/Paginating-Database-Records-with-the-Code-Igniter-PHP-Framework/</a></p> <p>The followings are model and controller. And it does not define $row but it still works. (The original had typo, so I fixed it.) </p> <p>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? </p> <p>In model,</p> <pre><code>// get 5 rows at a time function getUsers($row) { $query=$this-&gt;db-&gt;get('users',5,$row); if($query-&gt;num_rows()&gt;0) { // return result set as an associative array return $query-&gt;result_array(); } } </code></pre> <p>In controller, </p> <pre><code>$data['users']=$this-&gt;Users_model-&gt;getUsers($row); </code></pre> <p>The followings are the complete codes.</p> <p>Users_model.php</p> <pre><code>&lt;?php class Users_model extends Model { function Users() { // call the Model constructor parent::Model(); // load database class and connect to MySQL // $this-&gt;load-&gt;database(); } function getAllUsers() { $query=$this-&gt;db-&gt;get('users'); if($query-&gt;num_rows()&gt;0) { // return result set as an associative array return $query-&gt;result_array(); } } function getUsersWhere($field,$param) { $this-&gt;db-&gt;where($field,$param); $query=$this-&gt;db-&gt;get('users'); // return result set as an associative array return $query-&gt;result_array(); } // get 5 rows at a time function getUsers($row) { $query=$this-&gt;db-&gt;get('users',5,$row); if($query-&gt;num_rows()&gt;0) { // return result set as an associative array return $query-&gt;result_array(); } } // get total number of users function getNumUsers() { return $this-&gt;db-&gt;count_all('users'); } } </code></pre> <p>The following is users.php for controller.</p> <pre><code>&lt;?php class Users extends Controller{ function Users(){ // load controller parent parent::Controller(); // load 'Users' model $this-&gt;load-&gt;model('Users_model'); } function display($row=0){ // load pagination library $this-&gt;load-&gt;library('pagination'); // set pagination parameters $config['base_url']='http://127.0.0.1/ci_day4/index.php/users/display/'; $config['total_rows']=$this-&gt;Users_model-&gt;getNumUsers(); $config['per_page']='5'; $this-&gt;pagination-&gt;initialize($config); // store data for being displayed on view file $data['users']=$this-&gt;Users_model-&gt;getUsers($row); $data['title']='Displaying user data'; $data['header']='User List'; $data['links']=$this-&gt;pagination-&gt;create_links(); // load 'testview' view $this-&gt;load-&gt;view('users_view',$data); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload