我正在创建一个论坛,我正在尝试显示首页(包含所有部分,论坛和最新主题)。 出于某种原因,使用下面的代码,它只返回第一个结果。有什么帮助吗?
PHP:
<?php
$sects = "SELECT * FROM forum_cats WHERE parent=0 ORDER BY pos ASC";
$cats = "SELECT * FROM forum_cats WHERE `parent`='%s' ORDER BY pos ASC"; //PARENT = SECT
$threads = "SELECT count(*) AS total FROM forum_posts WHERE `parent`='%s'"; //PARENT = FORUM
$lastthread = "SELECT * FROM forum_posts WHERE `parent`='%s' AND `type`='thread' ORDER BY `posted` DESC LIMIT 1";
// --
$sects = $db->query($sects);
while($sect = $sects->fetch_assoc()):
$section = $sect["id"];
?>
<table class="forum-section">
<thead>
<tr>
<th colspan="6" class="forum-sec-title"><a title="<?php echo $sect["description"]; ?>" href="#forum/<?php echo $section; ?>"><?php echo $sect["title"]; ?></a></th>
</tr>
</thead>
<tbody>
<tr class="forum-cats">
<th class="forum-cat-icon"><!-- Icon --></th>
<th class="forum-cat-forum">Forum</th>
<th class="forum-cat-threads">Threads</th>
<th class="forum-cat-lastpost">Last Post</th>
</tr>
<?php
$cats = $db->query(sprintf($cats, $section));
while ($cat = $cats->fetch_assoc()):
$threads = $db->query(sprintf($threads, $cat['id']));
$threads = $threads->fetch_assoc();
$threads = $threads['total'];
$lastthread = $db->query(sprintf($lastthread, $cat['id']));
$lastthread = $lastthread->fetch_assoc();
?>
<tr>
<td class="forum-icon"><i class="fa fa-pencil"></i></td>
<td><span class="forum-title"><a href="#forum/<?php echo $cat['id']; ?>"><?php echo $cat['title']; ?></a></span><span class="forum-desc"><?php echo $cat['description']; ?></span></td>
<td class="forum-threads"><?php echo $threads; ?></td>
<?php if ($threads != null) : ?>
<td class="forum-lastinfo">
<span class="forum-lastinfo-name"><a href="#forum/post/<?php echo $lastthread['id']; ?>"><?php echo $lastthread['title']; ?></a></span>
<span class="forum-lastinfo-date" title="<?php echo $lastthread['posted']; ?>"><?php echo time_elapsed_string($lastthread['posted']); ?></span>
<span class="forum-lastinfo-user">by <a href="#user/<?php echo $lastthread['author']; ?>">User</a></span>
</td>
<?php else: ?>
<td class="forum-lastinfo">
<span class="forum-lastinfo-name">No posts</span>
</td>
<?php endif; ?>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php endwhile; ?>
MySQL数据库(forum_cats):
仅发布forum_cats表,因为它是问题的唯一部分。
答案 0 :(得分:0)
您正在使用%s
所以您可能需要
$cats = "SELECT * FROM forum_cats WHERE parent LIKE'%s' ORDER BY pos ASC";
这将返回以字母s
虽然parent = '%s'
将返回parent
值完全等于%s