PHP實(shí)現(xiàn)簡易blog的制作
最近,有時(shí)間看了點(diǎn)PHP的代碼。參考PHP100教程做了簡單的blog,這里面簡單的記錄一下。
首先是集成環(huán)境,這里選用的WAMP:http://www.wampserver.com/en/
首先通過,phpMyAdmin創(chuàng)建一張blog表。

純界面操作,過程比較簡單,需要注意的是id是主鍵,并且設(shè)置auto_increnent 選項(xiàng),表示該字段為空時(shí)自增。其它字段就比較隨便了,注意類型和長度即可。
創(chuàng)建數(shù)據(jù)連接
在./wamp/www/blog目錄下創(chuàng)建conn.php文件。
<?php
@mysql_connect("127.0.0.1:3306","root","") or die("mysql數(shù)據(jù)庫連接失敗");
@mysql_select_db("test")or die("db連接失敗");
mysql_query("set names 'gbk'");
?>
mysql默認(rèn)用戶名為root,密碼為空,這里創(chuàng)建的blog在test庫中,所以需要連接test庫。
添加blog
在./wamp/www/blog/目錄下創(chuàng)建add.php文件。
<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
include("conn.php"); //引入連接數(shù)據(jù)庫
if (!empty($_POST['sub'])) {
$title = $_POST['title']; //獲取title表單內(nèi)容
$con = $_POST['con']; //獲取contents表單內(nèi)容
$sql= "insert into blog values(null,'0','$title',now(),'$con')";
mysql_query($sql);
echo "insert success!";
}
?>
<form action="add.php" method="post">
title :<br>
<input type="text" name="title"><br><br>
contents:<br>
<textarea rows="5" cols="50" name="con"></textarea><br><br>
<input type="submit" name="sub" value="submit">
</form>
這段代碼分兩部分,上部分是PHP代碼,include (或 require)語句會(huì)獲取指定文件中存在的所有文本/代碼/標(biāo)記,并復(fù)制到使用 include 語句的文件中。
然后,判斷表單中name='sub'的內(nèi)容不為空的情況下,將獲取表單的內(nèi)容,然后執(zhí)行$sql 語句,null 表示id為空(自增),now()表示取當(dāng)前日起,$title和$con取表單中用戶提交的內(nèi)容。最后eche 插入成功的提示。
下半部分就是一段簡單的HTML代碼了,用于實(shí)現(xiàn)一個(gè)可以blog表單提交的功能。
創(chuàng)建blog的首頁
在./wamp/www/blog/目錄下創(chuàng)建index.php文件。
<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<br><br>
<form action="" method="get" style='align:"right"'>
<input type="text" name="keys" >
<input type="submit" name="subs" >
</form>
<hr>
<?php
include("conn.php"); //引入連接數(shù)據(jù)庫
if (!empty($_GET['keys'])) {
$key = $_GET['keys'];
$w = " title like '%$key%'";
}else{
$w=1;
}
$sql ="select * from blog where $w order by id desc limit 5";
$query = mysql_query($sql);
while ($rs = mysql_fetch_array($query)) {
?>
<h2>title: <a href="view.php?id=<?php echo $rs['id']; ?>"><?php echo $rs['title']; ?></a>
| <a href="edit.php?id=<?php echo $rs['id']; ?>">edit</a>
| <a href="del.php?id=<?php echo $rs['id']; ?>">delete</a> |
</h2>
<li>date: <?php echo $rs['data']; ?></li>
<!--截取內(nèi)容展示長度-->
<p>contents:<?php echo iconv_substr($rs['contents'],0,30,"gbk"); ?>...</p>
<hr>
<?php
};
?>
該頁面包含有的功能還是比較多的。
首先是一個(gè)搜索表單,通過if判斷搜索表單的內(nèi)容是否為空,如果不為空,通過輸入關(guān)鍵字匹配文章的標(biāo)題并顯示結(jié)果;如果為空查詢所有blog內(nèi)容,并循環(huán)顯示每一篇文章的標(biāo)題、日期、正文。點(diǎn)擊標(biāo)題會(huì)鏈接到該篇blog的詳細(xì)頁面。每一篇文章提供“編輯”和“刪除”功能。
mysql_query()用于執(zhí)行sql語句。mysql_fetch_arry()將返回的數(shù)據(jù)生成數(shù)組,這樣就可以像操作數(shù)組一樣,操作數(shù)據(jù)庫中的每一條數(shù)據(jù)了。
然后是正文的顯示,通過 iconv_substr() 函數(shù)提取正文前30個(gè)字符。
查看blog
在./wamp/www/blog/目錄下創(chuàng)建view.php文件。
<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
include("conn.php"); //引入連接數(shù)據(jù)庫
if (!empty($_GET['id'])) {
$id = $_GET['id'];
$sql ="select * from blog where id='$id' ";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
$sqlup = "update blog set hits=hits+1 where id='$id'";
mysql_query($sqlup);
}
?>
<h2>title: <?php echo $rs['title']; ?> </h1>
<h3>date: <?php echo $rs['data']; ?>
click number: <?php echo $rs['hits']; ?></h3>
<hr>
<p>contents:<?php echo $rs['contents']; ?></p>
blog的正文實(shí)現(xiàn)比較簡單,通過get請(qǐng)求獲取blog的id,然后通過sql語句將該id對(duì)應(yīng)的標(biāo)題、日期和正文查詢出來并顯示。
并外一個(gè)小功能是顯示了一個(gè)簡單的計(jì)數(shù)器,每刷新頁面,點(diǎn)擊數(shù)加1。
編輯blog
在./wamp/www/blog/目錄下創(chuàng)建edit.php文件。
<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
include("conn.php"); //引入連接數(shù)據(jù)庫
//獲取數(shù)據(jù)庫表數(shù)據(jù)
if (!empty($_GET['id'])) {
$edit = $_GET['id'];
$sql = "select * from blog where id='$edit'";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
}
//更新數(shù)據(jù)庫表數(shù)據(jù)
if (!empty($_POST['sub'])) {
$title = $_POST['title']; //獲取title表單內(nèi)容
$con = $_POST['con']; //獲取contents表單內(nèi)容
$hid = $_POST['hid'];
$sql= "update blog set title='$title', contents='$con' where id='$hid' ";
mysql_query($sql);
echo "<script>alert('update success.');location.href='index.php'</script>";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $rs['id'];?>">
title :<br>
<input type="text" name="title" value="<?php echo $rs['title'];?>">
<br><br>
contents:<br>
<textarea rows="5" cols="50" name="con" ><?php echo $rs['contents'];?></textarea><br><br>
<input type="submit" name="sub" value="submit">
</form>
編輯blog的功能相對(duì)復(fù)雜一些。分兩部操作,第一步先將blog的標(biāo)題和正文查詢出來,并顯示到輸入框。第二步將編輯好的內(nèi)容再更新到數(shù)據(jù)庫中。
刪除blog
在./wamp/www/blog/目錄下創(chuàng)建del.php文件。
<a href="index.php"><B>index</B></a>
<a href="add.php"><B>add blog</B></a>
<hr>
<?php
include("conn.php"); //引入連接數(shù)據(jù)庫
if (!empty($_GET['id'])) {
$del = $_GET['id']; //刪除blog
$sql= "delete from blog where id='$del' ";
mysql_query($sql);
echo "delete success!";
}
?>
最后是實(shí)現(xiàn)blog的刪除功能,通過id將該條blog的查詢出來并顯示。
因?yàn)樗许撁鏇]有使用前端樣式有美化,很丑就不貼圖了。功能還算完美。在此記錄,算做PHP學(xué)習(xí)的整理。
=======================================================
另外,雖然每個(gè)語言都有優(yōu)缺點(diǎn),這里還是忍不住要吐槽一下PHP的兩個(gè)不好之處。
1、符號(hào)不好寫, “$” 、“ ->” 、 “=>”。這些符號(hào)雖然并沒有增加代碼語法的理解難度。但敲起來具惡心。每次在打“$”符號(hào)的時(shí)候,都要眼看鍵盤按著shift鍵找4在哪兒。
2、php與html的混編在我看來也不是太優(yōu)雅。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
php的memcache類分享(memcache隊(duì)列)
這篇文章主要介紹了php的memcache類的使用方法(memcache隊(duì)列),需要的朋友可以參考下2014-03-03
PHP獲取當(dāng)前時(shí)間的5種實(shí)現(xiàn)方式
這篇文章主要介紹了PHP獲取當(dāng)前時(shí)間的5種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
laravel中Redis隊(duì)列監(jiān)聽中斷的分析
這篇文章主要給大家介紹了關(guān)于laravel中Redis隊(duì)列監(jiān)聽中斷的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Laravel自動(dòng)生成UUID,從建表到使用詳解
今天小編就為大家分享一篇Laravel自動(dòng)生成UUID,從建表到使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP使用CURL獲取302跳轉(zhuǎn)后的地址實(shí)例
這篇文章主要介紹了PHP使用CURL獲取302跳轉(zhuǎn)后的地址實(shí)例,需要的朋友可以參考下2014-05-05
PHP Curl模擬登錄微信公眾平臺(tái)、新浪微博實(shí)例代碼
這篇文章主要介紹了PHP Curl模擬登錄微信公眾平臺(tái)、新浪微博實(shí)例代碼的相關(guān)資料,涉及到php curl模擬登錄相關(guān)知識(shí),需要的朋友可以參考下2016-01-01

