在codeigniter中,我使用sqlsrv和分页库来显示数据库中的记录, 但是我点击分页链接上的第2页后,记录将显示在当前页面的末尾,而不是显示在新页面中,任何帮助将不胜感激, 这是控制器:
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/posts/example1";
$config["total_rows"] = $this->PostModels->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config["num_links"] = 5;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->PostModels->
fetchdata($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
以下是模型:
public function record_count() {
return $this->db->count_all("BlawBlaw");
}
public function fetchdata($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("BlawBlaw");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
&安培;这是视图:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php echo $links; ?></p>
</table>
答案 0 :(得分:1)
您可以使用此模型并查看代码中的更改
以下是模型:
function fetch_data($limit, $id)
{
$this->db->select('*')->from('category ')->limit($limit, $id);
//$this->db->limit($limit);
//$this->db->where('Cid', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
& here is the View:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php foreach ($links as $link) {
echo $link; ?></p>
</table>