我需要一些帮助来弄清楚这个的逻辑,因为在我看到的大多数教程中,他们只需要在数据库中存储topics_count int,并且每次在该特定论坛中创建主题时他们都会增加topics_count为他们创建的论坛+1;
但我们是否可以通过使用mysqli_num_rows
获取值来实现此目的?这将返回与论坛相关的总行数(主题)。但问题是当论坛在数组中时,我怎样才能创建这种关系。
论坛和主题都有:
我知道安全问题。这个例子只是一种做法。
这就是我如何从数据库中显示论坛并将href回显到每个论坛。
$array_forums = mysqli_fetch_assoc($res_forums);
echo "<div id='forum_{$array_forums["forum_id"]}' class='forum_classic'>";
echo "<div id='forum_classic_status'></div>";
echo "<div id='forum_classic_container'>";
echo "<div id='forum_classic_icon'></div>";
echo "<div id='forum_classic_description'>";
echo "<a href=forum.php?forum=" . urlencode($array_forums["forum_id"]) . ">";
echo "<h1>{$array_forums["forum_title"]}</h1>";
echo "</a>";
echo "<p>{$array_forums["forum_description"]}</p>";
echo "</div>";
echo "</div>";
echo "<div id='forum_classic_topics'>Topics</div>";
echo "<div id='forum_classic_replies'>Replies</div>";
echo "<div id='forum_classic_lastposter'>Lastposter</div>";
echo "</div>";
}
这是我在点击链接后在每个论坛中显示主题的方式。
$forum_id = $_GET['forum'];
//1. Perform database query for topics
$sql_topic = "SELECT * FROM topics WHERE topic_forum_id = {$forum_id}";
$res_topic = mysqli_query($conn, $sql_topic);
//2. Display returned data from topics
if (mysqli_num_rows($res_topic) >= 1) {
$array_topic = mysqli_fetch_assoc($res_topic);
echo "<div id='topic_{$array_topic["topic_id"]}'>{$array_topic["topic_title"]}";
echo "</div>";
} else {
echo "<a href='community.php'>Return To Forum Index</a>";
echo "<p>There are no topics in this forum yet.</p>";
}
答案 0 :(得分:1)
您可以使用count来计算行数
$sql_topic = "SELECT count(*) FROM topics WHERE topic_forum_id = {$forum_id}";
$row = mysqli_fetch_row($sql_topic);
$num = $row[0];
$ num将返回特定论坛
中的主题数量答案 1 :(得分:1)
在@Anil Shrestha的帮助下,$ num将作为主题计数器。
$sql_topic = "SELECT count(*) FROM topics WHERE topic_forum_id = {$array_forums["forum_id"]}";
$res_topic = mysqli_query($conn, $sql_topic);
$row = mysqli_fetch_row($res_topic);
$num = $row[0];