gpt4 book ai didi

php - 为什么这只返回第一个结果?

转载 作者:行者123 更新时间:2023-11-29 22:27:30 25 4
gpt4 key购买 nike

我正在创建一个论坛,目前正在尝试显示首页(包含所有部分、论坛和最新主题)。由于某种原因,下面的代码仅返回第一个结果。有什么帮助吗?

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):

mysql database

仅发布forum_cats表,因为它是唯一有问题的部分。

最佳答案

您正在使用%s,所以您可能想要

$cats = "SELECT * FROM forum_cats WHERE Parent LIKE'%s' ORDER BY pos ASC";

这将返回所有以字母 s 结尾的结果

parent = '%s' 将返回 parent 值完全等于 %s 的行

关于php - 为什么这只返回第一个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com