Welcome Guest, Not a member yet? Register   Sign In
display from database
#2

[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>&lt;?php echo $page_title; ?&gt;</h1>

<ul>
&lt;?php foreach($students as $student): ?&gt;
    <li>&lt;?php echo $student->first_name; ?&gt; &lt;?php echo $student->last_name; ?&gt;</li>
&lt;?php endforeach; ?&gt;
</ul>


Messages In This Thread
display from database - by El Forum - 02-09-2010, 04:38 AM
display from database - by El Forum - 02-09-2010, 12:35 PM
display from database - by El Forum - 02-09-2010, 10:14 PM
display from database - by El Forum - 02-09-2010, 10:44 PM
display from database - by El Forum - 02-09-2010, 11:23 PM
display from database - by El Forum - 02-10-2010, 12:11 AM
display from database - by El Forum - 02-10-2010, 12:19 AM
display from database - by El Forum - 02-10-2010, 12:29 AM
display from database - by El Forum - 02-10-2010, 12:59 AM
display from database - by El Forum - 02-10-2010, 02:17 AM
display from database - by El Forum - 02-10-2010, 02:29 AM
display from database - by El Forum - 02-10-2010, 02:37 AM
display from database - by El Forum - 02-10-2010, 03:27 AM
display from database - by El Forum - 02-10-2010, 04:34 AM
display from database - by El Forum - 02-10-2010, 04:49 AM
display from database - by El Forum - 02-10-2010, 05:05 AM
display from database - by El Forum - 02-10-2010, 10:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB