我正在使用codeigniter 2.2.x版本。基本上我在我的数据库中有几个帖子,每页显示2个帖子。你们所有人都知道分页呈现从1到总页数,当点击时让我们说3,网址应该是博客/ pages / 3,但我的是4,4到6,5到8等等。关系就像;如果分页号是x,并且url上显示的是y,则y = 2 * x-2 ...这是我的配置设置(控制器):
function index()
{
$this->load->Model('sayfalama_model', 'Model');
$this->load->library('pagination');
$data['sayfalama_linkleri'] = $this->sayfalama_linkleri($this->Model->yazilar_adet());
$data['yazilar'] = $this->Model->yazilar($this->uri->segment(3,0),2);
$data['title'] = 'Blog Arşivi';
$this->load->view('templates/header', $data);
$this->load->view('templates/menu', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer', $data);
}
function sayfalama_linkleri($toplam)
{
$config = array(
'base_url' => site_url('blog/sayfa'),
'first_url' => site_url('blog/sayfa/1'),
'total_rows' => $toplam,
'per_page' => 2,
'page_query_string' => FALSE,
'use_page_numbers' => FALSE,
'num_links' => 2,
'uri_segment' => 3,
'full_tag_open' => '<ul class="pagination pull-right">',
'full_tag_close' => '</ul>',
'first_link' => 'İlk Sayfa',
'first_tag_open' => '<li>',
'first_tag_close' => '</li>',
'last_link' => 'Son Sayfa',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'next_link' => '»',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_link' => '«',
'prev_tag_open' => '<li>',
'prev_tag_close' => '</li>',
'cur_tag_open' => '<li><span class="current">',
'cur_tag_close' => '</span></li>',
'num_tag_open' => '<li>',
'num_tag_close' => '</li>'
);
$this->pagination->initialize($config);
return $this->pagination->create_links();
}
这是我的模特:
class Sayfalama_model extends CI_Model{
function yazilar($baslangic, $limit)
{
$sql = "SELECT * FROM news ORDER BY id ASC LIMIT $baslangic,$limit";
$query = $this->db->query($sql);
if( $query->num_rows() > 0 )
{
return $query->result_array();
}
else
{
return FALSE;
}
}
function yazilar_adet()
{
$sql = "SELECT COUNT(*) as adet FROM news";
$query = $this->db->query($sql);
return (int)$query->row()->adet;
}
}
以防万一,也会分享观点。
<?php if( $yazilar ) : ?>
<?php foreach ($yazilar as $dizi): ?>
<?php
$kisa_metin = mb_substr($dizi['text'],0,85);
echo'
<div class="col-md-8">
<div class="panel panel-info">
<div class="panel-heading">
<h3 class="panel-title">'.$dizi['title'].'</h3>
</div>
<div class="panel-body">
'.$kisa_metin.'...<br><small class="pull-right"><a href="'.base_url().'blog/'.$dizi['slug'].'"> Devamını Oku...</a></small>
</div>
</div>
</div> <br clear="both">'; ?>
<?php endforeach ?>
<div class="row">
<?php echo $sayfalama_linkleri; ?>
</div>
<?php endif; ?>
注意:当我将'use_page_numbers'更改为true时,页码是正确的,但我丢失了一些数据。有些帖子没有显示出来。 请帮助一个男人。 谢谢大家。
答案 0 :(得分:0)
我试过并找到了解决方案。事实证明这很简单。我将_use_page_numbers_更改为 true 而不是 $ this-&gt; uri-&gt; segment(3,0)我写了($ this-&gt; uri- &gt;段(3,0)* 2)-2 。它没有问题。 请注意,对于第1页和第2页,我们必须使用if else,当$ this-&gt; uri-&gt;段(3,0)小于2 $ this-&gt; uri-&gt; segment(3, 0)将保持不变。 我们将不得不根据每页的帖子来配置所有内容,如果它不是2,不像我的情况。