【PHP CMS系统】 新闻管理系统 2

小项目代码:

manager.html

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset="utf-8" />
</head>
<h1>Manager</h1>
<hr/>
<a href="addNews.html">Add news</a>
<a href="newList.php">Update</a>
<a href="">Show all news</a>
</html>

 

addNews.html

<head>
<title>新闻标题</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<!--我们在添加新闻时,就同时生成一个对应的新闻页面(比如你设计好的一个新闻内容显示模板)-->

<form action="newsController.php" method="post"> 
<table>
<tr><td>新闻标题</td><td><input type="text" name="title"/></td></tr>
<tr><td>新闻内容</td><td><textarea cols="50" rows="10" name="content"></textarea></td></tr>
<tr><td><input type="submit" value="添加"/></td><td><input type="reset" value="重新填写"/></td></tr>
<!--隐藏区-->
<input type='hidden' name='oper' value='add'/>
</table>
</form>
</html>

 

 

newsController.php

<?php

    //处理用户的dml请求
    //先获取oper值
    $oper = $_POST['oper'];
    
    if($oper === "add"){
    
        $title = $_POST['title'];
        $content = $_POST['content'];
        
        $con = mysql_connect("localhost","root","root");
        //添加新闻到数据库
        if(!$con){
            die('Could not connect'. mysql_error());
        }
        
        mysql_select_db("news",$con);
        
        $sql = "insert into news values(null, '$title', '$content', '')";
        
        if(mysql_query($sql,$con)){
        
            //creat static page
            $id = mysql_insert_id();
            $html_filename = 'new-id'.$id.'.html';
            
            //取出当前的年月日创建一个文件夹,把这个静态页面放入这个文件夹中
            //safe_mode ,如果打开这个模式,那么创建文件夹的功能就失效了
            
            $html_fp = fopen('../'.$html_filename, 'w');
            //把模板文件读取进来
            $fp = fopen('news.tpl', 'r');
            //循环读取
            while(!feof($fp)){
                $row = fgets($fp);
                //把占位符替换掉-->myReplace
                
                $row = str_replace('%title%',$title,$row); //在row中找有没有%title%的字符串,找到后用$title替换
                $row = str_replace('%content%',$content,$row); //在row中找有没有%title%的字符串,找到后用$title替换
            
                fwrite($html_fp, $row);
            }
            //close file
            fclose($fp);
            fclose($html_fp);
            
            echo "恭喜你,添加成功<a href='manager.html'>管理新闻</a>";
            //如果希望添加完新闻就立即更新
            include 'newList.php';
        }else{
        
            die("Insert faild");
        }
        
    }else if($oper === "update"){
    
    
    }else if($oper === "delete"){
    
    }
?>

 

 

newList.php

<?php
    $con = mysql_connect("localhost","root","root");
    
    if(!$con){
        die('Could not connect'. mysql_error());
    }
    
    mysql_select_db("news",$con);
    
    $sql = "select * from news";
    
    $res = mysql_query($sql);
    ob_start();
    echo "<head><meta http-equiv='content-type' content='text/html;charset=utf-8' /></head>";
    echo "<h1>新闻列表</h1>";
    echo "<table>";
    echo "<tr><td>id</td><td>标题</td><td>查看详情</td><td>修改新闻</td></tr>";
    while($row=mysql_fetch_assoc($res)){
        echo '<tr><td>'.$row['id'].'</td><td>'.$row['title'].'</td><td><a href="new-id'.$row['id'].'.html">See More</a></td></tr>';
    }
    echo "</table>";
    
    //这里我们可以把ob内容取出,并生成一个静态页面
    $ob_str = ob_get_contents();
    file_put_contents('../index.html',$ob_str);
    ob_clean();
    echo "Success, <a href='../index.html'>Click to check</a>";

    
    mysql_free_result($res);
?>

 

new.tpl

<html>
<head>
<meta http-equiv='content-type' content="text/html; charset=utf-8" />
<title>%title%</title>
</head>
<body>
<h1>%title%</h1>
<hr/>
<pre>%content%</pre>
</body>
</html>

 

http://localhost/bigwebsite/staticPage/newPage/manager/manager.html

posted @ 2013-06-14 21:15  Zhentiw  阅读(345)  评论(0编辑  收藏  举报