[eluser]ede196620[/eluser]
i am stilll working on it but here it is
Code: class Display 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->model('display_model','',TRUE);
}
function index($offset = 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 for valid column
// load data
$querys = $this->display_model->get_paged_list($this->limit, $offset, $order_column, $order_type)->result();
// generate pagination
$this->load->library('pagination');
$config['base_url'] = site_url('display/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(" ");
$new_order = ($order_type == 'asc' ? 'desc' : 'asc');
$this->table->set_heading(
'No',
anchor('display/index/'.$offset.'/Question/'.$new_order, 'Question'),
anchor('display/index/'.$offset.'/qA/'.$new_order, 'qA'),
anchor('display/index/'.$offset.'/qB/'.$new_order, 'qB'),
anchor('display/index/'.$offset.'/qC/'.$new_order, 'qC'),
'Actions'
);
$i = 0 + $offset;
foreach ($querys as $query){
$this->table->add_row(++$i,
$query->Question,
$query->qA,
$query->qB,
$query->qC,
anchor('display/view/'.$query->id,'view',array('class'=>'view')).' '.
anchor('display/update/'.$query->id,'update',array('class'=>'update')).' '.
anchor('display/delete/'.$query->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure you want to remove this Question?')"))
);
}
$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('display_view', $data);
}
function add(){
// set common properties
$data['title'] = 'Add New Question ';
$data['action'] = site_url('display/add');
$data['link_back'] = anchor('display/index/','Back to list of Questions',array('class'=>'back'));
$this->_set_rules();
// run validation
if ($this->form_validation->run() === FALSE){
$data['message'] = '';
// set common properties
$data['title'] = 'Add new Question';
$data['message'] = '';
$data['query']['id']='';
$data['query']['Question']='';
$data['query']['qA']='';
$data['query']['qB']='';
$data['query']['qC']='';
$data['link_back'] = anchor('display/index/','See List Of Student',array('class'=>'back'));
$this->load->view('displayEdit', $data);
}else{
// save data
$query = array('Question' => $this->input->post('Question'),
'qA' => $this->input->post('qA'),
'qB' => $this->input->post('qB'),
'qC' => $this->input->post('qC'));
$id = $this->display_model->save($query);
// set form input name="id"
$this->validation->id = $id;
redirect('display/index/add_success');
}
}
function view($id){
// set common properties
$data['title'] = 'Question Details';
$data['link_back'] = anchor('display/index/','List Of Questions',array('class'=>'back'));
// get Student details
$data['query'] = $this->display_model->get_by_id($id)->row();
// load view
$this->load->view('display_view', $data);
}
function update($id){
// set common properties
$data['title'] = 'Update Question';
$this->load->library('form_validation');
// set validation properties
$this->_set_rules();
$data['action'] = ('display/update/'.$id);
// run validation
if ($this->form_validation->run() === FALSE){
$data['message'] = '';
$data['query'] = (array)$this->display_model->get_by_id($id)->row();
$_POST['gender'] = strtoupper($data['Student']['gender']);
$data['Student']['date_of_birth'] = date('d-m-Y',strtotime($data['Student']['date_of_birth']));
// set common properties
$data['title'] = 'Update Question';
$data['message'] = '';
}else{
// save data
$id = $this->input->post('id');
$query = array('Question' => $this->input->post('Question'),
'qA' => $this->input->post('qA'),
'qB' => $this->input->post('qB'),
'qC' => $this->input->post('qC'),
);
var_dump($query);
$this->display_model->update($id,$query);
$data['query'] = (array)$this->display_model->get_by_id($id)->row();
// set user message
$data['message'] = 'update Question success';
}
$data['link_back'] = anchor('display/index/','List Of Question',array('class'=>'back'));
// load view
$this->load->view('displayEdit', $data);
}
function delete($id){
// delete Question
$this->display_model->delete($id);
// redirect to Question list page
redirect('display/index/delete_success','refresh');
}
// validation rules
function _set_rules()
{
$this->form_validation->set_rules('Question', 'Question', 'required|trim');
$this->form_validation->set_rules('qA', 'qA', 'required');
$this->form_validation->set_rules('qB', 'qB', 'required');
$this->form_validation->set_rules('qC', 'qC', 'required');
}
}
?>
[eluser]TheFuzzy0ne[/eluser]
That's where you're passing 'id' as the sort column.
Code: function index($offset = 0, $order_column = 'QID', $order_type = 'asc')
{
...
You should consider validating the $order_column and $order_type variable, otherwise you'll end up with a lot of database errors if people manipulate the URL.
Code: function get_paged_list($limit=10, $offset=0, $order_column='', $order_type='asc')
{
// If we don't have a valid sort column, sort by primary key.
if ( ! in_array($order_column, array('QID', 'another_column', 'some_other_column')))
{
$order_column = $this->primary_key;
}
// If we don't have a valid order type, sort by 'asc'.
if ( ! in_array($order_type, array('asc', 'desc')))
{
$order_type = 'asc';
}
$this->db->order_by($order_column, $order_type);
return $this->db->get($this->table_name, $limit, $offset);
}
[eluser]ede196620[/eluser]
tnx for the replay and the help once again u saved me a lot of time  greatly appreciated i will consider this
|