Codeigniter 3分页链接和单柱链接抛出404
我在这里读过其他关于这方面的文章,但无法解决这个问题。我的问题是2Codeigniter 3分页链接和单柱链接抛出404,codeigniter,pagination,codeigniter-3,Codeigniter,Pagination,Codeigniter 3,我在这里读过其他关于这方面的文章,但无法解决这个问题。我的问题是2 分页显示,但单击分页链接会生成404 我有数据库中正在显示的文章列表,但当我 点击单篇文章页面的单篇文章链接,显示404 我在本地主机wamp上。这是我的密码。注释的代码是我一直在尝试的,但对我来说不起作用 .htaccess <IfModule mod_rewrite.c> RewriteEngine On #RewriteCond $1 !^(index\.php|resources|robots\.txt) R
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else
{
?>
<ol>
<?php
foreach($articles as $row)
{ ?>
<li>
<h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
<?php echo $row['name']; ?>
</a></h4>
<p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
<br>
<a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">Read more</a>
</p>
<br>
</li>
<?php } ?>
</ol>
<?php
}
?>
</p>
<div>
<?php echo $pages; ?>
</div>
config/baseurl
$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
class Articles_model extends CI_Model {
// function get_articles() {
public function get_articles($limit, $start) {
// $query = $this->db->query("SELECT * FROM articles");
// $this->db->select()->from('articles');
// $this->db->select()->from('articles')->where('active',1)->order_by('date_added','desc')->limit(0,20);
$this->db->select()->from('articles')->where('status','1')->order_by('date_added','desc')->limit($limit, $start);
$query = $this->db->get();
// return object
// return $query->result();
// return array
return $query->result_array();
}
function get_articles_count(){
$this->db->select('id')->from('articles')->where('status',1);
$query = $this->db->get();
return $query->num_rows();
}
// for single article
// function get_single_article($perma){
// $this->db->select()->from('articles')->where(array('status'=>1,'permalink'=>$perma));
// $query = $this->db->get();
// return $query->first_row('array');
// }
}
Controller-Articles.php
$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;
$route['articles/(:any)'] = 'articles/$1';
//$route['articles'] = 'articles';
class Articles extends CI_Controller {
public function index($start=0)
{
// $this->output->cache('86400');
$this->load->view('header');
//load model
// $this->load->model('articles_model');
// load 'get_articles' function from 'articles_model' model and store it in data
$this->load->library('pagination');
$data['articles']=$this->articles_model->get_articles(5,$start);
$config['base_url'] = base_url().'articles/';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 5;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 2; //NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = "<< First";
$config['last_link'] = "Last >>";
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//$data['pages'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
// //call the model function to get the department data
//$data['pages'] = $this->articles_model->get_articles($config["per_page"], $data['page']);
$data['pages'] = $this->pagination->create_links();
//$this->load->view('page_articles');
// load view and load model data with it
$this->load->view('page_articles',$data);
$this->load->view('footer');
}
// single article
// function post($perma){
// $this->load->view('header');
// $this->load->model('articles_model');
// $data['articles']=$this->articles_model->get_single_article($perma);
// $this->load->view('page_article',$data);
// $this->load->view('footer');
// }
}
查看/翻页文章
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else
{
?>
<ol>
<?php
foreach($articles as $row)
{ ?>
<li>
<h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
<?php echo $row['name']; ?>
</a></h4>
<p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
<br>
<a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">Read more</a>
</p>
<br>
</li>
<?php } ?>
</ol>
<?php
}
?>
</p>
<div>
<?php echo $pages; ?>
</div>
没有记录
一个问题
public function index($start=0)
您似乎对页码使用了$start
,对偏移量使用了值
$data['articles']=$this->articles_model->get_articles(5,$start);
如果$start
表示您的页码,则您也不能将其用于偏移量
值
需要计算您的偏移量
$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);
例如:
由于每页有5条记录,第4页将以记录16开头。因此15
是正确的偏移量
来获取第4页的记录#16、17、18、19和20
另一个潜在的问题
$config["uri_segment"] = 3;
您已将页码设置为第3段,但您的路线正在第2段显示页码
$route['articles/(:any)'] = 'articles/$1';
$config['base_url'] = base_url().'articles/';
它也与此设置不匹配,这也会将您的页码放在第2段
$route['articles/(:any)'] = 'articles/$1';
$config['base_url'] = base_url().'articles/';
基于
哇。我刚才所做的。。。我更改了$config['base_url']=base_url().'articles/'
至$config['base_url']=base_url().'articles/index/'
和分页链接现在显示页面,但记录不变。404消失了,页面正在更改-所有页面上的记录都是前5条
那么路线应该是这个
$route['articles/index/(:any)'] = 'articles/$1';
因此它与您描述的新的base\u url
匹配
$config['base_url'] = base_url().'articles/index/';
“不起作用”对任何人都没有多大帮助。你做过故障排除吗?你检查过渲染的链接了吗?链接构造是否正确?是的!所有这些。这是我第一次尝试MVC,已经两天了,我遇到了同样的两个问题。正在阅读教程,但还不能理解。注释的代码是正在进行的研究/失败的尝试。我甚至不能理解你所做的。如果假定$start
(偏移量
)表示页码,则您的偏移量不能是相同的数字。如果希望每页都有5
结果,则第3页的偏移量将不是3
。。。这将是10
。这就是问题所在。我看了视频教程,读了文章,尝试了一些东西,现在都混在一起了。我有TutsPlus的“CodeIgniter Essentials”,“CodeIgniter最佳实践”,“在CodeIgniter中构建CMS”,通过观看这些,我能够设置页面和系统设置和配置。我已经从数据库中检索到数据,但是分页和单个记录需要好的概念,需要更多的时间和实践。我刚才所做的。。。我更改了$config['base_url']=base_url().'articles/';到$config['base_url']=base_url().'articles/index/';分页链接现在显示页面,但记录不变。404消失了,页面正在更改-所有页面上的记录都是前5条…@HiroshiRana,然后看看我的其余答案!如果每页都有相同数量的记录,则不会使用您的偏移量值。这可能是因为你的路线仍然只是从第二段开始的路线。你的答案没有多大帮助。@HiroshiRana,如果你仍然不明白我的答案是如何显示你的错误的,那就简单地要求具体的澄清。请不要苛求或抱怨。这里的人只是想帮助你,而你需要构建一个包含所有必要内容的问题,以便有效地回答它。目前,您的OP缺少一些细节,如我的中所述。感谢Sparky的帮助。我仍在努力,但无法解决这个问题。当我能够解决它时,我将发布我的代码所遇到的详细问题。我想要的URL是-,对于分页页面和单个帖子URL