歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> CI (CodeIgniter 2.1) 框架下Layout / 翻頁等功能的實現

CI (CodeIgniter 2.1) 框架下Layout / 翻頁等功能的實現

日期:2017/3/1 10:20:29   编辑:Linux編程
新建測試用數據和數據表。表結構如下:
  1. SET FOREIGN_KEY_CHECKS=0;
  2. -- ----------------------------
  3. -- Table structure for `posts`
  4. -- ----------------------------
  5. DROP TABLE IF EXISTS `posts`;
  6. CREATE TABLE `posts` (
  7. `id` int(11) NOT NULL AUTO_INCREMENT,
  8. `title` varchar(50) NOT NULL,
  9. `content` text NOT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
  12. -- ----------------------------
  13. -- Records of posts
  14. -- ----------------------------
  15. INSERT INTO `posts` VALUES ('1', 'aa', 'aaaaaaa');
  16. INSERT INTO `posts` VALUES ('2', 'bb', 'bbbbbb');
  17. INSERT INTO `posts` VALUES ('3', 'cc', 'cccccccc');
  18. INSERT INTO `posts` VALUES ('4', 'dd', 'dddddd');
application/libraries下新建Layout.php:
  1. <?php
  2. if (!defined('BASEPATH')) exit('No direct script access allowed');
  3. class Layout
  4. {
  5. var $obj;
  6. var $layout;
  7. function Layout($params = array())
  8. {
  9. $this->obj =& get_instance();
  10. if (count($params) > 0)
  11. {
  12. $this->initialize($params);
  13. }
  14. else
  15. {
  16. $this->layout = 'layout';
  17. }
  18. }
  19. function initialize($params = array())
  20. {
  21. if (count($params) > 0)
  22. {
  23. foreach ($params as $key => $val)
  24. {
  25. $this->$key = $val;
  26. }
  27. }
  28. }
  29. function view($view, $data = null, $return = false)
  30. {
  31. $data['content_for_layout'] = $this->obj->load->view($view, $data, true);
  32. if($return)
  33. {
  34. $output = $this->obj->load->view($this->layout, $data, true);
  35. return $output;
  36. }
  37. else
  38. {
  39. $this->obj->load->view($this->layout, $data, false);
  40. }
  41. }
  42. }
  43. ?>
application/views下新建layout.php:
  1. <html>
  2. <head>
  3. <title><?php echo $title_for_layout; ?></title>
  4. <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  5. <link rel="stylesheet" href="<?php echo base_url(); ?>/assets/css/style.css" type="text/css" />
  6. </head>
  7. <body>
  8. <div id="pagewidth">
  9. <div id="header"><img src="<?php echo base_url(); ?>/assets/img/header.gif" width="728" height="90"></div>
  10. <div id="wrapper" class="clearfix">
  11. <div id="twocols" class="clearfix">
  12. <?php echo $content_for_layout; ?>
  13. </div>
  14. </div>
  15. <div id="footer"><img src="<?php echo base_url(); ?>/assets/img/footer.gif" width="728" height="90"></div>
  16. </div>
  17. </body>
  18. </html>
Copyright © Linux教程網 All Rights Reserved