ホームページ >バックエンド開発 >PHPチュートリアル >マイクロニュースシステムの開発
「経験して初めてその奥深さがわかります。」 このシステムを開発する目的は、主に学習し、システム開発プロセスを合理化し、さらに理論と実践を組み合わせることにあります。 phpを学ぶための高度な方法 重要なマイルストーン。
開発環境: Apache2.0+php5.4+mysql5.5
開発ツール: texteditor(dreamweaver/editplus)
1. バックステージメンバーシップ
2. 、権限の検証
3.オブジェクト、クラスのカプセル化
1.MySQLデータベース
には主に5つのデータテーブルがあります:完全なサイト構成テーブルp_config code>、バックエンドメンバーシップ テーブル <code>p_admin
、ニュース分類テーブル p_<a href="http://www.php.cn/wiki/165.html" target="_blank">新しい <code>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_newsclass
中f_id=0
时为顶级栏目,f_id=1
sクラス
p_newsbase
、ニュース コンテンツ テーブル p_newscontent
、以下に示すように: データシート
2. プログラムファイルウェブサイトディレクトリ構造
リスト (図に示す):
Webサイトのディレクトリ構成
4.開発内容詳細を一定のプロセスに沿って項目ごとに説明します( css およびその他のファイルは詳細にはリストされていません ):
1.mysqlデータベースの作成
次の手順 各テーブルの構成図を貼り付けます:
p_config テーブル
p_admin テーブル🎜
🎜🎜
p_newsclass table🎜
🎜🎜
p_newsbase テーブル🎜
🎜🎜
p_newscontent テーブル🎜🎜詳細な説明: 🎜
p_newsclass、p_newsbase、p_newscontent
これら 3 つのテーブルは関連しています: 🎜 p_newsbase| cid --> p_newsclass|id
🎜p_newscontent|nid -->p_newsbase|id
🎜🎜p_newsclass
中f_id= 0
が最上位列の場合、f_id=1
は、そのカテゴリが ID 1 のカテゴリのサブカテゴリに属していることを意味します。 🎜🎜🎜🎜🎜2.smartyの設定🎜🎜次のステップ🎜インストール🎜設定:🎜(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).グローバルグローバル呼び出しファイルを設定します。 ソースコードは次のとおりです: 🎜
<?php include_once('./configs/config.php'); //引入config.php include_once('./common/smarty/Smarty.class.php'); //引入smarty.class.php include_once('./common/mysql.class.php'); //引入mysql语句类文件 include_once('./common/action.class.php'); //引入动作执行类文件 include_once('./common/page.class.php'); //引入分页类文件 //实例化类,这里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>" . $message . "</font>"; echo "<br>"; }else{ echo ""; echo "
<?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]; ?>🎜(3) ホームページ/admin/index.php にログインします。ソースコードは次のとおりです。
<?php include_once('admin_global.php'); if(!empty($_POST[username]) && !empty($_POST[password])){ $db->Get_user_login($_POST[username],$_POST[password]); } ?> <meta> <meta> <link><title>后台管理</title> <link> <script> if(self!=top){ window.open(self.location,'_top'); } </script> <br><br><br>
到此已实现用户的后台登录验证,用户权限的判断只需将以下代码粘贴到每个后台页面即可:
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标签引入各个功能页面
nbsp;html> <title>网站后台控制面板</title> <meta> <script> window.self.focus(); </script> <frameset> <frame> <frame> <noframes> </noframes> </frameset>
admin_left.php:后台左侧导航
<?php include_once('admin_global.php'); $r=$db->Get_user_shell_check($uid,$shell); ?> nbsp;HTML> <title>PHP100_left</title> <meta> <link> <script> <!-- function menu_tree(meval) { var left_n=eval(meval); if (left_n.style.display=="none") { eval(meval+".style.display='';"); } else { eval(meval+".style.display='none';"); } } --> </script> <center> <table> <tbody> <tr><th>≡ 基础操作 ≡</th></tr> <tr> <td> <table> <tbody> <tr> <td> <a>配置信息</a> </td> </tr> <tr> <td> <a>退出后台</a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <table> <tbody> <tr> <th>≡ 新闻内容 ≡</th> </tr> <tr> <td> <table> <tbody> <tr> <td> <a>新闻分类</a> </td> </tr> <tr> <td> <a>新闻列表</a> </td> </tr> <tr> <td> <a>添加新闻</a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <table> <tbody> <tr> <th>〓 版本信息 〓</th> </tr> <tr> <td><a>PHP100news 1.0</a></td> </tr> <tr> <td>PHP100.com</td> </tr> </tbody> </table> </center>
admin_main.php:网站参数配置页面
<?php include_once ('admin_global.php'); $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"); } ?> <title>后台管理</title> <meta> <link>
系统配置 | |
---|---|
网站名称: | "/> |
网站地址: | "/> |
关键字: | "/> |
说明: | "/> |
电话: | "/> |
email: | "/> |
admin_news_class.php:新闻栏目分类管理页面
<?php include_once ('admin_global.php'); $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","更新成功"); } ?> <title>后台管理</title> <meta> <link>
后台 >> 新闻分类 |
---|
添加分类 |
---|
系统分类 |
---|
admin_news_list.php:新闻列表页面
<?php include_once('admin_global.php'); $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","删除成功"); } ?> <title>后台管理</title> <meta> <link>
后台 >> 新闻管理 |
---|
新闻分类 | 新闻标题 | 作者 | 日期 | 操作 | '>删除 / '>修改 |
---|---|---|---|---|
admin_news_add.php:新闻编辑添加页面
<?php include_once('admin_global.php'); $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","添加成功"); } ?> <title>后台管理</title> <meta> <link> <meta>
后台 >> 添加新闻 |
---|
添加分类 | |
---|---|
新闻分类 | |
新闻标题 | |
新闻作者 | |
新闻关键字 | |
新闻内容 | <script></script> <script>buttonPath = "edit/images/";makeWhizzyWig("edited", "all");</script> |
(5)分页功能page.class.php
后台和前台分别调用了不同的分页函数,以免混淆,不过由于函数功能基本不一样,这里只贴出前台的分页函数源码:
<?php $page=$_GET['page']; if(!function_exists(pageft)){ function pageft($total,$displaypg){ global $page,$pagenav,$firstcount; $GLOBALS["displaypg"]=$displaypg; if(!$page) $page=1; //获取url $url=$_SERVER['REQUEST_URI']; $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=''; $pagenav.='共'.$total.'条记录 '; if($page==1){ $pagenav.=" <span>首页 "; }else{ $pagenav.="<a>首页</a> "; } if($page==1){ $pagenav.=" <span>上一页</span> "; }else{ $pagenav.="<a>上一页</a> "; } for($i=1;$i$i "; else $pagenav.=" <a>$i</a> "; } if($page==$lastpage){ $pagenav.=" <span>下一页</span> "; }else{ $pagenav.="<a>下一页</a> "; } if($page==$lastpg){ $pagenav.=" <span>末页</span> "; }else{ $pagenav.="<a>末页</a> "; } } } ?>
(6)前台首页
包括首页程序文件index.php,smarty模板文件index.html,源码如下:
index.php:
<?php include_once('global.php'); $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:
<meta> <title>{$sm_config.websitename}</title> <link> <link> <link> <p> </p>
姓名:
电话:
OICQ:
手机:
地址:
入贯彻落实科学发展观的自觉性和坚定性湖南一 考生高考4门课程故意考零分温家宝调研太湖污染代表 中央向居民致歉湖南73人涉黑集团麻醉强奸女服务员 被公诉女大学生卖淫被抓警察让其参加毕被公诉女大学生卖淫被抓警察让其参加毕...[详细]
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 我的邮件:{$sm_config.website_email}
(7)新闻列表页
包含列表程序文件list.php,smarty模板文件:list.html
list.php
<?php include_once('global.php'); //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($firstcountquery("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
<meta> <title>{$sm_config.websitename}</title> <link> <link> <link> <p> </p>
{section name=l loop=$news_class_list_arr}
{$news_class_list_arr[l].name}
{/section}
{$sm_list[l].date_time}
{/section} {$pagenav}
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 我的邮件:{$sm_config.website_email}
(8) 新闻内容页
包括新闻内容程序文件view.php,smarty模板文件view.html
view.php
<?php include_once('global.php'); 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
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <meta> <title>{$sm_config.websitename}</title> <link> <link> <link> <p> </p>
{$row_news.content}
网店首页 | 公司介绍 | 资质认证 | 产品展示 | 视频网店 | 招商信息 | 招聘信息 | 促销活动 | 企业资讯 | 联系我们 目前已有[2222]点击
以上がマイクロニュースシステムの開発の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。