<?php class Student extends CI_Controller {  	private $limit = 5;  	function __construct() 	{ 		parent::__construct(); 		#load library dan helper yang dibutuhkan 		$this->load->library(array('table','form_validation')); 		$this->load->helper(array('form', 'url')); 		$this->load->model('student_model','',TRUE); 	}  	function index($offset = 0, $order_column = 'nrp', $order_type = 'asc') 	{  		if (empty($offset)) $offset = 0; 		if (empty($order_column)) $order_column = 'nrp'; 		if (empty($order_type)) $order_type = 'asc'; 		//TODO: check for valid column 	  		// load data 		$students = $this->student_model->get_paged_list($this->limit, $offset, $order_column, $order_type)->result(); 	  		// generate pagination 		$this->load->library('pagination'); 		$config['base_url'] = site_url('student/index/'); 		$config['total_rows'] = $this->student_model->count_all(); 		$config['per_page'] = $this->limit; 		$config['uri_segment'] = 3; 		$this->pagination->initialize($config); 		$data['pagination'] = $this->pagination->create_links(); 	  		// generate table data 		$this->load->library('table'); 		$this->table->set_empty("&nbsp;"); 		$new_order = ($order_type == 'asc' ? 'desc' : 'asc'); 		$this->table->set_heading( 			'No', 			anchor('student/index/'.$offset.'/nama/'.$new_order, 'Nama'), 			anchor('student/index/'.$offset.'/alamat/'.$new_order, 'Alamat'), 			anchor('student/index/'.$offset.'/jk/'.$new_order, 'Gender'), 			'Actions' 		); 		$i = 0 + $offset; 		foreach ($students as $student){ 			$this->table->add_row(++$i,  								  $student->nama, 								  $student->alamat,  								  strtoupper($student->jk)=='M'? 'Male':'Female',  								   				anchor('student/view/'.$student->nrp,'view',array('class'=>'view')).' '. 				anchor('student/update/'.$student->nrp,'update',array('class'=>'update')).' '. 				anchor('student/delete/'.$student->nrp,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure you want to remove this student?')")) 			); 		} 		$data['table'] = $this->table->generate(); 		 		if ($this->uri->segment(3)=='delete_success') 			$data['message'] = 'The Data was successfully deleted'; 		else if ($this->uri->segment(3)=='add_success') 			$data['message'] = 'The Data has been successfully added'; 		else 			$data['message'] = ''; 		// load view 		$this->load->view('studentList', $data); 	}  	 	function add(){ 		// set common properties 		$data['title'] = 'Add New student'; 		$data['action'] = site_url('student/add'); 		$data['link_back'] = anchor('student/index/','Back to list of students',array('class'=>'back'));  		$this->_set_rules();  		// run validation 		if ($this->form_validation->run() == FALSE){ 			$data['message'] = ''; 					// set common properties 			$data['title'] = 'Add new student'; 			$data['message'] = ''; 			$data['student']['nrp']=''; 			$data['student']['nama']=''; 			$data['student']['alamat']=''; 			$data['student']['jk']=''; 			$data['link_back'] = anchor('student/index/','See List Of student',array('class'=>'back')); 			$this->load->view('studentEdit', $data); 		 		}else{ 			// save data 			$student = array('nama' => $this->input->post('nama'), 							'alamat' => $this->input->post('alamat'), 							'jk' => $this->input->post('jk'), 							); 			$nrp = $this->student_model->save($student);  			// set form input name="id" 			$this->validation->nrp = $nrp;  			redirect('student/index/add_success'); 			 		} 		 	}  	function view($nrp){ 		// set common properties 		$data['title'] = 'student Details'; 		$data['link_back'] = anchor('student/index/','List Of student',array('class'=>'back'));  		// get student details 		$data['student'] = $this->student_model->get_by_id($nrp)->row();  		// load view 		$this->load->view('studentView', $data); 	}    	function update($nrp){ 		// set common properties 		$data['title'] = 'Update student'; 		$this->load->library('form_validation'); 		// set validation properties 		$this->_set_rules(); 		$data['action'] = ('student/update/'.$nrp);  		// run validation 		if ($this->form_validation->run() === FALSE){ 		 			$data['message'] = ''; 			$data['student'] = (array)$this->student_model->get_by_id($nrp)->row(); 			 			$_POST['jk'] = strtoupper($data['student']['jk']); 			 			// set common properties 			$data['title'] = 'Update student'; 			$data['message'] = '';  		 		}else{ 			// save data 			$nrp = $this->input->post('nrp'); 			$student = array('nama' => $this->input->post('nama'), 							'alamat' => $this->input->post('alamat'), 							'jk' => $this->input->post('jk'), 							); 							//var_dump($student); 			$this->student_model->update($nrp,$student); 			$data['student'] = (array)$this->student_model->get_by_id($nrp)->row(); 			// set user message 			$data['message'] = 'update student success'; 		} 		$data['link_back'] = anchor('student/index/','List Of student',array('class'=>'back')); 		// load view 		$this->load->view('studentEdit', $data); 	}  	function delete($nrp){ 		// delete student 		$this->student_model->delete($nrp); 		// redirect to student list page 		redirect('student/index/delete_success','refresh'); 	}  	// validation rules 	function _set_rules(){ 			 		$this->form_validation->set_rules('nama', 'Nama', 'required'); 		$this->form_validation->set_rules('jk', 'JK', 'required'); 		$this->form_validation->set_rules('alamat', 'Alamat', 'required');   	}  	// date_validation callback 	function valid_date($str) 	{ 		if(!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $str)) 		{ 			$this->form_validation->set_message('valid_date', 'date format is not valid. yyyy-mm-dd'); 			return false; 		} 		else 		{ 			return true; 		} 	} } ?>