PHP development...LOGIN

PHP development small forum tutorial forum section

Let’s start with the first step of building a forum

The first step is to start from the homepage: read the information in the database. The main page is to cycle through all forum sections in the 'forums' table. For those with basic knowledge, the query statement is very easy:

<?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'];
}
?>

When running like this, the page does not have any output, because the database we just created No data! So, I hope to make the forum more user-friendly. What if there is no forum section and the words "Sorry, the forum is still under construction..." should be output? ? We can use mysql_num_rows() to get the number of results. The code is as follows

<?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 "对不起,论坛正在建设中,感谢你的关注......";
}
?>

Now let’s use css style and layout to make our page look more beautiful. The code is as follows

<!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>

There is no data in the database yet, so when we run the homepage, it only displays "Sorry, the forum is still under construction...". Since we really want to see the results, let's add a few pieces of data to the database next!



Next Section

<!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>
submitReset Code
ChapterCourseware