当前位置:Gxlcms > PHP教程 > PHP实现简单的新闻发布系统实例_php技巧

PHP实现简单的新闻发布系统实例_php技巧

时间:2021-07-01 10:21:17 帮助过:36人阅读

本文实例讲述了PHP实现简单的新闻发布系统。分享给大家供大家参考。具体如下:

本人小白,一直在公司用模板和框架写PHP,发现有时候连基本的sql语句都忘记了,所以有空想把PHP基础复习下,巩固下。分页和搜索,以及排序,还没写,后期继续更新...(代码修改:添加搜索和分页功能)

articlePublish.html:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  2. <title>Insert title here</title>

articlePublishDo.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. date_default_timezone_set('Asia/Shanghai');
  4. $title=trim($_POST['title']);
  5. $content=trim($_POST['content']);
  6. $time=date("y-m-d H:i:s");
  7. require_once 'init.php';
  8. $sql="insert into article(title,content,create_time) values('$title','$content','$time')";
  9. //echo $sql;
  10. $re=mysql_query($sql);//执行sql语句
  11. if($re){
  12. echo "发布成功";
  13. echo '返回文章列表';
  14. }else{
  15. echo "发布失败";
  16. echo '返回文章列表';
  17. }
  18. mysql_close();//关闭数据库

articleList.php:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  2. <title>Insert title here</title>
  3. <!--
  4. 搜索框
  5. -->
  6. <br>
  7. 返回发布文章
  8. <?php
  9. require_once 'init.php';
  10. /**
  11. * 搜索
  12. */
  13. $keyword=$_GET['search'];
  14. /*分页*/
  15. $sql="select count(*) from article where title like '%$keyword%' or content like '%$keyword%'";
  16. $res=mysql_query($sql);
  17. //$count= (int)mysql_num_rows($result);
  18. $arr=mysql_fetch_assoc($res);
  19. while(list($key,$val)=each($arr)){
  20. $count = (int)$val;
  21. }
  22. //echo $count;
  23. $pageSize=4;
  24. $page=floor($count/$pageSize)+1;//总页数$page
  25. echo $page;
  26. //echo $page;
  27. if(isset($_GET['page']))
  28. {
  29. //$currentPage = $_GET['page'];
  30. if($_GET['page'] <=1){
  31. $currentPage = 1;
  32. }elseif ($_GET['page'] >= $page){
  33. $currentPage = $page-1;
  34. }else{
  35. $currentPage = $_GET['page'];
  36. }
  37. }else
  38. {
  39. $currentPage=1;
  40. }
  41. $start = ($currentPage-1)*$pageSize;
  42. $sql="select id,title,content from article where title like '%$keyword%' or content like '%$keyword%' limit $start,$pageSize";
  43. //echo $sql;
  44. $re=mysql_query($sql);//执行sql语句
  45. while($arr=mysql_fetch_assoc($re)){
  46. ?>
  47. <?php
  48. }
  49. mysql_close();//关闭数据库
  50. ?>
  51. <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccc" width="500"><tbody><tr>
  52. <th>编号</th>
  53. <th>文章标题</th>
  54. <th>文章内容</th>
  55. <th>编辑文章</th>
  56. </tr><tr>
  57. <td align="center" style="border:1px solid #000"><?php echo $arr['id'];?></td>
  58. <input type="hidden" name="id" value="<?php echo $arr['id'];?>">
  59. <td align="center" style="border:1px solid #000"><?php echo $arr['title'];?></td>
  60. <td align="center" style="border:1px solid #000"><?php echo $arr['content'];?></td>
  61. <td align="center" style="border:1px solid #000">
  62. "><font color="red">修改</font>
  63. "><font color="red">删除</font>
  64. </td>
  65. </tr></tbody></table>
  66. 共<?php echo $page?>页 |查到<?php echo $count;?>条记录
  67. 当前第<?php echo $_GET['page']?>页|
  68. ">首页
  69. &search=<?php echo $keyword?>">|上一页
  70. &search=<?php echo $keyword?>">|下一页
  71. &search=<?php echo $keyword?>">|末页

articleEdit.php:

  1. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  2. <title>Insert title here</title>
  3. <?php
  4. $id=(int)$_GET['id'];
  5. require_once 'init.php';
  6. $sql="select id,title,content from article where id = '$id'";
  7. //echo $sql;
  8. $re=mysql_query($sql);//执行sql语句
  9. $arr=mysql_fetch_assoc($re);
  10. //var_dump($arr);
  11. mysql_close();//关闭数据库
  12. ?>

articleUpdate.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. $arr=$_POST;
  4. $id=(int)$arr['id'];
  5. require_once 'init.php';
  6. $sql="update article set title = '$arr[title]',content = '$arr[content]' where id = '$id'";
  7. //echo $sql;
  8. $re=mysql_query($sql);//执行sql语句
  9. //echo $re;
  10. if($re){
  11. echo "修改成功";
  12. echo "返回文章列表";
  13. }else{
  14. echo "修改失败";
  15. echo "返回文章列表";
  16. }
  17. mysql_close();//关闭数据库

articleDelete.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. require_once 'init.php';
  4. $id=(int)$_GET['id'];
  5. $sql="delete from article where id = '$id'";
  6. //echo $sql;
  7. $re=mysql_query($sql);
  8. if($re){
  9. echo "删除成功";
  10. echo "返回文章列表";
  11. }else{
  12. echo "删除失败";
  13. echo "返回文章列表";
  14. }

init.php:

  1. <?php
  2. //连接数据库
  3. //五步走
  4. //往数据库添加文章
  5. $conn=mysql_connect("localhost","root","");//链接数据库
  6. //echo $conn;
  7. $re=mysql_select_db("article");//选择数据库
  8. mysql_query("set names utf8");//设置交互字符集

基础知识总结:

文章发布系统

1.articlePublish.html 发布文章页面 提交到articlePublishDo.php页面,执行写入数据库

2.articleList.php 文章列表页面

3.点击编辑,修改文章 提交到 aiticleEdit.php 表单页面(回显)

4.点击修改文章按钮 提交到 articleUpdate.php

希望本文所述对大家的php程序设计有所帮助。

人气教程排行