About Lesson
The getResult() and getRow() methods retrieve the data from the database. getResult() method returns the list of data objects and the getRow() method returns the single row.
Syntax:
// Get the list of data $this->db->table('table_name')->get()->getResult(); // Get the single data $this->db->table('table_name')->get()->getRow();
1. Copy and Paste the following lines in app/Config/Routes.php
$routes->add('user/list', 'User::list');
2. Create a list.php file in app/Views/ directory
<table width="600" border="1" cellspacing="5" cellpadding="5"> <tr style="background:#CCC"> <th>Sr No</th> <th>First_name</th> <th>Last_name</th> <th>Email Id</th> <th>Update</th> </tr> <?php $i=1; foreach($result as $row) { echo "<tr>"; echo "<td>".$i. "</td>"; echo "<td>".$row->first_name."</td>"; echo "<td>".$row->last_name."</td>"; echo "<td>".$row->email."</td>"; echo "<td><a href="<?php echo base_url('user/edit'.$row->id) ?>">Edit</a></td>"; echo "</tr>"; $i++; } ?> </table>
3. Create a model file UserModel.php in the app/Models/ directory.
<?php namespace AppModels; use CodeIgniterModel; use CodeIgniterDatabase\ConnectionInterface; class UserModel extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; } public function list() { return $this->db ->table('user_info') ->get() ->getResult(); } }
4. Create a controller file User.php in the app/Controllers/ directory.
<?php namespace AppControllers; use AppModelsUserModel; class User extends BaseController { public function __construct() { $db = db_connect(); $this->userModel = new UserModel($db); } public function add() { $this->load->view('add'); } public function list() { $data['result'] = $this->userModel->list(); echo view('list', $data); } }