2013-04-16 34 views
0

我一直在研究codeigniter中的一個應用程序,它需要一系列可選字段並對數據庫執行查詢以根據所選字段返回結果。這些結果然後使用codeigniter分頁庫進行分頁。CodeIgniter分頁create_links()|沒有考慮'where'子句的結果

分頁工作正常,結果分頁,問題是,當我例如獲得性別爲'男性',以便只有男性結果返回,即使第一頁完美工作,create_links()函數呈現頁面對於表中的每個結果,當我更改頁面時,將忽略db-> get()函數的所有'where'參數。任何意見與此將不勝感激。

代碼:

控制器:

public function searchcharacter() { 

     $data = $this->input->post(); 

     $gender = $data['gender']; 
     $age = $data['approx_age']; 
     $hairColour = $data['hair_colour']; 
     $hairLength = $data['hair_length']; 
     $eyeColour = $data['eye_colour']; 
     $earType = $data['ear_type']; 
     $weapons = $data['weapons']; 

     $this->load->library('pagination'); 

     $config['base_url'] = 'http://localhost/ci-animedb/site/searchcharacter'; 
     $config['total_rows'] = $this->db->get('characters')->num_rows(); 
     $config['per_page'] = 10; 
     $config['uri_segment'] = 3; 
     $config['num_links'] = 20; 

     $this->pagination->initialize($config); 

     $this->load->model('get_db'); 
     $results['characters'] = $this->get_db->getCharacterList($gender, $age, $hairColour, $hairLength, $eyeColour, $earType, $weapons, $config['per_page']); 

     $this->load->view('header'); 
     $this->load->view('characterlist', $results, FALSE); 
     $this->load->view('footer'); 

} 

型號:

function getCharacterList($gender, $age, $hairColour, $hairLength, $eyeColour, $earType, $weapons, $limit) { 

    if ($gender != "None" && !empty($gender)) 
    { 
     $this->db->where('gender', $gender); 
    } 

    if ($age != "None" && !empty($age)) 
    { 
     $this->db->where('approx_age', $age); 
    } 

    if ($hairColour != "None" && !empty($hairColour)) 
    { 
     $this->db->where('hair_colour', $hairColour); 
    } 

    if ($hairLength != "None" && !empty($hairLength)) 
    { 
     $this->db->where('hair_length', $hairLength); 
    } 

    if ($eyeColour != "None" && !empty($eyeColour)) 
    { 
     $this->db->where('eye_colour', $eyeColour); 
    } 

    if ($earType != "None" && !empty($earType)) 
    { 
     $this->db->where('ear_type', $earType); 
    } 

    if ($weapons != "None" && !empty($weapons)) 
    { 
     $this->db->where('weapons', $weapons); 
    } 

    $query = $this->db->get('characters', $limit, $this->uri->segment(3)); 
    return $query->result(); 
} 

查看:

<div class="container"> 

    <div class="row"> 
     <div class="span12"> 
      <img src="<?php echo base_url(); ?>img/banner1.png" alt="AnimeDB.me" /> 
     </div> 
    </div> 

    <div class="row"> 
     <div class="span12" style="height:1px; background-color: #cccccc; margin-top: 20px; margin-bottom: 15px;"></div> 
    </div> 

    <div class="row"> 
    <div class="span12" id="ajaxcontainer"> 

     <table class='table table-striped' id='resulttable' > 
     <thead> 
      <th>Image</th> 
      <th>Name</th> 
      <th>Anime</th> 
     </thead> 
     <tbody> 

      <?php 

      foreach ($characters as $row) { 
       echo "<tr class='resulttr'><td><a href='" . base_url('site/character'). '/' . $row->character_id . "' ><image height=140 width=140 src='" . base_url('img/characterimage') . "/" . $row->file_path . "' /></a></td>"; 
       echo "<td>" . $row->character_name . "</td>"; 
       echo "<td>" . $row->anime . "</td>"; 
       echo "<td class='rowid' style='display:none;'>" . $row->character_id . "</td></tr>"; 
      } 

      ?> 

     </tbody> 
     </table> 

    <div class="form-actions"> 
     <?php echo $this->pagination->create_links(); ?> 
    </div> 

    </div> 
    </div> 

</div> <!-- /container --> 

回答

3
$config['total_rows'] = $this->db->get('characters')->num_rows(); 

這是分頁類用來確定要創建的鏈接的數量。你要告訴它使用characters表中的每一行。

您需要將您的WHERE約束條件添加到此查詢中,以獲取正確的總記錄數。

+0

乾杯的人,我明白了。得到它的工作。 :) –