2014-05-25 47 views
0

我正在設計一個頁面,將有一個設施來搜索和顯示結果到一個表與分頁和用戶可以自由地選擇每頁顯示的行數。codeigniter CRUD操作與搜索和用戶定義per_page

控制器包含以下功能:

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); 
    } 

查看如下:

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'); ?> 

當我輸入行數並按GO按鈕時,它會返回所需的分頁數,但頁面鏈接數不等於所需的分頁數。例如如果搜索只返回一行,它會在沒有任何頁面鏈接的情況下顯示在表格中。但在我的情況下,它顯示單行(搜索結果)以及所有頁面鏈接(等於total_rows/per_page)。

這可能是由於total_rows被設置爲$config['total_rows'] = $this->db->get('company')->num_rows();,這實際上應該是搜索結果的編號。

但是,如果我需要添加代碼中給出的查詢參數(例如'where'子句),我該如何設置它?

回答

0

在這種情況下,您可以使用此代碼。首先添加這個庫來構建你的控制器。

看到人口統計學

類歡迎擴展是CI_Controller {

function __construct() 
{ 
    parent::__construct(); 
    $this->load->model('common_model', 'Common_model', true); 
    $this->load->library('pagination'); 
} 

那麼在您創建的任何功能,在這裏,我創建一個函數名 「bill_list」

public function bill_list() 
{ 
    /******** pagination ********/ 
     $pageNo = $this->uri->segment(2, 0); 
     $data['page_no'] = $pageNo; 
     $config['uri_segment'] = 2; 
     $config['base_url'] = BASEURL . 'bill_list'; 

     //get the total count list 
     $config['total_rows'] = $this->Common_model->count_list('lading_bill'); 
     $config['per_page'] = 20; //per page limit 
     $this->pagination->initialize($config); 
     $data['pagination'] = $this->pagination->create_links(); 
     $startLimit = ($pageNo) ? $pageNo : 0; 
     $per_page = $config['per_page']; 
    /******** pagination ********/ 

     //get the list with start limit and per page. 

     // here 'lading_bill' is table name 
    $data['bill_list'] = $this->Common_model>get_all_list('lading_bill',$startLimit, $per_page); 


    $data['mainContent'] = $this->load->view('bill_list', $data, true); 
    $this->load->view('template', $data); 
} 

然後只需添加$分頁變量給你bill_list查看頁面。

你可以複製粘貼這段代碼。並請檢查分頁庫是否可用在庫文件夾中。而已。

+0

這裏是'Common_model'? –

+0

我添加了您需要的模型功能的另一個答案。乾杯! – bchowdhury24

+0

嘿它幫了你嗎? – bchowdhury24

0

爲此,您需要在通用模型中的兩個功能 -

// return total row result. 
function count_list($table) 
{ 
    $sql = "SELECT COUNT(*) AS result_count FROM $table"; 
    $result = $this->db->query($sql)->row_array(); 
    return $result['result_count']; 
} 



//Search Total Result Using Pagination 
function get_all_list($table_name, $startLimit = NULL, $per_page = NULL) 
{ 
    if (isset($startLimit)) 
     $this->db->limit($per_page, $startLimit); 

    $result = $this->db->get($table_name)->result_array(); 
    return $result; 
} 
0

儘量給機會GroceryCRUD:http://www.grocerycrud.com/

+0

我已經嘗試過......但需要改變'format'到'打印任何record',updates'過程中添加一些圖像文件輸入字段'等。我不知道如何做到這一點的'grocerycrud'等(前/插入/更新後)離開它.. –

+0

這個庫有很多回調。通過回調和自定義操作,最後通過更改一些源代碼,我可以完成大部分請求。 – Dusan