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

新建测试用数据和数据表。表结构如下:
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `posts`
-- ----------------------------
DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(50) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of posts
-- ----------------------------
INSERT INTO `posts` VALUES ('1', 'aa', 'aaaaaaa');
INSERT INTO `posts` VALUES ('2', 'bb', 'bbbbbb');
INSERT INTO `posts` VALUES ('3', 'cc', 'cccccccc');
INSERT INTO `posts` VALUES ('4', 'dd', 'dddddd');

application/libraries下新建Layout.php:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Layout
{
    var $obj;
    var $layout;

    function Layout($params = array())
    {
        $this->obj =& get_instance();
        if (count($params) > 0)
        {
            $this->initialize($params);
        }
        else
        {
            $this->layout = 'layout';
        }
    }

    function initialize($params = array())
    {
        if (count($params) > 0)
        {  
            foreach ($params as $key => $val)
            {
                $this->$key = $val;
            }        
        }
    }

    function view($view, $data = null, $return = false)
    {
        $data['content_for_layout'] = $this->obj->load->view($view, $data, true);

        if($return)
        {
            $output = $this->obj->load->view($this->layout, $data, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($this->layout, $data, false);
        }
    }
}
?>

application/views下新建layout.php:
<html>
<head>
<title><?php echo $title_for_layout; ?></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="<?php echo base_url(); ?>/assets/css/style.css" type="text/css" />
</head>
<body>
    <div id="pagewidth">
        <div id="header"><img src="<?php echo base_url(); ?>/assets/img/header.gif" width="728" height="90"></div>
        <div id="wrapper" class="clearfix">
            <div id="twocols" class="clearfix">
                  <?php echo $content_for_layout; ?>
            </div>
        </div>
        <div id="footer"><img src="<?php echo base_url(); ?>/assets/img/footer.gif" width="728" height="90"></div>
    </div>
</body>
</html>

修改database.php文件配置数据库连接。
修改autoload.php加载需要用的到组件:
$autoload['helper'] = array('url');
$autoload['libraries'] = array('layout', 'database', 'pagination'


controllers下新建posts.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Posts extends CI_Controller {
    
//     function __construct() {
//         parent::__construct();
//         $config = array('layout' => 'layout');
//         $this->load->library('Layout', $config, 'layout');
//     }
    
    function index() {

//        $this->output->enable_profiler(TRUE);
        
//        $this->load->helper('url');
        $this->load->model('posts_model');
        $per_page = 3;
        $total = $this->posts_model->count_posts();
        $data['posts'] = $this->posts_model->get_posts($per_page, $this->uri->segment(3));

        $base_url = site_url('posts/index');
        $config['base_url']    = $base_url;
        $config['total_rows']  = $total;
        $config['per_page']    = $per_page;
        $config['uri_segment'] = '3';
        
//         $config['first_link']  = 'First';
//         $config['last_link']   = 'Last';
//         $config['next_link']   = '>';
//         $config['prev_link']   = '<';
//         $config['display_pages'] = FALSE;
        
        $this->pagination->initialize($config);
        
        $data['title_for_layout'] = 'My Posts';
        $this->layout->view('posts_view', $data);
    }
}

models下新建posts_model.php:

<?php

class Posts_model extends CI_Model {
        
    function get_posts($limit = NULL, $offset = NULL) {
        $this->db->limit ($limit, $offset);
        return $this->db->get('posts');
    }
    
    function count_posts() {
        return $this->db->count_all_results('posts');
    }
}

views下新建posts_view.php:

<br>
<table width="600px" class="my_table">
    <tr>
        <td width="100px"><b>Title</b></td>
        <td><b>Content</b></td>
    </tr>
    <?php
        foreach ($posts->result() as $post):
            echo '<tr>';
            echo '<td>' . $post->title . '</td>';
            echo '<td>' . $post->content . '</td>';
            echo '</tr>';
        endforeach;
    ?>
</table>
<?php echo $this->pagination->create_links(); ?>
<br>
<br>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值