我一直在使用 codeigniter 中的一个应用程序,它采用一系列可选字段并对数据库进行查询以根据所选字段返回结果。然后使用 codeigniter 分页库对这些结果进行分页。
分页工作正常并且结果被分页,问题是当我例如将性别设置为“男性”以便只返回男性结果时,即使第一页工作正常,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 -->