《PHP學(xué)習(xí):PHP實(shí)現(xiàn)簡(jiǎn)易blog的制作》要點(diǎn):
本文介紹了PHP學(xué)習(xí):PHP實(shí)現(xiàn)簡(jiǎn)易blog的制作,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
最近,有時(shí)間看了點(diǎn)PHP的代碼.參考PHP100教程做了簡(jiǎn)單的blog,這里面簡(jiǎn)單的記錄一下.
PHP實(shí)例
首先是集成環(huán)境,這里選用的WAMP:http://www.wampserver.com/en/
PHP實(shí)例
首先通過,phpMyAdmin創(chuàng)建一張blog表.
PHP實(shí)例
PHP實(shí)例
純界面操作,過程比較簡(jiǎn)單,需要注意的是id是主鍵,并且設(shè)置auto_increnent 選項(xiàng),表示該字段為空時(shí)自增.其它字段就比較隨便了,注意類型和長(zhǎng)度即可.
PHP實(shí)例
創(chuàng)建數(shù)據(jù)連接????
PHP實(shí)例
在./wamp/www/blog目錄下創(chuàng)建conn.php文件.
PHP實(shí)例
<?php @mysql_connect("127.0.0.1:3306","root","") or die("mysql數(shù)據(jù)庫(kù)連接失敗"); @mysql_select_db("test")or die("db連接失敗"); mysql_query("set names 'gbk'"); ?>
mysql默認(rèn)用戶名為root,密碼為空,這里創(chuàng)建的blog在test庫(kù)中,所以需要連接test庫(kù).
PHP實(shí)例
添加blog?????????????????????????
PHP實(shí)例
在./wamp/www/blog/目錄下創(chuàng)建add.php文件.
PHP實(shí)例
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入連接數(shù)據(jù)庫(kù) 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 語句的文件中.PHP實(shí)例
然后,判斷表單中name='sub'的內(nèi)容不為空的情況下,將獲取表單的內(nèi)容,然后執(zhí)行$sql 語句,null 表示id為空(自增),now()表示取當(dāng)前日起,$title和$con取表單中用戶提交的內(nèi)容.最后eche 插入成功的提示.PHP實(shí)例
下半部分就是一段簡(jiǎn)單的HTML代碼了,用于實(shí)現(xiàn)一個(gè)可以blog表單提交的功能.PHP實(shí)例
創(chuàng)建blog的首頁(yè)?????????????????????????PHP實(shí)例
在./wamp/www/blog/目錄下創(chuàng)建index.php文件.
PHP實(shí)例
<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ù)庫(kù) 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)容展示長(zhǎng)度--> <p>contents:<?php echo iconv_substr($rs['contents'],0,30,"gbk"); ?>...</p> <hr> <?php }; ?>
該頁(yè)面包含有的功能還是比較多的.PHP實(shí)例
首先是一個(gè)搜索表單,通過if判斷搜索表單的內(nèi)容是否為空,如果不為空,通過輸入關(guān)鍵字匹配文章的標(biāo)題并顯示結(jié)果;如果為空查詢所有blog內(nèi)容,并循環(huán)顯示每一篇文章的標(biāo)題、日期、正文.點(diǎn)擊標(biāo)題會(huì)鏈接到該篇blog的詳細(xì)頁(yè)面.每一篇文章提供“編輯”和“刪除”功能.PHP實(shí)例
mysql_query()用于執(zhí)行sql語句.mysql_fetch_arry()將返回的數(shù)據(jù)生成數(shù)組,這樣就可以像操作數(shù)組一樣,操作數(shù)據(jù)庫(kù)中的每一條數(shù)據(jù)了.PHP實(shí)例
然后是正文的顯示,通過 iconv_substr() 函數(shù)提取正文前30個(gè)字符.PHP實(shí)例
查看blog????????????????????????????????????????????????????????????????????????????????????????????????????????????PHP實(shí)例
在./wamp/www/blog/目錄下創(chuàng)建view.php文件.
PHP實(shí)例
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入連接數(shù)據(jù)庫(kù) 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)比較簡(jiǎn)單,通過get請(qǐng)求獲取blog的id,然后通過sql語句將該id對(duì)應(yīng)的標(biāo)題、日期和正文查詢出來并顯示.
并外一個(gè)小功能是顯示了一個(gè)簡(jiǎn)單的計(jì)數(shù)器,每刷新頁(yè)面,點(diǎn)擊數(shù)加1.PHP實(shí)例
編輯blog????????????????????????????????????????????????????????????????????????????????????????????????????????PHP實(shí)例
在./wamp/www/blog/目錄下創(chuàng)建edit.php文件.
PHP實(shí)例
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入連接數(shù)據(jù)庫(kù) //獲取數(shù)據(jù)庫(kù)表數(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ù)庫(kù)表數(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ù)庫(kù)中.
PHP實(shí)例
刪除blog???
PHP實(shí)例
在./wamp/www/blog/目錄下創(chuàng)建del.php文件.
PHP實(shí)例
<a href="index.php"><B>index</B></a> <a href="add.php"><B>add blog</B></a> <hr> <?php include("conn.php"); //引入連接數(shù)據(jù)庫(kù) 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的查詢出來并顯示.PHP實(shí)例
因?yàn)樗许?yè)面沒有使用前端樣式有美化,很丑就不貼圖了.功能還算完美.在此記錄,算做PHP學(xué)習(xí)的整理.PHP實(shí)例
=======================================================PHP實(shí)例
另外,雖然每個(gè)語言都有優(yōu)缺點(diǎn),這里還是忍不住要吐槽一下PHP的兩個(gè)不好之處.PHP實(shí)例
1、符號(hào)不好寫, “$” 、“ ->” 、 “=>”.這些符號(hào)雖然并沒有增加代碼語法的理解難度.但敲起來具惡心.每次在打“$”符號(hào)的時(shí)候,都要眼看鍵盤按著shift鍵找4在哪兒.PHP實(shí)例
2、php與html的混編在我看來也不是太優(yōu)雅.PHP實(shí)例
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.PHP實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/2956.html