第一步,
index.php 首先执行执行包含进来的
require('./include/init.php');初始化脚本
1,检测当前环境
2,判断魔术引用有没有开启
3,链接数据库,选库
第二步,填写表单,
index.php通过post发送到pubaction.php
1, 打印出post发过来的表单 //print_r($_POST);先查看一下POST发过来的数据有没有问题?
2, if 用户名 标题 内容 是否为空,做出相应提示!
3, 执行 sql语句 插入到数据库 执行sql语句之前echo一下sql语句,有没有问题?
成功或者失败?
第三步,失败退出/成功进入下一步
1,返回数据到tie.php?tid=$tid
如何获取当前的tid?从数据库返回值获取!
1 2 3 4 5 6 7 8 9 10 11 12 |
if (!mysql_query($sql,$conn)) { exit('发帖失败'); }else{ $tid = mysql_insert_id($conn); $script = <<<EOT <script type = "text/javascript"> alert('发帖成功'); window.location.href = 'tie.php?tid=$tid'; </script> EOT; echo $script; } |
第四步,发送的帖子显示在发帖页?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
require('./include/init.php'); $tid = $_GET['tid'] + 0; //echo $tid; if ($tid <= 0) { //tid不可能<=0,必然是非法的tid再或者tid的值超出文章的tid(bool false)再做一个判断 //因此直接退出 exit('tid非法'); } $sql = 'select * from thread where tid =' . $tid; //这个sql要取出一条数据,因此用getRow函数 $thread = getRow($sql,$conn); //print_r($thread); if (empty($thread)) { //empty检测$thread是否为空 exit('tid非法'); } //帖子标题:<?php echo $thread['title'];?> //帖子用户:<?php echo $thread['username'];?> //帖子内容:<?php echo $thread['content'];?> |
第五步,如何发送的数据显示在首页?
1,foreach循环出发送的数据显示在首页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php /* php和htnl是可以随意混杂在一块的. 因为再解释的时候,解释器是见到<?php ?>标志就可以解释 */ require('./include/init.php'); //首页显示出来 $sql = 'select * from thread order by pubtime desc'; //根据时间倒序,最新发帖 $list = getAll($sql,$conn); foreach ($list as $v) { ?> <?php echo $v['title']?> <?php echo $v['username']?> <?php echo $v['content']?> <?php } ?> |
第六步,帖子的回复功能,用户名,账号,回复内容等等.
1,思路分析:单纯看一条回复,在数据库里,也是一行记录.
因此:回复的过程,无非也是 提交-->接收-->写入数据库
但是,要注意,回复的内容如何标示,该回复是哪一个帖子的呢?
我们需要再回复的帖子中,添加一个字段 tid
该字段值就是恢复帖子的tid值
比如tid为7,则说明你回复的thread表中的tid=7的帖子.获取当前id>>表单+隐藏表单>>提交表单>>repaction.php
2,回复发帖repaction.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php require('./include/init.php'); /* 回复第一步,接收数据 print_r($_POST); Array ( [tid] => 8 [username] => 测试你好 [content] => 测试你好 ) */ //第二步,检测数据 //判断如下,出现以下为空,退出提示非法! $tid = $_POST['tid'] + 0; if (!$tid || $_POST['username']==''||$_POST['content']=='') { exit('数据非法'); } //回复第三步,拼接sql /*tid username content reptime */ $time = time(); //返回当前时间戳 $sql = "insert into reply (tid,username,content,reptime) values (".$tid.",'".$_POST['username']."','".$_POST['content']."',".$time.")"; //echo $sql; //第四步 执行sql if (mysql_query($sql,$conn)) { $script = <<<EOT <script type = "text/javascript"> alert('回复成功'); window.location.href = 'tie.php?tid=$tid'; </script> EOT; echo $script; }else{ echo '回复失败'; } ?> |
3,拼接sql,格式化时间戳
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//继续取帖子的回复 $sql='select * from reply where tid='.$tid; //echo $sql;exit; //回复有可能1行到多行,用getAll()来取 $replys=getAll($sql,$conn); $num = count($replys);//数数帖子数量 ?> <!--回复数量--> <!--1条回复开始的地方--> <?php foreach($replys as $r){?> <li class="lzl_single_post j_lzl_s_p " data-field="{'pid':'85254985924','spid':'88467909116','user_name':'真诚是德行518','portrait':'3a55e79c9fe8af9ae698afe5beb7e8a18c3531388239'}"><a data-field="{'un':'真诚是德行518'}" target="_top" class="j_user_card lzl_p_p" href="http://tieba.baidu.com/home/main?un=%E7%9C%9F%E8%AF%9A%E6%98%AF%E5%BE%B7%E8%A1%8C518&ie=utf-8&fr=pb" username="真诚是德行518"><img src="3a55e79c9fe8af9ae698afe5beb7e8a18c3531388239"></a><div class="lzl_cnt"><a class="at j_user_card " data-field="{'un':'真诚是德行518'}" alog-group="p_author" target="_top" href="http://tieba.baidu.com/home/main?un=%E7%9C%9F%E8%AF%9A%E6%98%AF%E5%BE%B7%E8%A1%8C518&ie=utf-8&fr=pb" username="<?php echo $r['username'];?>"><?php echo $r['username'];?></a>: <span class="lzl_content_main">: <?php echo $r['content'];?><a href="http://jump.bdimg.com/safecheck/index?url=x+Z5mMbGPAs95UolpNuq+CwM2BpViN6QF0BYrUCVRu9fr65bvTalhcFlJ3zgkKV1RxcQZyJSDPnx7NtYTKkZxcqq5BaostGBkocM1jkuFoeL4MRChHi1OQQBafeT4Uf+m5DJ7YwbhtYwPGbuJnYGNA==" target="_top">http://150643.com/</a></span><div class="lzl_content_reply"><span class="lzl_jb" style="display: none;"><a href="#" data-field="{'delete_mine':'0','user_name':'真诚是德行518'}" class="lzl_jb_in">举报</a> | </span><span class="lzl_op_list j_lzl_o_l"></span><span class="lzl_time"> <?php date_default_timezone_set('PRC'); $ling=date('Y-m-d'); //现在的时间戳 $tmp= strtotime($ling);//零时的时间戳 if ($r['reptime']>=$tmp) { echo date('H:i',$r['reptime']); }else{ echo date('m-d',$r['reptime']); } ?> </span><a href="#" class="lzl_s_r">回复</a></div></div></li> <?php }?> <!--1条回复结束的地方--> |
第七步,发帖时间的优化
昨天之前的时间按照日期(m-d)显示,今天的时间安装时间(H:i)显示,判断
1,首页帖子的发布时间
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php date_default_timezone_set('PRC'); $ling=date('Y-m-d'); //现在的时间戳 $tmp= strtotime($ling);//零时的时间戳 if ($v['pubtime']>=$tmp) { echo date('H:i',$v['pubtime']); }else{ echo date('m-d',$v['pubtime']); } ?> |
2,回复帖子的时间
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php date_default_timezone_set('PRC'); $ling=date('Y-m-d'); //现在的时间戳 $tmp= strtotime($ling);//零时的时间戳 if ($r['reptime']>=$tmp) { echo date('H:i',$r['reptime']); }else{ echo date('m-d',$r['reptime']); } ?> |
3,首页显示主题帖到回复数量
1 2 3 |
//一条sql语句就可以实现,where子查询 $sql = 'select *,(select count(*) from reply where reply.tid=thread.tid) as rnum from thread order by pubtime desc'; $list = getAll($sql,$conn); |
密码:yo12

我的微信
把最实用的经验,分享给最需要的读者,希望每一位来访的朋友都能有所收获!