PHP开发 小型论坛教程之论... 登录

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>
提交 重置代码
章节 评论 笔记 课件
  • 取消 回复 发送
  • 取消 发布笔记 发送