Paginating长篇文章

时间:2011-12-11 23:11:54

标签: php codeigniter

我试图为在codeigniter框架上构建的网站在移动设备上查看一篇长篇文章。 我已经能够确定文章的页数,但是在如何创建链接以及能够将参数传递给uri以进行分页方面我很失败。 这是我控制器的代码

function view_post($alias)
      {
           $post = $this->post_model->load_post($alias);
           $data['title'] = $post->post_title;
           $data['logged'] = $this->ion_auth->logged;
           $data['user_id'] = $this->ion_auth->user;
           $data['username'] = $this->ion_auth->username;  
           $data['comments'] = $this->comment_model->count_post_comments($post->ID);
           $link = strtolower($post->name)."/".$post->alias;
           $data['post_link'] = "www.giggsmagazine.com/".$link;
           $data['post'] = $post;

           // Article Pagination for long articles
           $posts = explode("<!-- pagebreak -->",$post->post_content);
           $page_count = count($posts) - 1;
           $data['pages'] = $page_count;
           $data['posts'] = $posts;
           if($this->isMobile())
          {
              $device = $this->agent->mobile;
              switch($device)
              {
                  case "BlackBerry":
                  $this->load->view('mobile/bb/inc/bb_header',$data);
                  $this->load->view('mobile/bb/posts/view_post');
                  $this->load->view('mobile/bb/inc/bb_footer');
                  break;
                  case "Apple Iphone" || "Apple Ipod Touch":
                  $this->load->view('mobile/iphone/inc/iphone_header',$data);
                  $this->load->view('mobile/iphone/posts/view_post');
                  $this->load->view('mobile/iphone/inc/iphone_footer');
                  break;
                  default:
                  $this->load->view('mobile/generic/inc/generic_header',$data);
                  $this->load->view('mobile/generic/view_post');
                  $this->load->view('mobile/generic/inc/generic_footer');
                  break;
              }
           }else {
           $this->load->view('templates/site_header',$data);
           $this->load->view('posts/view_post');
           $this->load->view('templates/site_footer');
           }
      }

请指出正确的方向

2 个答案:

答案 0 :(得分:0)

到目前为止你尝试了什么?你可能最好用javascript / jQuery对客户端进行分页(只需要一个简单的'read more'链接就可以了,将数据限制到某个字符限制),但是如果你想用服务器端代码使用代码你已经写好了,你可能会做这样的事情:

向view_post添加新的'page'参数:

function view_post($alias, $page = 0)

为帖子内容创建新变量

$data['post_content'] = $posts[$page];

要显示分页,请在您的视图中添加以下内容:

for ($i = 0; $i <= $page_count; $i++) {
  echo "<a href='/path/to/view_post/{$alias}/{$i}'>{$i}</a>";
}

这可以让你走上正轨。

答案 1 :(得分:0)

CodeIgniter有its own Pagination library - 为什么不使用它?