I'm designing a page which will have a facility to search and display result into a table with pagination and user has given a freedom to chose number of rows to be displayed per page.
Controller contain following function:
settings_admin.php
function view_company(){
$this->check_logged_in();
$this->load->library('table');
$this->load->library('pagination');
$fields = $this->db->list_fields('company');
$this->table->set_heading($fields);
$field_to_search = 'all';
if(isset($_POST['company_cols'])){
$field_to_search = $_POST['company_cols'];
}
if(isset($_POST['search_text'])){
$first=true;
$search_text = $_POST['search_text'];
if($field_to_search == 'all' && $search_text!=''){
foreach ($fields as $field){
if($first){
$this->db->like($field,$search_text);
$first=false;
}else{
$this->db->or_like($field,$search_text);
}
}
}else if($search_text!=''){
$this->db->like($field_to_search,$search_text);
}
}
if(isset($_POST['num_of_rows'])){
$config['per_page'] = $_POST['num_of_rows'];
$this->session->set_userdata('rows_per_page', $_POST['num_of_rows']);
}else if($this->session->userdata('rows_per_page')!=null){
$config['per_page'] = $this->session->userdata('rows_per_page');
}else{
$config['per_page'] = 10;
}
$result = $this->db->get('company',$config['per_page'], $this->uri->segment(3));
$config['total_rows'] = $this->db->get('company')->num_rows();
//$config['total_rows'] = $result->num_rows();
$config['base_url'] = site_url('/settings_admin/company');
$config['num_links'] = 5;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$config['records'] = $result;
$this->pagination->initialize($config);
//var_dump($config);
//var_dump($_POST);
$config['num_of_rows'] = $config['per_page'];
$this->load->view('admin/view/company',$config);
}
View is as follows:
company.php
<?php
$data = array(
'title' => 'Company',
'form_action' => 'settings_admin/company'
);
?>
<?php $this->load->view('includes/header',$data); ?>
<?php $this->load->view('sidebar_admin_controls'); ?>
<div class="content">
<h3>Companies</h3>
<div align='right'>
Search:
<input style="width:15em;" type="text" name="search_text"
value="<?php echo set_value('search_text'); ?>" placeholder="search company details"/>
in
<select name="company_cols">
<option value='all'>all</option>
<?php
$fields = $this->db->list_fields('company');
foreach ($fields as $field){
echo "<option value='$field'>$field</option>";
}
?>
</select>
<input type="submit" name="search" value="Search"/>
</div>
<div align='left'>
<?php
if(!isset($num_of_rows)){
$num_of_rows = 10;
}
?>
Rows per page: <input style="width:4em;" type="text" name="num_of_rows" value=<?php echo $num_of_rows;?> placeholder="rows"/>
<input type="submit" name="search" value="Go"/>
</div>
<div class="view_table" id="view_table">
<?php
echo $this->table->generate($records);
?>
</div>
<?php
echo "<br/>";
echo $this->pagination->create_links(); // can run and show me the ok..
// when i press on page link, it goes back to default #of rows per page (10)
?>
<br/>
<div align="center" id="option_buttons">
<input type="submit" name="add" value="Add New"/>
<input type="submit" name="update" value="Update"/>
<input type="submit" name="delete" value="Delete"/>
<input type="submit" name="print" value="Print"/>
</div>
</div><!-- end .content -->
<?php $this->load->view('includes/footer'); ?>
when I enter number of rows and press GO
button, it return with desired pagination but number of page links is not equal to desired ones. e.g. if search returns only one row, it shouls be displayed in table without any page link.But in my case it's displaying single row (search result) along with all page links (equal to total_rows/per_page).
This may be due to the fact that total_rows is been set as $config['total_rows'] = $this->db->get('company')->num_rows();
which should actually be number of search result.
But how can I set it if I need to add query parameters as given in code (e.g. 'where' clause)?