PHP開發(fā)之留言板展示頁面
我們?nèi)绻胱屛覀儼l(fā)布的留言跟首頁展示在一起,就需要利用我們的HTML和PHP語言混合運用,看下面的代碼
我們已經(jīng)從HTML頁面發(fā)布了留言成功,說明數(shù)據(jù)已經(jīng)成功存入了數(shù)據(jù)庫,我們只需要從數(shù)據(jù)庫把這些數(shù)據(jù)讀取出來,代碼如下
<?php
session_start();
header("content-type:text/html;charset=utf-8");
$conn=mysqli_connect("localhost","root","root","Ressage");
mysqli_set_charset($conn,'utf8');?//設(shè)定字符集
$sql=?"select?*?from?ressage_user;
$result=mysqli_query($conn,$sql);
?>
session_start();
header("content-type:text/html;charset=utf-8");
$conn=mysqli_connect("localhost","root","root","Ressage");
mysqli_set_charset($conn,'utf8');?//設(shè)定字符集
$sql=?"select?*?from?ressage_user;
$result=mysqli_query($conn,$sql);
?>
上面的兩行代碼就可以讓我們從數(shù)據(jù)庫把數(shù)據(jù)查詢出來,
難的是怎么讓它在html頁面按指定的格式展現(xiàn)出來,看下面的代碼
<p> <? if($result==null){ echo"暫時沒有留言"; } ?> </p> <?php while($row=mysqli_fetch_array($result)){ ?> <table border="1" cellspacing="0"> <tr> <td>姓名:<?php echo $row['name']?></td> <td style="text-align: center">留言時間:<?php echo $row['ressage_time']?></td> <td><a href="ressage_delete.php?id=<?php echo $row['id']?>" >刪除</a> </td> </tr> <tr> <td colspan="3">你的留言:<?php echo $row['content']?></td> </tr> </table> <?php }?>
上面的代碼就是我們的HTML和PHP的混編,我們首先把我們數(shù)據(jù)庫里的數(shù)據(jù)以數(shù)組的形式讀取出來,然后我們在取相應(yīng)的字段,循環(huán)出來。把上面的代碼組合起來就可以把我們的數(shù)據(jù)讀取展示出來,但是我們還要將這些代碼跟我們的首頁代碼放到一起,所以,這些代碼還需要進一步的組合,看下一章節(jié)