PHP實戰 新聞管理系統 使用到了bootstrap架構,bootstrap實戰

來源:互聯網
上載者:User

PHP實戰 新聞管理系統 使用到了bootstrap架構,bootstrap實戰

剛剛接觸 PHP 仿照視頻 寫了個新聞管理系統 其中也使用到了bootstrap架構

寫下來整理一下思路。

這是個很簡單的系統,首先是建立資料庫表。

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");//使用者名稱

define(PASS,"");//密碼

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-width: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>關鍵字</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">關鍵字</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">關鍵字</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新聞管理系統就基本完成了,明天再改進一下。





有沒好用,又簡單的cms 新聞管理系統? php的

php,dede的都可以,還有推薦一個08cms的,但這個比較自由專業複雜,這兩年新出來的,可能不大適合你,不過也可以看看。
 
一個極精簡的PHP新聞管理系統CMS

kingcms
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.