zoukankan      html  css  js  c++  java
  • PHP实战 新闻管理系统 使用到了bootstrap框架

    刚刚接触 PHP 仿照视频 写了个新闻管理系统 当中也使用到了bootstrap框架

    写下来整理一下思路。

    这是个非常easy的系统。首先是建立数据库表。

    mysql>create database newsdb

    mysql> create table news(
    -> id int unsigned not null auto_increment primary key,//这是新闻的id
    -> title varchar(64) not null,//这是新闻的标题
    -> keywords varchar(64) not null,//这是新闻的关键字
    -> author varchar(16) not null,//这是新闻的作者
    -> addtime int unsigned not null,//这是新闻的加入时间
    -> content text not null);//这是新闻的内容

    这样,数据库表就建成了,以下開始写页面。

    首先写了一个数据库配置文件dbconfig.php:

    <?php

    define(HOST,"localhost");//主机名

    define(USER,"root");//username

    define(PASS,"");//password

    define(DBNAME,"newsdb");//数据库名

     ?>

    然后是一个menu.php文件

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>HTML5&BootStrap</title>
    <link href="bootstrap-3.2.0-dist/css/bootstrap.css" rel="stylesheet">
    <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link href="prettify-4-Mar-2013/prettify.css" rel="stylesheet">
    <link href="style.css" rel="stylesheet">
    </head>
    <body onLoad="prettyPrint()">
    <style>
    body{background:orange;}
    @media(max-997px){body{background:#0FC;}}
    </style>
    <div class="container">
      <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid"> 
          <!-- Collect the nav links, forms, and other content for toggling -->
          <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
            <h2>新闻管理系统</h2>
              <li><a href="index.php">浏览新闻</a></li>
              <li><a href="add.php">公布新闻</a></li>
              <hr>
            </ul>
          </div>
          <!-- /.navbar-collapse --> 
        </div>
        <!-- /.container-fluid --> 
      </nav>
    </div>
    <script type="text/javascript" src="bootstrap-3.2.0-dist/js/jquery-1.9.1.min.js"></script> 
    <script type="text/javascript" src="bootstrap-3.2.0-dist/js/bootstrap.js"></script> 
    <script type="text/javascript" src="prettify-4-Mar-2013/prettify.js"></script>
    </body>

    </html>

    上面两步简单的工作做好之后,就该进行主页index.php的编写了:

    首先。导入导航栏menu.php

    <?php  include("index.php"); ?>

    然后是加个标题和表格

    <h3>浏览新闻</h3>

    <table class="table table-hover">

    <tr>

    <th>新闻id</th>

    <th>标题</th>

    <th>keyword</th>

    <th>作者</th>

    <th>时间</th>

    <th>内容</th>

    <th>操作</th>

    </tr>

    <?

    php  //这儿有5部分

    //1.导入配置文件

    require("dbconfig.php");

    //2.链接mysql,选择数据库

    $link=@mysql_connect(HOST,USER,PASS) or die("链接数据库出错!");

    mysql_select_db(DBNAME,$link);

    //3.运行查询,返回结果集

    $sql="select * from news order by addtime desc";

    $result=mysql_query($sql,$link);

    //4.解析结果集,并遍历输出

    while($row=mysql_fetch_assoc($result)){

    echo"<tr>";

    echo"<td>{$row['id']}</td>";

    echo"<td>{$row['tilte']}</td>";

    echo"<td>{$row['keywords']}</td>";

    echo"<td>{$row['author']}</td>";

    echo"<td>{$row['addtime']}</td>";

    echo"<td>{$row['content']}</td>";

    echo"<td>

    <a href=‘#’>删除</a>;//此处的“#”仅仅是一个代号,后面会把它替换掉,因为增删操作比較复杂,所以单独写一个action.php文件

    <a href=‘#’>改动</a>;

    </td>";

    echo"</tr>"

    }

    //5.释放结果集

    mysql_free_result(&result);

    musql_close($link);

    ?

    >

    </table>


    action.php:

    <?php 

    //这是一个数据的增删改查的页面

    //1.导入配置文件

    require("dbconfig.php");

    //2.链接mysql。并选择数据库

    $link=@mysql_connect(HOST,USER,PASS) or die("数据库链接失败");

    mysql_select_db(DBNAME,$link);

    //3.依据action的值。来推断所属的操作。运行对应的代码

    switch($_GET["action"]){

    case"add":

    //1.获取要加入的信息,补充其它信息

    $tilte=$_POST["title"];

    $keywords=$_POST["keywords"];

    $author=$_POST["author"];

    $content=$_POST["content"];

    $addtime=time();

    //2.信息的过滤

    //3.拼接sql语句,运行对应的操作

    $sql=insert into news value(null,'($title)','($keywords)','($author)',$addtime,'($content)');

    mysql_query($sql,$link);

    //4.推断是否成功

    $id=mysql_insert_id($link);

    if($id>0){

    echo "<h3>新闻信息加入成功</h3>";

    }

    else{

    echo "<h3>新闻信息加入失败</h3>";

    }

    echo("<a href='javascript:window.history.back()'>返回</a>");

    echo("<a href='index.php'>浏览新闻</a>");

    break;

    case "del":

    //1.获取要删除的新闻id:

    $id=$_GET['id'];

    //2.拼装删除sql语句,运行对应的删除操作

    $sql="delete from news where id=($id)";

    mysql_query($sql,$link);

    //3.删除之后自己主动跳转至新闻浏览界面

    header("location:index.php");

    break;

    case "update":

    //1.获取要改动的信息
    $title = $_POST['title'];
    $keywords = $_POST['keywords'];
    $author = $_POST['author'];
    $content = $_POST['content'];
    $id = $_POST['id'];
    //2.过滤要改动的信息(此处省略)
    //3.拼装改动sql语句,并运行改动操作
    $sql="update news set title='($title)',keywords='($keywords)',author='($author)',content='($content)' where id=($id)";
    //echo $sql;
    mysql_query($sql,$link);
    //4.跳转至浏览界面
    header("location:index.php");

    break;

    }

    //4.关闭数据库链接

    mysql_close("$link");

    ?>


    以下写加入新闻的页面add.php文件:

    <?php include("menu.php");//导入导航栏?

    >
    <h3 align="center">公布新闻</h3>
                    <div class="container">
    <form action="action.php?action=add" method="post">
                        
    <table class="table table-bordered table-hover table-responsive">
    <tr>
    <td align="center">标题</td>
    <td><input  class="col-xs-10" type="text" name="title"></td>
    </tr>
    <tr>
    <td align="center">keyword</td>
    <td><input  class="col-xs-10" type="text" name="keywords"></td>
    </tr>
    <tr>
    <td align="center">作者</td>
    <td><input  class="col-xs-10" type="text" name="author"></td>
    </tr>
    <tr>
    <td valign="top" align="center">内容</td>
    <td><textarea class="col-xs-10" name="content"></textarea></td>
    </tr>
    <tr>
    <td colspan=2 align="center">
    <input class="btn btn-primary" type="submit" value="加入">
    <input class="btn btn-primary" type="reset" value="重置">
    </td>
    </tr>
    </table>
    </form>
                        </div>


    然后是编辑的页面edit.php页面:

    <?

    php include("menu.php");//导入导航栏
    //1.导入配置文件
    require("dbconfig.php");

    //2.连接mysql,选择数据库
    $link=@mysql_connect(HOST,USER,PASS)or die("数据库链接失败");
    mysql_select_db(DBNAME,$link);
    //3.获取要改动的信息的id,而且拼装查看sql语句,运行查询,获取要改动信息
    $sql="select * from news where id={$_GET['id']}";
    $result=mysql_query($sql,$link);
    //4.推断是否获取到了要改动的信息
    if($result && mysql_num_rows($result)>0){
    $news=mysql_fetch_assoc($result);
    }else{
    die("没有找到要改动的信息");
    }

    ?

    >
    <h3 align="center">编辑新闻</h3>
    <form action="action.php?action=update" method="post">
    <input type="hidden" name="id" value="<?php echo $news['id']; ?>">
    <table class="table table-bordered table-hover table-responsive">
    <tr>
    <td  align="center">标题</td>
    <td><input  class="col-xs-10" type="text" name="title" value="<?

    php echo $news['title']; ?>"></td>
    </tr>
    <tr>
    <td  align="center">keyword</td>
    <td><input  class="col-xs-10" type="text" name="keywords" value="<?php echo $news['keywords']; ?>"></td>
    </tr>
    <tr>
    <td  align="center">作者</td>
    <td><input  class="col-xs-10" type="text" name="author" value="<?

    php echo $news['author']; ?

    >"></td>
    </tr>
    <tr>
    <td valign="top"  align="center">内容</td>
    <td><textarea  class="col-xs-10" name="content"><?php echo $news['content']; ?

    ></textarea></td>
    </tr>
    <tr>
    <td colspan=2 align="center">
    <input class="btn btn-primary" type="submit" value="编辑">
    <input class="btn btn-primary" type="reset" value="重置">
    </td>
    </tr>
    </table>
    </form>


    最后。提一下。删除和改动的“#”用什么取代

    此处为了人性化一些,用js代码给出一个提示

    <script type="text/javascript">
    function dodel(id){
    if(confirm("确定要删除吗?")){
    window.location="action.php?

    action=del&id="+id;
    }
    }
    </script>

    第一个“#”,用javascript:dodel({$row["id"]})替代

    第二个“#”。用edit.php?id={$row["id"]}替代

    至此,一个完整的php新闻管理系统就基本完毕了。明天再改进一下。




  • 相关阅读:
    分页精度
    abp zero core 启动vue项目
    swagger 配置错误
    .net core 3.0配置跨域
    .net core 3.0 swagger
    .net core 3.0一个记录request和respose的中间件
    .net Core3.0 +Nlog+Sqlserver
    .net core 3.0+unit of work (一)
    .NetCore 3.0迁移遇到的各种问题
    open xml 导出excel遇到的问题
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6703327.html
Copyright © 2011-2022 走看看