while 循环内的 while 循环并输出 php?
我有一个 while 循环来显示我网站上帖子的回复。
查询中使用的parentID 值是$post['postID'],它是正在查看的帖子的详细信息数组。
如下所示,它输出以下内容(每个主题都是查看完整帖子的链接),
$q = $dbc -> prepare("SELECT * FROM boardposts WHERE parentID = ?");
$q -> execute(array($post['postID']));
while ($postReply = $q -> fetch(PDO::FETCH_ASSOC)) {
echo '<p><a href="http://www.example.com/boards?topic=' . $_GET['topic'] . '&view=' . $postReply['postID'] . '">' . $postReply['subject'] . '</a>';
}
当前输出的内容类似于
对此消息的回复:
主题 1
主题 2
主题 3
科目 4
有没有一种方法可以让我在列表中包含对回复的回复,类似于
对此消息的回复:
主题 1
主题1回复
主题1回复
主题1回复回复
主题 2
主题 3
主题3回复
主题3回复
主题3回复回复
主题 4
主题4回复
科目 5
科目 6
主题6回复
主题4回复回复
我知道所有的缩进都可以用css,但是我不知道如何从mysql数据库中提取数据并以正确的顺序,我尝试了while循环内的while循环,但这涉及while循环内的查询,这很糟糕!
感谢您的投入!
I have a while loop to show the replies for a post on my website.
The value for parentID used in the query is $post['postID'] which is an array of details for the post being viewed.
As seen below it outputs the following (each subject is a link to view the full post)
$q = $dbc -> prepare("SELECT * FROM boardposts WHERE parentID = ?");
$q -> execute(array($post['postID']));
while ($postReply = $q -> fetch(PDO::FETCH_ASSOC)) {
echo '<p><a href="http://www.example.com/boards?topic=' . $_GET['topic'] . '&view=' . $postReply['postID'] . '">' . $postReply['subject'] . '</a>';
}
This currently outputs something along the lines of,
Replies To This Message:
subject 1
subject 2
subject 3
subject 4
Is there a way in which I can also in the list include replies to the replies, something along the lines of,
Replies To This Message:
subject 1
subject 1 reply
subject 1 reply
subject 1 reply reply
subject 2
subject 3
subject 3 reply
subject 3 reply
subject 3 reply reply
subject 4
subject 4 reply
subject 5
subject 6
subject 6 reply
subject 4 reply reply
I understand all the indenting can be with css, but am stuck as to how to pull the data from the mysql database and in the correct order, I tried while loops within while loops, but that involved queries inside while loops, which is bad!
Thanks for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑您要做的是重新设计数据库以将线程/主题关系存储在一个字段中,以便您可以在一个查询中提取所有线程,然后在 php 中以编程方式对它们进行排序。如果您有一个parent_id字段,并且每个回复行都知道其父级是谁以及其顺序是什么(大概顺序只是id>前一个id),那么根据需要组织它们就非常简单了。
然后,您可以遍历响应列表并将所有项目放入 JSON(y) 对象中,该对象具有所有嵌套响应的正确顺序/关系。
为了减少不必要的膨胀,您可能还需要存储顶级父 ID,以便您可以轻松地仅拉取属于特定顶级线程的那些线程。这样,您可以轻松地仅获取属于最后 20 个顶级线程的线程(以便减少对无人需要的古老线程进行不必要的计算,并构建某种分页)。
编辑...一些伪代码...
select * from row order by top_level_id desc limit 0,20
// 这将为您提供最多 20 个顶级线程,因此您不会拉动整个数据库。末尾的 0 应该是您从 PHP 传入的页面变量,因此您可以为第 2 页执行 20,20,为第 3 页执行 40,20现在您执行类似的操作(显然这是伪代码,但您应该能够根据这个模型完成它......
I suspect what you're going to have to do is redesign your database to store the thread/subject relationship in a field so that you can pull all the threads in one query and then sort them programmatically in php. If you have a parent_id field and each reply row knows who its parent is and what its order is (presumably the order is simply id > previous id) it's pretty simple to then organize them as you need them.
You can then walk your response list and throw all the items into JSON(y) Object that has all your nested responses in their correct order/relationship.
To reduce unwanted bloat, you may want to store the top level parent ID as well, so that you can easily pull only those threads that belong to specific top level threads. That way you can easily grab only the threads that belong to, for instance, the last 20 top level threads (so as to reduce unnecessary computing on ancient threads that no one needs, and to build some sort of pagification).
EDIT... some pseudo code...
select * from row order by top_level_id desc limit 0,20
// this will give you a max of 20 top level threads, so you're not pulling the entire db. The 0 at the end should be a page variable you pass in from your PHP, so you can do 20,20 for page 2 and 40,20 for page 3now you do something like this (obviously this is pseudo code, but you should be able to get it done based on this model...