I am utilizing CodeIgniter's Pagination Class to implement pagination in the project.
In this project, there are 3 modules: Event,Business & Parts.
Each Module has same "view" for pagination. I have written pagination code for the event module, which has 3 types of pagination depending on the condition (received from end user).
Pagination ScreenShot: http://i.stack.imgur.com/twM3e.png
Following is the code for the event module. The controller:
class Event extends CI_Controller{
public function event_list($sort_by = 'Description', $sort_order = 'asc', $limit = 3,$offset = 0,$search = false,$category = false){
$config = array();
if($category){
$third_term = $this->uri->segment(3);
if(is_numeric($sort_by)){
if(is_numeric($third_term)){
$sort_by = 'Description';
}else{
$sort_by = 'Price';
}
}
$config['base_url'] = site_url("event/categories/$sort_by/$sort_order/$limit");
}
elseif($search || $_POST){
if($_POST){
$search_data = array(
'search_term' => '',
'search_category' => '',
);
$this->session->unset_userdata($search_data);
$search_term = $this->input->post('search',TRUE);
$search_category = $this->input->post('select-search',TRUE);
$this->session->set_flashdata('search_term',$search_term);
$this->session->set_flashdata('search_category',$this->input->post('select-search',TRUE));
$config['base_url'] = site_url("event/search_list/$sort_by/$sort_order/$limit");
}
else{
$this->session->keep_flashdata('search_term');
$this->session->keep_flashdata('search_category');
$search_term = $this->session->flashdata('search_term');
$search_category = $this->session->flashdata('search_category');
$config['base_url'] = site_url("event/search_list/$sort_by/$sort_order/$limit");
}
}else{
$config['base_url'] = site_url("event/event_list/$sort_by/$sort_order/$limit");
}
$data['fields'] = array(
'Description' => 'Description',
'Price' => 'Price'
);
$config['per_page'] = $limit;
$this->load->model('event_model');
$results = $this->event_model->search($limit, $offset, $sort_by, $sort_order,$search,$category);
$data['num_results'] = $results['num_rows'];
$data['events'] = $results['rows'];
$this->load->library('pagination');
$config['total_rows'] = $data['num_results'];
$config['uri_segment'] = 6;
$this->config->load('pagination');
//$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$data['page_no'] = ceil($this->uri->segment(6)/ $limit) + 1;
$total_pages = ceil($config['total_rows'] / $limit);
if($total_pages > 1){
$data['total_pages'] = $total_pages;
}
$config['use_page_numbers'] = TRUE;
$data['limit'] = $limit;//echo '<pre/>'.print_r($config);exit();
$this->load->view('templates/event/event-list',$data);
}
public function search_list($sort_by = 'Description', $sort_order = 'asc', $limit = 3,$offset = 0,$search = true,$category = false){
$this->event_list($sort_by,$sort_order,$limit,$offset,$search);
}
public function categories($sort_by = 'Description', $sort_order = 'asc', $limit = 3,$offset = 0,$search = false,$category = true){
$search_category = $this->uri->segment(3);
$in_flash = $this->session->flashdata('search_category');
if(($in_flash == $search_category) && (!is_numeric($search_category))){
$this->session->keep_flashdata('search_category');
}else{
$search_data = array(
'search_term' => '',
'search_category' => '',
);
$this->session->unset_userdata($search_data);
$this->session->set_flashdata('search_category',$search_category);
}
$this->event_list($sort_by,$sort_order,$limit,$offset,$search=false,$category = true);
}
}
The Model:
class event_model extends CI_Model{
function search($limit, $offset, $sort_by, $sort_order,$search,$category) {
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('Description', 'Price');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'Description';
if($search){
$search_term = "'%".$this->input->post('search',TRUE)."%'";
$search_category = $this->input->post('select-search',TRUE);
$query = sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = "event" and type= "thumb.normal" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE b.id = %s AND a.Event_Type = b.id AND c.Object_type = "event" AND c.Object_Id = a.id AND a.Description like "%s" ORDER BY a.%s %s LIMIT %s ,%s',$search_category,$search_term,$sort_by, $sort_order,$offset,$limit);
$count = "Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE a.Event_Type = b.id AND c.Object_type = 'event' AND c.Object_Id = a.id AND a.Description like $search_term";
}
if($category){
$search_category = $this->session->flashdata('search_category');
$query = sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = "event" and type= "thumb.normal" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE b.id = %s AND c.Object_type = "event" AND c.Object_Id = a.id ORDER BY a.%s %s LIMIT %s ,%s',$search_category,$sort_by, $sort_order,$offset,$limit);
$count = "Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE a.Event_Type = $search_category AND c.Object_type = 'event' AND c.Object_Id = a.id";
}
else{
$query = sprintf('SELECT *,a.id AS eid,(SELECT Url from oc_storage where Object_Id = a.id and Object = "event" and type= "thumb.normal" limit 0,1 ) as thumb FROM oc_event a,oc_event_type b,oc_address c WHERE a.Event_Type = b.id AND c.Object_type = "event" AND c.Object_Id = a.id ORDER BY a.%s %s LIMIT %s ,%s',$sort_by, $sort_order,$offset,$limit);
$count = "Select Count(*)FROM oc_event a,oc_event_type b,oc_address c WHERE a.Event_Type = b.id AND c.Object_type = 'event' AND c.Object_Id = a.id ";
}
$stmt = $this->db->conn_id->prepare($query);
$stmt->execute();
$ret['rows'] = $stmt->fetchAll( PDO::FETCH_ASSOC ) ;
$ret['num_rows'] = $this->db->conn_id->query($count)->fetchColumn();
return $ret;
}
The view : "Including only whats required";
<div class="sorting">
<?php
foreach($fields as $field_name => $field_display): ?>
<?php if (($sort_by == $field_name)){
(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc')
?>
<a href="<?php
$call_by = $this->uri->segment(2);
echo site_url("event/$$call_by/Description/".(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc')."/$limit");
?>">Desc <?php
echo ($sort_order == 'asc' && $sort_by == "Description") ? '▲' : '▼'
?></a><a href="<?php
echo site_url("event/$$call_by/Price/".(($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc')."/$limit");
?>">Price <?php
echo ($sort_order == 'asc' && $sort_by == "Price") ? '▲' : '▼'?></a>
<?php } ?>
<?php endforeach; ?>
</div>
I am quite sure that I haven't followed all laws of programming.
What I want to know is, how can I use the above code for all 3 modules, and
where I can improve these functions, and how can I make this code more Object Oriented and Less Scripted. Any suggestions/modification please.
Thx