[eluser]kirkaracha[/eluser]
Controller (controllers/students.php):
Code:
<?php
class Students extends Controller {
public function index() {
// load the model
$this->load->model(array('Students_model'));
// run the database query
$students = $this->Students_model->get_students();
if ($students->num_rows() > 0) {
// show a list of students if there are any in the database
$data = array(
'page_title' => 'Students',
'students' => $students->result(),
);
$this->load->view('students/list',$data);
} else {
// show a message if no students are listed in the database
$data = array(
'page_title' => 'No Students Listed',
'message_text' => 'Sorry, no students are listed.'
);
$this->load->view('shared/display_messages',$data);
}
} // index
} // Students
?>
Model (models/students_model.php):
Code:
<?php
class Students_model extends Model{
function Students_model() {
parent::Model();
$this->table = 'students';
}
function get_students() {
$this->db->select('*');
$this->db->from($this->table);
return $this->db->get();
} // get_students
} // Students_model
?>
View (views/students/list.php):
Code:
<h1><?php echo $page_title; ?></h1>
<ul>
<?php foreach($students as $student): ?>
<li><?php echo $student->first_name; ?> <?php echo $student->last_name; ?></li>
<?php endforeach; ?>
</ul>