你好,游客 登录 注册 搜索
背景:
阅读新闻

CI (CodeIgniter 2.1) 框架下Layout / 翻页等功能的实现

[日期:2012-06-06] 来源:Linux社区  作者:kunshan_shenbin [字体:大 中 小]
修改database.php文件配置数据库连接。
修改autoload.php加载需要用的到组件:
$autoload['helper'] = array('url');
$autoload['libraries'] = array('layout', 'database', 'pagination'


controllers下新建posts.php:
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  2.   
  3. class Posts extends CI_Controller {  
  4.       
  5. //     function __construct() {   
  6. //         parent::__construct();   
  7. //         $config = array('layout' => 'layout');   
  8. //         $this->load->library('Layout', $config, 'layout');   
  9. //     }   
  10.       
  11.     function index() {  
  12.   
  13. //        $this->output->enable_profiler(TRUE);   
  14.           
  15. //        $this->load->helper('url');   
  16.         $this->load->model('posts_model');  
  17.         $per_page = 3;  
  18.         $total = $this->posts_model->count_posts();  
  19.         $data['posts'] = $this->posts_model->get_posts($per_page, $this->uri->segment(3));  
  20.   
  21.         $base_url = site_url('posts/index');  
  22.         $config['base_url']    = $base_url;  
  23.         $config['total_rows']  = $total;  
  24.         $config['per_page']    = $per_page;  
  25.         $config['uri_segment'] = '3';  
  26.           
  27. //         $config['first_link']  = 'First';   
  28. //         $config['last_link']   = 'Last';   
  29. //         $config['next_link']   = '>';   
  30. //         $config['prev_link']   = '<';   
  31. //         $config['display_pages'] = FALSE;   
  32.           
  33.         $this->pagination->initialize($config);  
  34.           
  35.         $data['title_for_layout'] = 'My Posts';  
  36.         $this->layout->view('posts_view', $data);  
  37.     }  
  38. }  
models下新建posts_model.php:
  1. <?php  
  2.   
  3. class Posts_model extends CI_Model {  
  4.           
  5.     function get_posts($limit = NULL, $offset = NULL) {  
  6.         $this->db->limit ($limit, $offset);  
  7.         return $this->db->get('posts');  
  8.     }  
  9.       
  10.     function count_posts() {  
  11.         return $this->db->count_all_results('posts');  
  12.     }  
  13. }  
views下新建posts_view.php:
  1. <br>  
  2. <table width="600px" class="my_table">  
  3.     <tr>  
  4.         <td width="100px"><b>Title</b></td>  
  5.         <td><b>Content</b></td>  
  6.     </tr>  
  7.     <?php  
  8.         foreach ($posts->result() as $post):  
  9.             echo '<tr>';  
  10.             echo '<td>' . $post->title . '</td>';  
  11.             echo '<td>' . $post->content . '</td>';  
  12.             echo '</tr>';  
  13.         endforeach;  
  14.     ?>  
  15. </table>  
  16. <?php echo $this->pagination->create_links(); ?>  
  17. <br>  
  18. <br>  
linux
相关资讯       CodeIgniter 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款