【问题标题】:Codeigniter 3 pagination links and single post link throw 404Codeigniter 3 分页链接和单个帖子链接抛出 404
【发布时间】:2016-07-25 22:23:40
【问题描述】:

我从这里阅读了其他关于此的文章,但无法解决此问题。我的问题是 2。

  1. 分页显示,但点击分页链接生成404。
  2. 我在 db 中有正在显示的文章列表,但是当我 单篇文章页面点击单篇文章链接,显示404。

我在 localhost wamp 上。这是我的代码。注释代码是我一直在尝试但对我不起作用的代码。

.htaccess

<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>

routes.php

$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;

$route['articles/(:any)'] = 'articles/$1';
//$route['articles'] = 'articles';

配置/baseurl

$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

控制器 - Articles.php

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'] = "&lt;&lt; First";
    $config['last_link'] = "Last &gt;&gt;";
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $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');
// }
}

模型/Articles_model

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');
// }


}

查看/page_articles

<?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>

【问题讨论】:

  • “不起作用”对任何人都不是很有帮助。您是否进行了任何故障排除?你检查过渲染的链接吗?链接是否正确构建?
  • 是的!所有这一切。这是我第一次尝试 MVC,已经 2 天了,我遇到了同样的 2 个问题。阅读教程,但无法弄清楚。注释代码是正在进行的研究/失败尝试。
  • 我什至无法理解你的所作所为。如果$start (offset) 应该代表页码,那么你的offset 不能是这个相同的数字。如果您希望每页有 5 的结果,那么第 3 页的 offset 将不是 3... 而是 10
  • 这就是问题所在。我看过视频教程、阅读文章和尝试过的东西,现在搞混了。
  • 我有 TutsPlus 的“CodeIgniter Essentials”、“Code Igniter 最佳实践”、“在 CodeIgniter 中构建 CMS”,通过观看这些内容,我能够设置页面以及系统设置和配置。我已经从 db 中检索到数据,但是分页和单条记录需要好的概念,可能需要更多的时间和实践。

标签: codeigniter pagination codeigniter-3


【解决方案1】:

一个问题...

public function index($start=0)

您似乎使用$start 作为您的页码,并再次使用您的offset 值...

$data['articles']=$this->articles_model->get_articles(5,$start);

如果$start 代表您的页码,那么您不能也将它用于您的offset 值。

需要计算您的偏移量...

$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);

例子:

每页有 5 条记录,第 4 页将从记录 #16 开始。所以 15 是正确的 offset 来获取第 4 页的记录 #16、17、18、19 和 20。


另一个潜在问题...

$config["uri_segment"] = 3;

您已将页码设置为段 3,但您的路线在段 2 处显示页码...

$route['articles/(:any)'] = 'articles/$1';

它也与此设置不匹配,这也会将您的页码放在第 2 段...

$config['base_url'] = base_url().'articles/';

基于your comment

哇。我刚才所做的...我将$config['base_url'] = base_url().'articles/'; 更改为$config['base_url'] = base_url().'articles/index/'; 并且分页链接现在显示页面但记录没有改变。 404 消失了,页面正在发生变化 - 所有页面上的记录都是前 5 个....

那么路线应该是这样的……

$route['articles/index/(:any)'] = 'articles/$1';

所以它匹配你描述的新base_url...

$config['base_url'] = base_url().'articles/index/';

【讨论】:

  • 哇。我刚才所做的……我改变了 $config['base_url'] = base_url().'articles/';到 $config['base_url'] = base_url().'articles/index/';分页链接现在显示页面,但记录没有改变。 404 消失了,页面正在改变 - 所有页面上的记录都是前 5 条......
  • @HiroshiRana,然后看看我的其余答案!如果每个页面都有相同数量的记录,则不会使用您的 offset 值。这可能是因为您的路线仍然只是从第二段路由。
  • 你的回答没有多大帮助。
  • @HiroshiRana,如果您仍然不明白我的回答如何显示您的错误,那么只需要求具体澄清。请不要苛求或抱怨。这里的人只是想提供帮助,而您应该构建一个包含有效回答所需的所有内容的问题。目前,您的 OP 缺少一些细节,如我的 last comment underneath 所述。
  • 感谢 Sparky 的帮助。我仍在尝试但无法解决这个问题。当我能够解决它时,我将发布我的代码遇到的详细问题。我想要的 URL 像 localhost/practice/project-code-igniter/articles/1 - localhost/practice/project-code-igniter/articles/2 - localhost/practice/project-code-igniter/articles/3 用于分页页面和单个帖子 URL,如 localhost/practice/project-code-igniter/articles/3/…
猜你喜欢
  • 2013-10-07
  • 1970-01-01
  • 2013-08-21
  • 2020-09-04
  • 2017-02-19
  • 1970-01-01
  • 2012-01-01
  • 2014-01-29
  • 1970-01-01
相关资源
最近更新 更多