Sammy30
3/5/2014 - 7:20 AM

Set many function in codeigniter

Set many function in codeigniter

<?php

class Siswa extends CI_Controller
{
	private $limit=10;

	function __construct()
	{
		parent::__construct();
		#load library dan helper
		$this->load->library(array('table','form_validation'));
		$this->load->helper(array('form','url'));
		$this->load->model('model_siswa','',TRUE);
	}

	function index($offet=0,$order_column='id',$order_type='asc')
	{
		if (empty($offset)) $offset=0;
		if (empty($order_column)) $order_column='id';
		if (empty($order_type)) $order_type='asc';
		//TOdo check valid column

		//Load data siswa
		$siswas=$this->model_siswa->get_page_list($this->limit,$offset,$order_column,$order_type)->result();

		//generate pagination
		$this->load->library('pagination');
		$config['base_url']= site_url('siswa/index');
		$config['total_rows']= $this->model_siswa->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('siswa/index'.$offset.'/nama/'.$new_order,'Nama');
				anchor('siswa/index'.$offset.'/alamat/'.$new_order,'Alamat');
				anchor('siswa/index'.$offset.'/jenis_kelamin/'.$new_order,'Jenis Kelamin');
				anchor('siswa/index'.$offset.'/tanggal_lahir/'.$new_order,'Tanggal Lahir (dd-mm-yyyy)'),'Action'
			);
				
				$i=0+$offset;
				foreach ($siswas as $siswa) 
					{
					$this->table->add_row
						{
							++$i,$siswa=nama,$siswa=alamat,strtoupper
							($siswa->jenis_kalamin=='M'?'Laki-laki':'Perempuan', date
								('d-m-Y',strtotime($siswa->tanggal_lahir)),
								 
								 anchor('siswa/view'.$siswa->id,'view',array('class'=>'view')).''.
								 anchor('siswa/update'.$siswa->id,'update',array('class'=>'update')).''.
								 anchor('siswa/delete'.$siswa->id,'delete',array('class'=>'delete')).''.
								 'onclick'=>"return confirm(Apakah anda yakin ingin menghapus data siswa?')");
						}
					}

		$data['table']=$this->table->generate();
		if ($this->uri->segment(3)=='delete_success')
			$data['message']='Data berhasil dihapus';
		else if ($this->uri->segment(3)=='add_success')
			$data['message']='Data berhasil di tambah';
		else
			$data['message']='';
		//load view
		$this->load->view('siswaList',$data);
			
	}

	function add()
	{
		//set common properties
		$data['title']='Tambah siswa baru';
		$data['action']=site_url('siswa/add');
		$data['lick_back']=anchor('siswa/index/','Back to list of siswa',array('class'=>'back'));

		$this->_set_rules();

		//run validation
		if ($this->form_validation->run()=== FALSE)
		{
			$data['message']='';
			//set common properties
			$data['title']='Add new siswa';
			$data['message']='';
			$data['siswa']['id']='';
			$data['siswa']['nama']='';
			$data['siswa']['alamat']='';
			$data['siswa']['jenis_kalamin']='';
			$data['siswa']['tanggal_lahir']='';
			$data['link_back']=anchor('siswa/index/','Lihat daftar siswa',array('class'=>'back'));
			$this->load->view('siswaEdit',$data);
		}
		else
		{
			//save data
			$siswa=array('nama'=>$this->input->post('nama'),
				'alamat'=>$this->input->post('alamat'),
				'jenis_kalamin'=>$this->input->post('jenis_kalamin'),
				'tanggal_lahir'=>date('y-m-d',strtotime($this->input->post('tanggal_lahir'))
					));
			$id=$this->model_siswa->save($siswa);

			//set form input nama="id"
			$this->validation->id =$id;

			redirect('siswa/index/add_success');
		}
	}

	function view($id)
	{
		//set common properties
		$data['title']='siswa details';
		$data['link_back']=anchor('siswa/index/',
			'lihat daftar siswas',array('calss'=>'back'));

		//get siswa details
		$data['siswa']=>$this->model_siswa->get_by_id($id)->row();

		//load view
		$this->load->view('siswaView',$data);
	}

	function update($id)
	{
		//set common properties
		$data['title']='Update siswa';
		$this->load->library('form_validation');

		//set validation properties
		$this->set_rules();
		$data['action']=('siswa/update'.$id);

		//run validation
		if ($this->form_validation->run()=== FALSE)
		 {
		 	$data['message']='';
		 	$data['siswa']=$this->model_siswa->get_by_id($id)->row_array();
		 	$_POST['jenis_kalamin']=strtoupper($data['siswa']['jenis_kalamin']);
		 	$data['siswa']['tanggal_lahir']=date('d-m-y',strtoupper($data['siswa']['tanggal_lahir']));

			//set common properties
			$data['title']='Update siswa';
			$data['message']='';


		 }
		 else
		 {
		 	//save data
		 	$id=$this->input->post('id');
		 	$siswa= array
		 		(
		 		'nama'=>$this->input->post('nama'),
		 		'alamat'=>$this->input->post('alamat'),
		 		'jenis_kalamin'=>$this->input->post('jenis_kalamin'),
		 		'tanggal_lahir'=>date('y-m-d',strtoupper($this->input->post('tanggal_lahir'))),
		 		)
		 	$this->model_siswa->update($id,$siswa);
		 	$data['siswa']=$this->model_siswa->get_by_id($id)->row_array();

		 	//set user message
		 	$data['message']='update siswa success';
		 }

		$data['link_back']=anchor('siswa/index/', 'lihat daftar siswa', array('class'=>'back'));
		
		//load view
		$this->load->view('siswaEdit',$data); 
	}

	function delete($id)
	{
		//delete siswa
		$this->model_siswa->delete($id);

		//redirect to siswa list page
		redirect('siswa/index/delete_success','refresh');
	}

	//Validation rules
	function _set_rules($id)
	{
		$this->form_validation->set_rules('nama','Nama','required|trim');
		$this->form_validation->set_rules('jenis_kalamin','Password','required');
		$this->form_validation->set_rules('alamat','Alamat','required|callback_valid_date');
		$this->form_validation->set_rules('tanggal_lahir','Tanggal_lahir','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. yyy-mm-dd');
			return false;
		}
		else
		{
			return true;
		}
	}
	
}