首页 >后端开发 >php教程 >微型新闻系统的开发

微型新闻系统的开发

PHPz
PHPz原创
2017-04-04 14:53:152363浏览

一、系统说明

“经历过方知深浅”,开发该系统目的主要是为了学习,理顺系统开发的流程,将理论与实践应用进一步的结合,这也是php学习入门进阶的一个重要里程碑。

开发环境:Apache2.0+php5.4+mysql5.5
开发工具:文本编辑器(dreamweaver/editplus)

二、系统开发要点

1.smarty的应用
2.后台会员、权限验证
3.对象、类的封装

三、系统模块分类

1.MySQL数据库
主要有5个数据表:全站配置表p_config、后台会员表p_admin、新闻分类表p_<a href="http://www.php.cn/wiki/165.html" target="_blank">new</a>s<a href="http://www.php.cn/wiki/164.html" target="_blank">class</a>、新闻标题表p_newsbase、新闻内容表p_newscontent,如下图所示:

微型新闻系统的开发

数据表

2.程序文件
网站目录结构一览表,如图:

微型新闻系统的开发

网站目录结构

四、开发详解

下面将按照一定流程分项讲解细节(css等文件不详细列出):
1.mysql数据库的创建
下面依次贴上各个表的配置图:

  • p_config表

微型新闻系统的开发

p_config表

  • p_admin表

微型新闻系统的开发

p_admin表

  • p_newsclass表

微型新闻系统的开发

p_newsclass表

  • p_newsbase表

微型新闻系统的开发

p_newsbase表

  • p_newscontent表

微型新闻系统的开发

p_newscontent表

详解:

  • p_newsclass,p_newsbase,p_newscontent这三个表存在关联关系:
    p_newsbase|cid --> p_newsclass|id
    p_newscontent|nid --> p_newsbase|id

  • p_newsclassf_id=0时为顶级栏目,f_id=1表示该分类属于id为1的分类的子分类。

2.smarty配置
将smarty文件放置根目录common文件夹下,下一步安装配置:
(1).设置config.php配置文件,源码如下:

<?php>

//数据库常用变量配置
$myhost       ="localhost";   //主机名
$mydbuser     ="root";        //数据库用户名
$mydbpw       ="password";    //数据库密码
$mydbname     ="news_system"; //数据库名称
$mydbcharset  ="GBK";         //数据库编码

//smarty常用变量配置
$smarty_template_dir   ='./templates/';     //模板路径变量
$smarty_compile_dir    ='./templates_c/';   //编译目录模板变量
$smarty_config_dir     ='./configs/';       //配置目录变量
$smarty_cache_dir      ='./cache/';         //缓存目录变量
$smarty_caching        ='false';            //缓存开关变量         
$smarty_delimiter      =explode("|","{|}"); //定界符变量,返回数组array([0]=>'{',[1]=>'}')

?>

(2).设置global全局调用文件,源码如下:

<?php
include_once(&#39;./configs/config.php&#39;);              //引入config.php
include_once(&#39;./common/smarty/Smarty.class.php&#39;);  //引入smarty.class.php
include_once(&#39;./common/mysql.class.php&#39;);          //引入mysql语句类文件
include_once(&#39;./common/action.class.php&#39;);         //引入动作执行类文件
include_once(&#39;./common/page.class.php&#39;);           //引入分页类文件

//实例化类,这里action已事先在继承了Mysql类
$db=new action($myhost,$mydbuser,$mydbpw,$mydbname,ALL_PS,$mydbcharset);

//配置smarty
$smarty = new smarty();
$smarty->template_dir    = $smarty_template_dir;   //模板目录
$smarty->compile_dir     = $smarty_compile_dir;    //编译目录
$smarty->config_dir      = $smarty_config_dir;     //配置目录
$smarty->cache_dir       = $smarty_cache_dir;      //缓存目录
$smarty->caching         = $smarty_caching;        //缓存开关
$smarty->left_delimiter  = $smarty_delimiter[0];   //左定界符
$smarty->right_delimiter = $smarty_delimiter[1];   //右定界符
$smarty->assign("t_dir",$smarty_template_dir);     //模板路径变量映射

?>

到现在,smarty已配置好。

3.mysql语句类
这里详细说明系统常用到的mysql自定义语句类:mysql.class.php,源码如下:

<?php
class mysql{
  private $db_host;
  private $db_user;
  private $db_pwd;
  private $db_database;
  private $coding;
  private $conn;
  private $sql;
  private $row;
  private $result;
  private $bulletin=true;    //是否开启错误记录
  private $show_error=true;  //测试阶段,显示所有错误,具有安全隐患,默认关闭

  //构造函数,初始化赋值,实例化后可直接传参
  public function construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){
    $this->db_host=$db_host;
    $this->db_user=$db_user;
    $this->db_pwd=$db_pwd;
    $this->db_database=$db_database;
    $this->conn=$conn;
    $this->coding=$coding;
    $this->connect();
  }

  //连接数据库
  public function connect(){
    if ($this->conn == "pconn") {
      //永久链接
      $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
    } else {
      //即时链接
      $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
    }

    if (!mysql_select_db($this->db_database, $this->conn)) {
      if ($this->show_error) {
        $this->show_error("数据库不可用:", $this->db_database);
      }
    }
    mysql_query("set names $this->coding");
  }

  //数据库执行
  public function query($sql){
    if($sql=""){
      $this->show_error("SQL语句错误:","SQL查询语句为空");
    }
    $this->sql=$sql;
    $result=mysql_query($this->sql,$this->conn);

    if (!$result) {
      //调试中使用,sql语句出错时会自动打印出来
      if ($this->show_error) {
        $this->show_error("错误SQL语句:", $this->sql);
      }
    } else {
      $this->result = $result;
    }
  }

  //取得记录值,获取数组-索引和关联
  public function fetch_array(){
    return mysql_fetch_array($this->result);
  }

  //简化查询
  public function select($table,$columnName="*",$condition='',$debug=''){
    $condition=$condition ? "where" . $condition : NULL;
    if($debug){
      echo "select $columnName from $table $condition";
    }else{
      $this->query("select $columnName from $table $condition");
    }
  }

  //简化查询select
  public function findall($table){
    $this->query("select * from $table");
  }

  //取得上一步 INSERT 操作产生的 
  public function insert_id() {
    return mysql_insert_id();
  }

  //错误记录
  public function show_error($message="",$sql=""){
    if(!$sql){
      echo "<font color=&#39;red&#39;>" . $message . "</font>";
      echo "<br />";
    }else{
      echo "<fieldset>";
      echo "<legend>错误信息提示:</legend><br />";
      echo "<p style=&#39;font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;&#39;>";
      echo "<p style=&#39;height:20px; background:#000000; border:1px #000000 solid&#39;>";
      echo "<font color=&#39;white&#39;>错误号:12142</font>";
      echo "</p><br />";
      echo "错误原因:" . mysql_error() . "<br /><br />";
      echo "<p style=&#39;height:20px; background:#FF0000; border:1px #FF0000 solid&#39;>";
      echo "<font color=&#39;white&#39;>" . $message . "</font>";
      echo "</p>";
      echo "<font color=&#39;red&#39;><pre class="brush:php;toolbar:false">" . $sql . "
";           $ip=$this->getip();           if ($this->bulletin) {         $time = date("Y-m-d H:i:s");         $message = $message . "\r\n$this->sql" . "\r\n客户IP:$ip" . "\r\n时间 :$time" . "\r\n\r\n";         $server_date = date("Y-m-d");         $filename = $server_date . ".txt";         $file_path = "error/" . $filename;         $error_content = $message;         //$error_content="错误的数据库,不可以链接";         $file = "error"; //设置文件保存目录         //建立文件夹         if (!file_exists($file)) {           if (!mkdir($file, 0777)) {             //默认的 mode 是 0777,意味着最大可能的访问权             die("upload files directory does not exist and creation failed");            }         }             //建立txt日期文件         if (!file_exists($file_path)) {           //建立 “建立日期文件”           fopen($file_path,"w+");           //首先要确定文件存在并且可写           if (is_writable($file_path)) {             //使用添加模式打开$filename,文件指针将会在文件的开头             if(!$handle=fopen($file_path,'a')){               echo "不能打开文件 $filename";               exit;             }             //将$somecontent写入到我们打开的文件中。             if(!fwrite($handle,$error_content)){               echo "不能写入到文件 $filename";               exit;             }             echo "——错误记录被保存!";             //关闭文件             fclose($handle);           }else{             echo "文件 $filename 不可写";           }         }else{           //首先要确定文件存在并且可写           if (is_writable($file_path)) {             //使用添加模式打开$filename,文件指针将会在文件的开头             if (!$handle = fopen($file_path, 'a')) {               echo "不能打开文件 $filename";               exit;             }             //将$somecontent写入到我们打开的文件中。             if (!fwrite($handle, $error_content)) {               echo "不能写入到文件 $filename";               exit;             }             //echo "文件 $filename 写入成功";             echo "——错误记录被保存!";             //关闭文件             fclose($handle);           }else{             echo "文件 $filename 不可写";           }         }       }       echo "
";       if($this->is_error){         exit;       }     }     echo "

";     echo "";     echo "
";   }     /*获得客户端真实的IP地址*/     function getip() {           if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {             $ip = getenv("HTTP_CLIENT_IP");           } else           if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {                 $ip = getenv("HTTP_X_FORWARDED_FOR");             } else                 if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {                     $ip = getenv("REMOTE_ADDR");                 } else                     if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {                         $ip = $_SERVER['REMOTE_ADDR'];                     } else {                         $ip = "unknown";                     }         return ($ip);         }       } ?>

4.后台登录以及用户权限判断
后台登录以及用户权限的判断,主要通过session加密来实现,封装好权限判断函数,在每个后台页面引用即可。
(1).首先,我们需要先创建后台调用文件admin_global.php,源码如下:

<?php
session_start();
include_once ("../common/mysql.class.php"); //mysql类
include_once ("../configs/config.php"); //配置参数
include_once ("common/action.class.php"); //数据库操作类
include_once ("common/page.class.php"); //数据库操作

$db = new action($mydbhost, $mydbuser, $mydbpw, $mydbname, ALL_PS, $mydbcharset); //数据库操作类

//将存在的session[uid]和session[shell]赋值,便于后续的调用判断
$uid = $_SESSION[uid];
$shell = $_SESSION[shell];    
?>

(2).创建action.class.php文件,包含用户登录、权限判断等功能,源码如下:

<?php
class action extends mysql{

  //用户权限判断
  public function Get_user_shell($uid,$shell){
    $query=$this->select('p_admin',"*",'`uid`=\''.$uid.'\'');
    $us=is_array($row=$this->fetch_array($query));
    $ps=$us ? $shell=md5($row[username].$row[password]."TKBK") : FALSE;
    return $shell ? $row : NULL;
  }

  public function Get_user_shell_check($uid,$shell,$m_id=9){
    if($row=$this->Get_user_shell($uid,$shell)){
      if($row[m_id] <= $m_id){
        return $row;
      }else{
        echo "你无权限操作!";
        exit();
      }
    }else{
      $this->Get_admin_msg('index.php','请先登录');
    }
  }
    //===================================

  //用户登录时间超时
  public function Get_user_ontime($long='3600'){
    $new_time=mktime();
    $onlinetime=$_SESSION[ontime];
    if($new_time-$onlinetime>$long){
      echo "登录超时";
      session_destroy();
      exit();
    }else{
      $_SESSION[ontime]=mktime();
    }
  }

  //用户退出登录
  public function Get_user_out(){
    session_destroy();
    $this->Get_admin_msg('index.php','退出成功');
  }

  //用户登录
  public function Get_user_login($username,$password){
    $username=str_replace(" ","",$username);   //过滤提交上来username中的空格
    $query=$this->select('p_admin','*','`username` = \''.$username.'\'');//调用查询方法,查询管理员表中的username
    $us=is_array($row=$db->fetch_array($query));
    $ps=$us ? md5($password)==$row[password] : FALSE;

    if($ps){
      $_SESSION[uid]=$row[uid];
      $_SESSION[shell]=md5($row[username].$row[password]."TKBK");
      $_SESSION[ontime]=mktime();
      $this->Get_admin_msg('main.php','登录成功!');
    }else{
      $this->Get_admin_msg('index.php','密码或用户错误!');
      session_destroy();
    }
  }

  /**
    * 后台通用信息提示
    */
    public function Get_admin_msg($url, $show = '操作已成功!') {
        $msg = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml"><head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <link rel="stylesheet" href="css/common.css" type="text/css" />
                <meta http-equiv="refresh" content="2; URL=&#39; . $url . &#39;" />
                <title>管理区域</title>
                </head>
                <body>
                <p id="man_zone">
                  <table width="30%" border="1" align="center"  cellpadding="3" cellspacing="0" class="table" style="margin-top:100px;">
                    <tr>
                      <th align="center" style="background:#cef">信息提示</th>
                    </tr>
                    <tr>
                      <td><p>' . $show . '<br />
                      2秒后返回指定页面!<br />
                      如果浏览器无法跳转,<a href="&#39; . $url . &#39;">请点击此处</a>。</p></td>
                    </tr>
                  </table>
                </p>
                </body>
                </html>';
        echo $msg;
        exit ();
    }  

}
?>

(3).登录首页/admin/index.php,源码如下:

<?php
include_once(&#39;admin_global.php&#39;);
if(!empty($_POST[username]) && !empty($_POST[password])){
    $db->Get_user_login($_POST[username],$_POST[password]);    
}
?>

<html>
  <head>
    <meta http-equiv=&#39;Content-Type&#39; content=&#39;text/html; charset=gb2312&#39;>
    <meta name=&#39;Author&#39; content=&#39;Alan&#39;>
    <link rev=MADE href=&#39;mailto:haowubai@hotmail.com&#39;><title>后台管理</title>
    <link rel=&#39;stylesheet&#39; type=&#39;text/css&#39; href=&#39;images/private.css&#39;>
    <script> if(self!=top){ window.open(self.location,'_top'); } </script>
  </head>
  <body>
    <br><br><br>
    <form action="" method="post">
    <table border=0 cellspacing=1 align=center class=form>
    <tr>
        <th colspan="2">用户登录</th>
    </tr>
           <tr>
             <td align="right">登录用户:</td>
             <td><input type="text" name="username" value="" size="20" maxlength="40"/>  </td>
           </tr>
           <tr>
            <td align="right">登录密码:</td>
            <td><input type="password" name="password" value="" size="20" maxlength="40"/>  </td>
           </tr>
           <tr>
            <td colspan="2" align="center" height=&#39;30&#39;>
             <input type="submit" name="update" value=" 登录 "/>
           </td>  
          </tr>
         </table>
       </form>
     </body>
</html>

到此已实现用户的后台登录验证,用户权限的判断只需将以下代码粘贴到每个后台页面即可:

include_once('admin_global.php');
$r=$db->Get_user_shell_check($uid,$shell);

(4).后台管理页面:
后台全局调用页面admin_global.php
后台首页main.php
左侧导航页面admin_left.php
网站参数配置页面admin_main.php
新闻栏目分类管理页面admin_news_class.php
新闻列表页面admin_news_list.php
新闻编辑添加页面admin_news_add.php
各个页面源码如下:

admin_global.php:引入各类文件

<?php
session_start();
include_once ("../common/mysql.class.php"); //mysql类
include_once ("../configs/config.php"); //配置参数
include_once ("common/action.class.php"); //数据库操作类
include_once ("common/page.class.php"); //数据库操作

$db = new action($mydbhost, $mydbuser, $mydbpw, $mydbname, ALL_PS, $mydbcharset); //数据库操作类.

$uid = $_SESSION[uid];
$shell = $_SESSION[shell];    
?>

main.php:利用frame标签引入各个功能页面

<!DOCTYPE html>
<html>
  <head><title>网站后台控制面板</title>
    <meta http-equiv=Content-Type content="text/html; charset=gb2312">
    <script language=JavaScript>
      window.self.focus();
    </script>
  </head>
  <frameset border=0 frameSpacing=0 frameBorder=0 cols=150,*>
    <frame name=menu src="admin_left.php" scrolling=yes>
    <frame name=main src="admin_main.php" scrolling=yes>
    <noframes>
    </noframes>
  </frameset>
</html>

admin_left.php:后台左侧导航

<?php
include_once(&#39;admin_global.php&#39;);
$r=$db->Get_user_shell_check($uid,$shell);
?>
<!DOCTYPE HTML>
<html>
  <head>
    <title>PHP100_left</title>
    <meta http-equiv=Content-Type content="text/html; charset=gb2312">
    <link href="images/private.css" type=text/css rel=stylesheet>
    <script language=javascript>
      <!--
      function menu_tree(meval)
        {
        var left_n=eval(meval);
        if (left_n.style.display=="none")
          { eval(meval+".style.display=&#39;&#39;;"); }
      else
      { eval(meval+".style.display=&#39;none&#39;;"); }
    }
    -->
   </script>

 </head>
 <body>
  <center>
   <table class=Menu cellSpacing=0>
    <tbody>
     <tr><th onClick="javascript:menu_tree(&#39;left_1&#39;);" align=middle>≡ 基础操作 ≡</th></tr>
     <tr id=left_1>
    <td>
      <table width="100%">
        <tbody>
        <tr>
     <td> 
     <A href="admin_main.php" target=main>配置信息</A></td>
       </tr>
       <tr>
          <td>
      <A onClick="return confirm(&#39;提示:您确定要退出系统吗?&#39;)" href="admin_main.php?action=logout"  target=_parent>退出后台</A>
     </td>
        </tr>
     </tbody></table>
     </td></tr></tbody></table>

<table class=Menu cellSpacing=0 style="margin-top:5px">
  <tbody>
  <tr>
    <th onClick="javascript:menu_tree(&#39;left_2&#39;);" align=middle>≡ 新闻内容 ≡</th></tr>
  <tr id=left_2>
    <td>
      <table width="100%">
        <tbody>
        <tr>
            <td>
          <A href="admin_news_class.php" target=main>新闻分类</A></td>
        </tr>
        <tr>
            <td> 
          <A href="admin_news_list.php" target=main>新闻列表</A></td>
        </tr>
        <tr>
            <td>
          <A href="admin_news_add.php" target=main>添加新闻</A></td>
        </tr>
     </tbody></table>
     </td></tr></tbody></table>

<table class=Menu cellSpacing=0 style="margin-top:5px">
  <tbody>
  <tr>
    <th align=middle>〓 版本信息 〓</th></tr>
  <tr>
    <td align=middle><a href="http://www.php100.com" target="_blank">PHP100news 1.0</a></td></tr>
  <tr>
    <td align=middle>PHP100.com</td></tr></tbody></table>
</center>
</body>
</HTML>

admin_main.php:网站参数配置页面

<?php
include_once (&#39;admin_global.php&#39;);

$r=$db->Get_user_shell_check($uid, $shell);

if($_GET[action]=='logout')$db->Get_user_out();

 $query=$db->findall("p_config");
 while($row=$db->fetch_array($query)){
     $row_arr[$row[name]]=$row[values];
 }


 if(isset($_POST['update'])){
     unset($_POST['update']);
     foreach($_POST as $name=>$values){
         $db->query("update p_config set `values`='$values' where `name`='$name'");
     }
     $db->Get_admin_msg("admin_main.php");
 }


?>

<html>
<head><title>后台管理</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<link href="images/private.css" type=text/css rel=stylesheet>
</head>
<body>
<table class=navi cellSpacing=1 align=center border=0>
  <tbody>
  <tr>
      <form action="" method="POST">
    <th>后台 >> 系统配置</th></tr></tbody></table><br>

    <table border=0 cellspacing=1 align=center class=form>
    <tr>
        <th colspan="2">系统配置</th>
    </tr>
           <tr>
  <td align="right">网站名称:</td>
  <td><input type="text" name="websitename" value="<?php echo $row_arr[websitename]?>"/>  </td>
  </tr>
             <tr>
  <td align="right">网站地址:</td>
  <td><input type="text" name="website_url" value="<?php echo $row_arr[website_url]?>"/>  </td>
  </tr>
               <tr>
  <td align="right">关键字:</td>
  <td><input type="text" name="website_keyword" size=40 value="<?php echo $row_arr[website_keyword]?>"/>  </td>
  </tr>
                 <tr>
  <td align="right">说明:</td>
  <td><input type="text" name="website_cp" size=40 value="<?php echo $row_arr[website_cp]?>"/>  </td>
  </tr>
                   <tr>
  <td align="right">电话:</td>
  <td><input type="text" name="website_tel" size=40 value="<?php echo $row_arr[website_tel]?>"/>  </td>
  </tr>
                   <tr>
  <td align="right">email:</td>
  <td><input type="text" name="website_email" size=40 value="<?php echo $row_arr[website_email]?>"/>  </td>
  </tr>
  <tr>
    <td colspan="2" align="center" height=&#39;30&#39;>
  <input type="submit" name="update" value=" 更新 "/>

  </td>  </form>
    </tr>
    </table>

    </body>
    </html>

admin_news_class.php:新闻栏目分类管理页面

<?php
include_once (&#39;admin_global.php&#39;);
$r=$db->Get_user_shell_check($uid,$shell);

if(isset($_POST['into_class'])){
    $db->query("INSERT INTO `p_newsclass` (`id`,`f_id`,`name`,`keywrod`,`remark`) VALUES (NULL,'$_POST[f_id]','$_POST[name]','','')");
    $db->Get_admin_msg("admin_news_class.php","已经成功添加分类");    
}

if(!empty($_GET[del])){
    $db->query("DELETE FROM `p_newsclass` WHERE `id` = '$_GET[del]' LIMIT 1;");
    $db->Get_admin_msg("admin_news_class.php","删除成功");
}

if(isset($_POST[update_class])){
    $db->query("update `p_newsclass` set `name` = '$_POST[name]' WHERE `id` = '$_POST[id]' LIMIT 1;");
    $db->Get_admin_msg("admin_news_class.php","更新成功");    
}
?>

<html>
<head>
<title>后台管理</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<link href="images/private.css" type=text/css rel=stylesheet>
</head>
<body>
<table class=navi cellSpacing=1 align=center border=0>
  <tbody>
  <tr>



    <th>后台 >> 新闻分类</th></tr></tbody></table><br>

    <table border=0 cellspacing=1 align=center class=form>
    <tr>
        <th colspan="2">添加分类</th>
    </tr>
    <form action="" method="post" >
    <tr>
    <td colspan="2" align="center" height=&#39;30&#39;>

  <select name="f_id">
    <option value="0">添加大类</option>
    <?php
    $query=$db->findall("p_newsclass where f_id=0");
    while ($row=$db->fetch_array($query)) {
        $news_class_arr[$row[id]]=$row[name];
      echo "<option value=\"$row[id]\">$row[name]</option>";
    }
    ?>

  </select>
    <input type="text" name="name" value="" />
    <input type="submit" name="into_class" value=" 添加分类 "/>
    </td>
    </form>
    </tr>
    </table>
<br>
        <table border=0 cellspacing=1 align=center class=form>
    <tr>
    <th>系统分类</th>
    </tr>
<?php
    foreach($news_class_arr as $id=>$val){

?>

  <tr>
  <form action="" method="post">
  <td>
  <input type="hidden" name="id" value="<?php echo $id ?>" />
  <input type="text" name="name" value="<?php echo $val ?>"/>
  <input type="submit" name="update_class" value="更新"/>
  <input type="button" value="删除" onclick="location.href=&#39;?del=<?php echo $id ?>'"/>
  </form>
<?php
    $query_fid=$db->findall("p_newsclass where f_id=$id");
    while($row_fid=$db->fetch_array($query_fid)){
?>

  <form action="" method="post">
     ┗<input type="hidden" name="id" value="<?php echo $row_fid[id]?>" />
  <input type="text" name="name" value="<?php echo $row_fid[name]?>"/>
  <input type="submit" name="update_class" value="更新"/>
  <input type="button" value="删除" onclick="location.href=&#39;?del=<?php echo $row_fid[id]?>'">
  </form>
<?php
    }
?>
  </td>
  </tr>
    <?php
    }
    ?>

    </table>
    </body></html>

admin_news_list.php:新闻列表页面

<?php
include_once(&#39;admin_global.php&#39;);
$r=$db->Get_user_shell_check($uid,$shell);

$query=$db->findall("p_newsclass");
while($row=$db->fetch_array($query)){
    $news_class_arr[$row[id]]=$row[name];    
}

if(!empty($_GET[del])){
    $db->query("DELETE FROM `p_newsbase` WHERE `id` = '$_GET[del]'");
    $db->query("DELETE FROM `p_newscontent` WHERE `nid` = '$_GET[del]' LIMIT 1;");
    $db->Get_admin_msg("admin_news_list.php","删除成功");    
}

?>


<html>
<head>
<title>后台管理</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<link href="images/private.css" type=text/css rel=stylesheet>
</head>
<body>
<table class=navi cellSpacing=1 align=center border=0>
  <tbody>
  <tr>
    <th>后台 >> 新闻管理</th></tr></tbody></table><br>

    <table border=0 cellspacing=1 align=center class=form>
    <tr>
        <th width=&#39;100&#39;>新闻分类</th><th>新闻标题</th><th width=&#39;100&#39;>作者</th><th width=&#39;100&#39;>日期</th><th width=&#39;100&#39;>操作</th>
    </tr>
    <?php
    $result = mysql_query("select id from p_newsbase");
    $total = mysql_num_rows($result);
    pageft($total, 2);
    if ($firstcount < 0) $firstcount = 0;
   $query = $db->findall("p_newsbase limit  $firstcount, $displaypg");
   while ($row = $db->fetch_array($query)) {
   ?>
        <td><?php echo $news_class_arr[$row[cid]]?></td><td><?php echo $row[title]?></td><td><?php echo $row[author]?></td>
        <td><?php echo date("Y-m-d H:i",$row[date_time])?></td><td><a href=&#39;?del=<?php echo $row[id]?>'>删除</a> / <a href=&#39;admin_news_edit.php?id=<?php echo $row[id]?>'>修改</a></td>

    </tr>
    <?php
}
?>
    <tr>
        <th colspan="5"><?php echo $pagenav; echo $displaypg ?></th>
    </tr>

    <tr>
        <th colspan="5"></th>
    </tr>
    </table>
<br>
    </body>
</html>

admin_news_add.php:新闻编辑添加页面

<?php
include_once(&#39;admin_global.php&#39;);
$r=$db->Get_user_shell_check($uid,$shell);

if(isset($_POST[into_news])){
    $db->query("insert into `p_newsbase` (`id`,`cid`,`title`,`author`,`date_time`)" . 
    "values (NULL,'$_POST[cid]','$_POST[title]','$_POST[author]','".mktime()."')");
    $last_id=$db->insert_id();    
    $db->query("insert into `p_newscontent` (`nid`,`keywrod`,`content`,`remark`)" . 
    "values ('$last_id','$_POST[keywrod]','$_POST[content]','')");
    $db->Get_admin_msg("admin_news_add.php","添加成功");
}

?>
<html>
<head>
<title>后台管理</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
<link href="images/private.css" type=text/css rel=stylesheet>
<meta content="MShtml 6.00.6000.16890" name=GENERATOR></head>
<body>
<table class=navi cellSpacing=1 align=center border=0>
  <tbody>
  <tr>
    <th>后台 >> 添加新闻</th></tr></tbody></table><br>

    <table border=0 cellspacing=1 align=center class=form>
    <tr>
        <th colspan="2">添加分类</th>
    </tr>
    <form action="" method="post" onsubmit="syncTextarea()" >
    <tr>
   <td width=80>新闻分类</td>
  <td>
  <select name="cid">
    <option value="0">添加大类</option>
    <?php
        $query=mysql_query("select * from `p_newsclass` where `f_id` = 0");
        while($row=mysql_fetch_array($query)){
            echo "<option value=\"$row[id]\">$row[name]</option>";

            $query_son=mysql_query("select * from `p_newsclass` where `f_id` = '$row[id]'");
            while($row_son=mysql_fetch_array($query_son)){
                echo "<option value=\"$row_son[id]\">   ┗$row_son[name]</option>";
            }    
        }
    ?>
  </select>
    </td></tr>
   <tr>
   <td width=80>新闻标题</td>
  <td>
  <input type="text" name="title" size=50>
  </select>
    </td>
    </tr>
       <tr>
   <td width=80>新闻作者</td>
  <td>
  <input type="text" name="author" size=20>
    </td>
    </tr>
       <tr>
   <td width=80>新闻关键字</td>
  <td>
  <input type="text" name="keywrod" size=80>
    </td>
    </tr>
       <tr>
   <td width=80>新闻内容</td>
  <td>
  <textarea id="edited" name="content" style="width:95%;height:280px;"></textarea>
    <script language="javascript" type="text/javascript" src="edit/whizzywig.js"></script>
    <script type="text/javascript">buttonPath = "edit/images/";makeWhizzyWig("edited", "all");</script>
    </td>
    </tr>
    <tr>
   <td width=80></td>
  <td>
  <input type="submit" name="into_news" style="height:30px;" value="添加新闻">
    </td>
    </tr>
     </form>
    </table>
<br>

    </body></html>

(5)分页功能page.class.php
后台和前台分别调用了不同的分页函数,以免混淆,不过由于函数功能基本不一样,这里只贴出前台的分页函数源码:

<?php

$page=$_GET[&#39;page&#39;];
if(!function_exists(pageft)){

    function pageft($total,$displaypg){

        global $page,$pagenav,$firstcount;

        $GLOBALS["displaypg"]=$displaypg;

        if(!$page) $page=1;

        //获取url
        $url=$_SERVER[&#39;REQUEST_URI&#39;];
        $parse_url=parse_url($url);
        $url_query=ereg_replace("(^|&)page=$page","",$parse_url["query"]);
        $url=$parse_url[path]."?".$url_query."&page";

        $lastpg=ceil($total/$displaypg);
        $page=min($lastpg,$page);
        $prepg=$page-1;
        $nextpg=$page+1;

        //数据记录提取初始值
        $firstcount=($page-1)*$displaypg;

        $pagenav=&#39;&#39;;
        $pagenav.=&#39;共&#39;.$total.&#39;条记录 &#39;;

        if($page==1){
            $pagenav.=" <span>首页</span> ";
        }else{
            $pagenav.="<a href=&#39;$url=1&#39;>首页</a> ";
        }

        if($page==1){
            $pagenav.=" <span>上一页</span> ";
        }else{
            $pagenav.="<a href=&#39;$url=$prepg&#39;>上一页</a> ";
        }

        for($i=1;$i<=$lastpg;$i++){
            if($i==$page) $pagenav.=" <span>$i</span> ";
            else $pagenav.=" <a href=&#39;$url=$i&#39;>$i</a> ";    
        }

        if($page==$lastpage){
            $pagenav.=" <span>下一页</span> ";
        }else{
            $pagenav.="<a href=&#39;$url=$nextpg&#39;>下一页</a> ";
        }

        if($page==$lastpg){
            $pagenav.=" <span>末页</span> ";
        }else{
            $pagenav.="<a href=&#39;$url=$lastpg&#39;>末页</a> ";
        }

    }
}
?>

(6)前台首页
包括首页程序文件index.php,smarty模板文件index.html,源码如下:
index.php:

<?php

include_once(&#39;global.php&#39;);

$sql="select * from `p_newsclass` where f_id=0 order by id DESC";
$query=$db->query($sql);
while($row_class=$db->fetch_array($query)){
    $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]);    
}

$smarty->assign("sm_class",$sm_class);

$sql="select * from `p_newsbase` order by id DESC limit 5";
$query=$db->query($sql);
while($row_news=$db->fetch_array($query)){
    $sm_news[]=array('title'=>$row_news[title],'id'=>$row_news[id]);
}

$smarty->assign('sm_news',$sm_news);

$sql="select * from `p_config`";
$query=$db->query($sql);
while($row_config=$db->fetch_array($query)){
    $sm_config[$row_config[name]]=$row_config[values];    
}



$smarty->assign('sm_config',$sm_config);
$smarty->display("index.html");


?>

index.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{$sm_config.websitename}</title>
<link href="{$t_dir}css/common.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/layout.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/red.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p class="content border_bottom">
     <ul id="sub_nav">
         <li><a href="{$sm_config[0]}">设为首页</a></li>
         <li><a href="#">加入收藏</a></li>
         <li class="nobg"><a href="#">联系我们</a></li>
     </ul>
          <br class="clear" />
</p>
<p class="content dgreen-bg">
     <p class="content">
     <ul id="main_nav">
         <li class="nobg"><a href="index.php">新闻首页</a></li>
        {section name=l loop=$sm_class}
         <li><a href="list.php?cid={$sm_class[l].id}">{$sm_class[l].name}</a></li>
        {/section}
     </ul><br class="clear" />
     </p>
</p>
<p class="content">
     <p id="left-nav-bar" class="bg_white">
          <p id="top-contact-info">
          姓名:<br />
          电话:<br />
          OICQ:<br />
          手机:<br />
          地址:
          </p>
          <h2>招商信息</h2>
          <ul>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
          </ul>
          <h2>企业资讯</h2>
          <ul>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
              <li><a href="#">千亿美元进中国 推高股价房价</a></li>
          </ul>

          <h3><a href="http://www.php100.com">[站外图片上传中……(4)]</a></h3>
       <span id="hits">现在已经有[122]次点击</span>
  </p> 
  <p id="right-cnt">
          <p class="col_center">
            <p class="sub-title"><h2>促销活动</h2><span><a href="#" class="cblue">MORE</a></span><br class="clear" />
            </p>
            <ul>
            {section name=l loop=$sm_news}
                <li><a href="view.php?id={$sm_news[l].id}">{$sm_news[l].title}</a></li>
            {/section}    
          </p>
          <p class="col_center right">
            <p class="sub-title"><h2>公司简介</h2><span><a href="#" class="cblue">MORE</a></span><br class="clear" /></p>
            <p id="intro">
            入贯彻落实科学发展观的自觉性和坚定性湖南一
考生高考4门课程故意考零分温家宝调研太湖污染代表
中央向居民致歉湖南73人涉黑集团麻醉强奸女服务员
被公诉女大学生卖淫被抓警察让其参加毕被公诉女大学生卖淫被抓警察让其参加毕...[<a href="#" class="cgray">详细</a>]           </p>
          </p><br class="clear" />
          <p id="m_adv"></p>

            <p class="pages"><h2>产品展示</h2><span>产品分类:展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示 | 展示</span><p id="more"><a href="#" class="cblue">MORE</a></p>
            <br class="clear" /></p>
            <ul id="products-list">
                <li>

                <h3>产品展示</h3>
                <ul>
                    <li>规格:</li>
                    <li>产地:</li>
                    <li>价格:1200 <span>[<a href="#" class="cdred">详细</a>]</span></li>
                </ul>
                </li>
                <li>

                <h3>产品展示</h3>
                <ul>
                    <li>规格:</li>
                    <li>产地:</li>
                    <li>价格:1200 <span>[<a href="#" class="cdred">详细</a>]</span></li>
                </ul>
                </li>
                <li>

                <h3>产品展示</h3>
                <ul>
                    <li>规格:</li>
                    <li>产地:</li>
                    <li>价格:1200 <span>[<a href="#" class="cdred">详细</a>]</span></li>
                </ul>
                </li>
                <li>

                <h3>产品展示</h3>
                <ul>
                    <li>规格:</li>
                    <li>产地:</li>
                    <li>价格:1200 <span>[<a href="#" class="cdred">详细</a>]</span></li>
                </ul>
                </li>
            </ul><br class="clear" />
  </p>

  <br class="clear" />
</p>
<p id="about">
     <p class="content">
          <span class="left"><a href="#">网店首页</a> | <a href="#">公司介绍</a> | <a href="#">资质认证</a> | <a href="#">产品展示</a> | <a href="#">视频网店</a> | <a href="#">招商信息</a> | <a href="#">招聘信息</a> | <a href="#">促销活动</a> | <a href="#">企业资讯</a> | <a href="#">联系我们</a></span>
          <span class="right">我的邮件:{$sm_config.website_email}</span>
     </p>
</p>
<p id="copyright">
</p>
</body>
</html>

(7)新闻列表页
包含列表程序文件list.php,smarty模板文件:list.html
list.php

<?php

include_once(&#39;global.php&#39;);

//id为空,不执行该页
if(empty($_GET[cid])) exit();


//引入title
$query=$db->findall('p_config');
while($row_config=$db->fetch_array($query)){
    $sm_config[$row_config[name]]=$row_config[values];    
}

$smarty->assign('sm_config',$sm_config);


//配置导航
$sql="select * from `p_newsclass` where f_id=0 order by id DESC";
$query=$db->query($sql);
while($row_class=$db->fetch_array($query)){
    $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]);    
}

$smarty->assign('sm_class',$sm_class);


$query = $db->findall("p_newsclass");
while ($row = $db->fetch_array($query)) {
    $news_class_arr[$row[id]] = $row[name];
}


//打印左侧当前文章栏目分类
$query=$db->findall("p_newsclass where f_id = $_GET[cid]");
while($row_chlidclass=$db->fetch_array($query)){
    $news_class_in.=$row_chlidclass[id].",";
    $news_class_list_arr[]=array('name'=>$row_chlidclass[name],'id'=>$row_chlidclass[id]);
}



$news_class_in.="$_GET[cid]";

//文章列表
$sql="select id from `p_newsbase` where cid in ($news_class_in)";
$total=mysql_num_rows(mysql_query($sql));
pageft($total,2);
if($firstcount<0) $firstcount=0;
$query=$db->query("select * from `p_newsbase` where cid in ($news_class_in) limit $firstcount,$displaypg");
while($row_list=$db->fetch_array()){
    $sm_list[]=array(
        "title"=>$row_list[title],
        'cid'=>$row_list[cid],
        'id'=>$row_list[id],
        'cidname'=>$news_class_arr[$row_list[cid]]);
}


$smarty->assign("news_class_list_arr",$news_class_list_arr);
$smarty->assign("sm_list",$sm_list);
$smarty->assign("pagenav", $pagenav); //新闻分页


$smarty->display("list.html");


?>

list.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{$sm_config.websitename}</title>
<link href="{$t_dir}css/common.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/layout.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/red.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p class="content border_bottom">
     <ul id="sub_nav">
         <li><a href="{$sm_config[0]}">设为首页</a></li>
         <li><a href="#">加入收藏</a></li>
         <li class="nobg"><a href="#">联系我们</a></li>
     </ul>
          <br class="clear" />
</p>
<p class="content dgreen-bg">
     <p class="content">
     <ul id="main_nav">
         <li class="nobg"><a href="index.php">新闻首页</a></li>
        {section name=l loop=$sm_class}
         <li><a href="list.php?cid={$sm_class[l].id}">{$sm_class[l].name}</a></li>
        {/section}
     </ul><br class="clear" />
     </p>
</p>
<p class="content">
     <p id="left-nav-bar" class="bg_white">
          <p id="top-contact-info">
          {section name=l loop=$news_class_list_arr}
          <a href=list.php?cid={$news_class_list_arr[l].id}>{$news_class_list_arr[l].name}</a><br>
          {/section}
          </p>
  </p> 
  <p id="right-cnt"><br class="clear" />
    <p class="pages">
      <h2>类别</h2>
      <span>新闻标题</span>
      <p id="more"><a href="#">时间</a></p>
    </p>

    {section name=l loop=$sm_list}
     <p class="listtt">
      <h2><a href="list.php?cid={$sm_list[l].cid}">{$sm_list[l].cidname}</a></h2>
      <span><a href="view.php?id={$sm_list[l].id}">{$sm_list[l].title}</a></span>
      <p id="more">{$sm_list[l].date_time}</p>
    </p>
    {/section}

    {$pagenav}

            </p>

  <br class="clear" />
</p>
<p id="about">
     <p class="content">
          <span class="left"><a href="#">网店首页</a> | <a href="#">公司介绍</a> | <a href="#">资质认证</a> | <a href="#">产品展示</a> | <a href="#">视频网店</a> | <a href="#">招商信息</a> | <a href="#">招聘信息</a> | <a href="#">促销活动</a> | <a href="#">企业资讯</a> | <a href="#">联系我们</a></span>
          <span class="right">我的邮件:{$sm_config.website_email}</span>
     </p>
</p>
<p id="copyright">
</p>
</body>
</html>

(8) 新闻内容页
包括新闻内容程序文件view.php,smarty模板文件view.html
view.php

<?php
include_once(&#39;global.php&#39;);
if(empty($_GET[id])) exit();

//配置标题
$sql="select * from `p_config`";
$query=$db->query($sql);
while($row_config=$db->fetch_array($query)){
    $sm_config[$row_config[name]]=$row_config[values];    
}

$smarty->assign("sm_config",$sm_config);

//配置栏目导航
$sql="select * from `p_newsclass` where f_id=0";
$query=$db->query($sql);
while($row_class=$db->fetch_array($query)){
    $sm_class[]=array('name'=>$row_class[name],'id'=>$row_class[id]);    
}

$smarty->assign("sm_class",$sm_class);


$sql="select * from `p_newsbase` as a,p_newscontent as b where a.id=b.nid and  a.id=$_GET[id]";
$query=$db->query($sql);
$row_news=mysql_fetch_array($query,MYSQL_ASSOC);
$smarty->assign("row_news",$row_news);

$smarty->display("view.html");
?>

view.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{$sm_config.websitename}</title>
<link href="{$t_dir}css/common.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/layout.css" rel="stylesheet" type="text/css" />
<link href="{$t_dir}css/red.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p class="content border_bottom">
     <ul id="sub_nav">
         <li><a href="{$sm_config[0]}">设为首页</a></li>
         <li><a href="#">加入收藏</a></li>
         <li class="nobg"><a href="#">联系我们</a></li>
     </ul>
          <br class="clear" />
</p>
<p class="content dgreen-bg">
     <p class="content">
     <ul id="main_nav">
         <li class="nobg"><a href="index.php">新闻首页</a></li>
        {section name=l loop=$sm_class}
         <li><a href="list.php?cid={$sm_class[l].id}">{$sm_class[l].name}</a></li>
        {/section}
     </ul><br class="clear" />
     </p>
</p>
<p class="content"><br /> 
     <h2>{$row_news.title}<br />
       <br />
     </h2>
     时间:{$row_news.date_time|date_format:"%D"} 作者:{$row_news.author}<br />
     <br />
     <hr />
     <br />
     <p>

     {$row_news.content}


     </p>
     <br class="clear" />
</p>
<p id="about">
     <p class="content">
          <span class="left"><a href="#">网店首页</a> | <a href="#">公司介绍</a> | <a href="#">资质认证</a> | <a href="#">产品展示</a> | <a href="#">视频网店</a> | <a href="#">招商信息</a> | <a href="#">招聘信息</a> | <a href="#">促销活动</a> | <a href="#">企业资讯</a> | <a href="#">联系我们</a></span>
          <span class="right">目前已有[2222]点击</span>
     </p>
</p>
<p id="copyright">
</p>
</body>
</html>

以上是微型新闻系统的开发的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn