student 的 controller 和 view
Controller: student.php
<?php
class Student extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
// redirect to the listing page
$this->listing();
}
public function add() {
$this->load->helper('form');
// display information for the view
$data['title'] = "Classroom: Add Student";
$data['headline'] = "Add a New Student";
$data['include'] = 'student_add';
$data['uid'] = $this->session->userdata('uid');
$data['is_admin'] = $this->session->userdata('is_admin');
$this->load->view('template', $data);
}
public function create() {
$this->load->helper('url');
$this->load->model('MStudent','',TRUE);
$this->MStudent->add_student($_POST);
redirect('student/listing','refresh');
}
public function listing()
{
$this->load->library('table');
$this->load->model('MStudent','',TRUE);
if ($this->session->userdata('is_admin')) {
$students = $this->MStudent->list_all_students();
} else {
$students = $this->MStudent->list_students($this->session->userdata('uid'));
}
// display information for the view
$data['title'] = "Classroom: Student Listing";
$data['headline'] = "Student Listing";
$data['include'] = 'student_listing';
$data['uid'] = $this->session->userdata('uid');
$data['is_admin'] = $this->session->userdata('is_admin');
$data['students'] = $students;
$this->load->view('template', $data);
}
public function edit()
{
$this->load->helper('form');
$id = $this->uri->segment(3);
$this->load->model('MStudent','',TRUE);
$data['row'] = $this->MStudent->get_student($id)->result();
// display information for the view
$data['title'] = "Classroom: Edit Student";
$data['headline'] = "Edit Student Information";
$data['include'] = 'student_edit';
$data['uid'] = $this->session->userdata('uid');
$data['is_admin'] = $this->session->userdata('is_admin');
$this->load->view('template', $data);
}
public function update()
{
$this->load->model('MStudent','',TRUE);
$this->MStudent->update_student($_POST['id'], $_POST);
redirect('student/listing','refresh');
}
public function delete()
{
$id = $this->uri->segment(3);
$this->load->model('MStudent','',TRUE);
$this->MStudent->delete_student($id);
redirect('student/listing','refresh');
}
}
/* End of file student.php */
/* Location: ./application/controllers/student.php */
View: student_add.php
<?php
echo form_open('student/create');
// an array of the fields in the student table
$field_array = array('s_name' => 'Student Name','p_name' => 'Parent Name','address' => 'Address','city' => 'City','state' => 'State','zip' => 'Zip','phone' => 'Phone','email' => 'Email');
echo form_hidden('user_id', $uid);
foreach($field_array as $field => $label) {
echo '<p>';
echo form_label($label, $field);
echo form_input(array('name' => $field, 'value' => set_value($field)));
echo '</p>';
}
// not setting the value attribute omits the submit from the $_POST array
echo form_submit('', 'Add');
echo form_close();
/* End of file student_add.php */
/* Location: ./application/views/student_add.php */
View: student_edit.php
<?php
echo form_open('student/update');
// an array of the fields in the student table
$field_array = array('s_name' => 'Student Name','p_name' => 'Parent Name','address' => 'Address','city' => 'City','state' => 'State','zip' => 'Zip','phone' => 'Phone','email' => 'Email');
echo form_hidden('id', $row[0]->id);
foreach($field_array as $field => $label) {
echo '<p>';
echo form_label($label, $field);
echo form_input(array('name' => $field, 'value' => $row[0]->$field));
echo '</p>';
}
// not setting the value attribute omits the submit from the $_POST array
echo form_submit('', 'Update');
echo form_close();
/* End of file student_edit.php */
/* Location: ./application/views/student_edit.php */
View: student_listing.php
<?php
// generate HTML table from query results
$tmpl = array (
'table_open' => '<table>',
'heading_row_start' => '<tr class="table_header">',
'row_start' => '<tr class="odd_row">'
);
$this->table->set_template($tmpl);
$this->table->set_empty(" ");
$this->table->set_heading('', 'Child Name', 'Parent Name', 'Address',
'City', 'State', 'Zip', 'Phone', 'Email');
$table_row = array();
foreach ($students as $student) {
$table_row = NULL;
$table_row[] = '<span style="white-space: nowrap">' .
anchor('student/edit/' . $student['id'], 'edit') . ' | ' .
anchor('student/delete/' . $student['id'], 'delete',
"onclick=\" return confirm('Are you sure you want to '
+ 'delete the record for ".addslashes($student['s_name'])."?')\"") .
'</span>';
$table_row[] = htmlspecialchars($student['s_name']);
$table_row[] = htmlspecialchars($student['p_name']);
$table_row[] = htmlspecialchars($student['address']);
$table_row[] = htmlspecialchars($student['city']);
$table_row[] = $student['state'];
$table_row[] = $student['zip'];
$table_row[] = $student['phone'];
$table_row[] = mailto($student['email']);
$this->table->add_row($table_row);
}
$students_table = $this->table->generate();
echo $students_table;
if (!$is_admin) {
echo '<br />';
echo anchor('student/add', 'Add Your Student');
}
/* End of file student_listing.php */
/* Location: ./application/views/student_listing.php */