现在的位置: 首页 > 综合 > 正文

[PHP]使用CodeIgniter快速搭建博客框架

2013年01月17日 ⁄ 综合 ⁄ 共 2463字 ⁄ 字号 评论关闭

转载:http://blog.csdn.net/pleasecallmewhy/article/details/8575955

1.首先是控制器部分,Blog.php作为Controller即控制器:

  1. <?php header('Content-Type:text/html;charset=utf-8');  
  2. class Blog extends CI_Controller {  
  3.     function __construct()  
  4.     {  
  5.         //继承父类的构造方法,不写报错  
  6.         parent::__construct();  
  7.       
  8.         //加载框架中的相关helper  
  9.         $this->load->helper('url');  
  10.         $this->load->helper('form');  
  11.     }  
  12.   
  13.     function index(){  
  14.         //为即将跳转的页面设置相关数据  
  15.         $data['title']="My Blog Title";  
  16.         $data['heading']="My Blog Heading";  
  17.         $data['todo']=array('eat','sleep','call');  
  18.   
  19.         //连接数据库并返回查询结果  
  20.         $sql = "SELECT * FROM `Entries` LIMIT 0, 30 ";  
  21.         //初始化MySQL数据库  
  22.         $mysqlnew SaeMysql();  
  23.         $sqlData = $mysql->getData($sql);  
  24.   
  25.         //将数据库的结果传入data中  
  26.         $data['query']=$sqlData;  
  27.   
  28.         //使用变量$data向目标网页传入数据  
  29.         $this->load->view('blog_view',$data);  
  30.     }  
  31.   
  32.     function comments(){  
  33.         //为即将跳转的页面设置相关数据  
  34.         $data['title']="My Comment Title";  
  35.         $data['heading']="My Comment Heading";  
  36.   
  37.         //连接数据库并返回查询结果  
  38.         $sql = "SELECT * FROM `Comments` where `entry_id`=".$this->uri->segment(3);  
  39.   
  40.         //初始化MySQL数据库  
  41.         $mysqlnew SaeMysql();  
  42.         $sqlData = $mysql->getData($sql);  
  43.   
  44.         //将数据库的结果传入data中  
  45.         $data['query']=$sqlData;  
  46.   
  47.         //使用变量$data向目标网页传入数据  
  48.         $this->load->view('comment_view',$data);  
  49.     }  
  50.   
  51.     function comment_insert(){  
  52.         //插入POST提交的评论数据到MySQL中  
  53.         $sql = "INSERT INTO `Comments` (`entry_id`, `body`, `author`)   
  54.                         VALUES ('".$_POST['entry_id']."''".$_POST['body']."''".$_POST['author']."');";     
  55.         //初始化MySQL数据库  
  56.         $mysqlnew SaeMysql();  
  57.         $mysql->runSql($sql);  
  58.         redirect('blog/comments/'.$_POST['entry_id']);  
  59.   
  60.     }  
  61. }  
  62.   
  63. ?>  


2.接下来是View即视图部分,blog_view是博客列表的视图:

  1. <html>  
  2. <head>  
  3. <title><?php echo $title?></title>  
  4. </head>  
  5. <body>  
  6. <h1><?php echo $heading?></h1>  
  7.   
  8. <?php   
  9. //输出从数据库中读取到的文章列表  
  10. foreach($query as $key=>$value): ?>  
  11. <h3><?=$value['title']?></h3></br>  
  12. <p><?=$value['body']?></p></br>  
  13. <p><?=anchor('blog/comments/'.$value['id'],'Comments')/*插入评论的超链接*/?></p>  
  14. <hr>  
  15. <?php endforeach; ?>  
  16.   
  17. </body>  
  18. </html>  


comment_view是评论列表的内容:

  1. <html>  
  2. <head>  
  3. <title><?php echo $title?></title>  
  4. </head>  
  5. <body>  
  6. <h1><?php echo $heading?></h1>  
  7.   
  8.   
  9. <?php if(count($query)>0): /*确保有数据返回*/?>  
  10.     <?php   

抱歉!评论已关闭.