PHP開發 小型論壇教學之論壇版塊

我們從做論壇的第一步開始吧

第一步,從首頁開始:讀取資料庫中的資訊。首頁主要是循環顯示‘forums’表中的所有論壇版塊。對於有基礎的人來說,查詢語句很容易:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mybbs";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_set_charset($conn,'utf8'); //设定字符集
$sql="select * from forums";
$que=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($que)){
    echo "论坛 :".$row['forum_name'];
}
?>

這樣運行,頁面沒有任何輸出,因為我們剛建立的資料庫中沒有任何數據!那麼,我希望讓論壇更人性化,假如沒有論壇版塊應該輸出「對不起,論壇尚在建設中„„」的字樣該怎麼做? ?我們可以用mysql_num_rows()可以得到結果數目,程式碼如下

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mybbs";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
mysqli_set_charset($conn,'utf8'); //设定字符集
$sql="select * from forums";
$que=mysqli_query($conn,$sql);
$sum=mysqli_num_rows($que);
if($sum){
    while($row=mysqli_fetch_array($que)){
        echo "论坛 :".$row['forum_name'];
    }
}else{
    echo "对不起,论坛正在建设中,感谢你的关注......";
}
?>

我們現在來用css的樣式和佈局來讓我們的頁面看起開更美觀一點,程式碼如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>论坛</title>
    <style>
        table{
            width: 55%;
            margin-top: 10px;
        }
        .title{
            background-color: #B10707;
            font-size: 17px;
            color: white;
        }
        .right{
            margin-left: 120px;
        }
    </style>
</head>
<body>
<table border="1px" cellspacing="0" cellpadding="8"align="center">
    <tr class="title">
        <td COLSPAN="3">
            论坛列表<span class="right">[<a style="color: white" href="add_forum.php">添加</a> ]</span>
        </td>
    </tr>
    <tr>
        <td width="10%"><strong>主题</strong></td>
        <td width="40"><strong>论坛</strong></td>
        <td width="15"><strong>最后更新</strong></td>
    </tr>
    <?php
 $sql="select * from forums";
 $que=mysqli_query($conn,$sql);
 $sum=mysqli_num_rows($que);
 if($sum>0) {
 while ($row = mysqli_fetch_array($que)) {
 ?>
 <tr>
                <td><?php echo $row['subject'] ?></td>
                <td><?php echo "<div class=\"bold\"><a class=\"forum\" href=\"forums.php?F=" . $row['id'] . "\">" . $row["forum_name"] . "</a></div>"
 . $row["forum_description"] ?></td>
                <td>
                    <div><?php echo $row["last_post_time"]?></div>
                </td>
            </tr>
            <?php
 }
    }else{
 echo "<tr><td colspan='3'>对不起,论坛正在建设中,感谢你的关注......</td></tr>";
    }
 ?>
</table>
</body>
</html>

現在資料庫中還沒有數據,所以,我們運行首頁,只顯示「對不起,論壇尚在建設中„„」。既然我們很希望看到結果,那麼我們下一步就往資料庫中加幾條資料吧!




#
繼續學習
||
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>论坛</title> <style> table{ width: 55%; margin-top: 10px; } .title{ background-color: #B10707; font-size: 17px; color: white; } .right{ margin-left: 120px; } </style> </head> <body> <table border="1px" cellspacing="0" cellpadding="8"align="center"> <tr class="title"> <td COLSPAN="3"> 论坛列表<span class="right">[<a style="color: white" href="add_forum.php">添加</a> ]</span> </td> </tr> <tr> <td width="10%"><strong>主题</strong></td> <td width="40"><strong>论坛</strong></td> <td width="15"><strong>最后更新</strong></td> </tr> <?php $sql="select * from forums"; $que=mysqli_query($conn,$sql); $sum=mysqli_num_rows($que); if($sum>0) { while ($row = mysqli_fetch_array($que)) { ?> <tr> <td><?php echo $row['subject'] ?></td> <td><?php echo "<div class=\"bold\"><a class=\"forum\" href=\"forums.php?F=" . $row['id'] . "\">" . $row["forum_name"] . "</a></div>" . $row["forum_description"] ?></td> <td> <div><?php echo $row["last_post_time"]?></div> </td> </tr> <?php } }else{ echo "<tr><td colspan='3'>对不起,论坛正在建设中,感谢你的关注......</td></tr>"; } ?> </table> </body> </html>
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!