mysql中多个表的嵌套foreach循环查询的问题

发布于 2024-11-17 17:26:14 字数 2913 浏览 0 评论 0原文

我刚刚开始使用 php 和 mysql。我的数据库有两个查询。第一个查询名为生活方式的表,然后执行 foreach 循环并输出其行上的每个项目。现在,每一行都有评论框,供成员发表评论。评论表有一个名为 postid 的关联列,用于表示生活方式行。因此,我在前一个循环中执行了另一个 foreach 循环,以在生活方式的每一行下方显示这些评论。 问题在于,每个评论都会被重复每个生活方式查询的次数。 请参阅下面:

Title:  TEST

Date:   28th-Jun-2011 at 10:10 PM

TEST LIFESTYLE POST

Comments

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

和代码:

<?php

        $lifestyles=dbAll("select * from user_accounts, lifestyle where lifestyle.category=1 and lifestyle.user_id=user_accounts.id order by cdate DESC");

        if(!$lifestyles){
            echo '<em><strong>No Posts yet. Why don\'t you be the first!</strong></em>';
        }
        else{

            foreach($lifestyles as $lifestyle){
                $lifestyleid=(int)$lifestyle['id'];
                echo '<form class="postform" method="post" action="shopping.php?redirect='.$_SERVER["PHP_SELF"].'">';
                echo '<input type="hidden" name="postshop" value="1" />';
                echo '<input type="hidden" name="postid" value="'.$lifestyleid.'" />';
                echo '<table class="postinfo"><tr><th>Title:</th><td style="font-weight:bold">'.$lifestyle['title'].'</td>
                </tr>';
                echo '<th>Date:</th><td>'.date('dS-M-Y', strtotime($lifestyle['cdate'])).' at'.' '.date('g:h A', strtotime($lifestyle['cdate'])).'</td>
                <th>By:</th><td>'.$lifestyle['firstname'].' '.$lifestyle['lastname'].'</td>';
                echo '</table>';

                echo '<div class="postbody"><table>';
                echo '<tr><td>'.$lifestyle['body'].'</td><td></td></tr>'.'</table></div>';

                $comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");

                echo '<div class="comment">';
                echo '<table><tr><th>Comments</th></tr>';

                **foreach($comments as $comment)
                {
                    echo '<tr><td>'.$comment['commentbody'].'</td></tr>';
                }**

                echo '<tr><td><textarea rows="1" title="Write a comment" name="comment" placeholder="Write a comment..."></textarea></td></tr>';
                echo '</table></div>';
                echo '<input type="submit" name="action" value="Save Comment" />';
                echo '<hr />';
                echo '</form>';
            }

        }
    ?>

我已经尝试了一整天,请帮忙。

I am just begining php and mysql. I have two queries from my database. First one queries a table called lifestyles then does a foreach loop and outputs each item on its row. Now each of these rows have commenting boxes for members to post comments about them. the comments table has associative column called postid for the lifestyle rows. So, I did another foreach loop inside the previous one to display these comments below each row of lifestyle.
The problem is that each comment is replicated the number of times of each lifestyle query.
See below:

Title:  TEST

Date:   28th-Jun-2011 at 10:10 PM

TEST LIFESTYLE POST

Comments

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

TEST COMMENT

and the code:

<?php

        $lifestyles=dbAll("select * from user_accounts, lifestyle where lifestyle.category=1 and lifestyle.user_id=user_accounts.id order by cdate DESC");

        if(!$lifestyles){
            echo '<em><strong>No Posts yet. Why don\'t you be the first!</strong></em>';
        }
        else{

            foreach($lifestyles as $lifestyle){
                $lifestyleid=(int)$lifestyle['id'];
                echo '<form class="postform" method="post" action="shopping.php?redirect='.$_SERVER["PHP_SELF"].'">';
                echo '<input type="hidden" name="postshop" value="1" />';
                echo '<input type="hidden" name="postid" value="'.$lifestyleid.'" />';
                echo '<table class="postinfo"><tr><th>Title:</th><td style="font-weight:bold">'.$lifestyle['title'].'</td>
                </tr>';
                echo '<th>Date:</th><td>'.date('dS-M-Y', strtotime($lifestyle['cdate'])).' at'.' '.date('g:h A', strtotime($lifestyle['cdate'])).'</td>
                <th>By:</th><td>'.$lifestyle['firstname'].' '.$lifestyle['lastname'].'</td>';
                echo '</table>';

                echo '<div class="postbody"><table>';
                echo '<tr><td>'.$lifestyle['body'].'</td><td></td></tr>'.'</table></div>';

                $comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");

                echo '<div class="comment">';
                echo '<table><tr><th>Comments</th></tr>';

                **foreach($comments as $comment)
                {
                    echo '<tr><td>'.$comment['commentbody'].'</td></tr>';
                }**

                echo '<tr><td><textarea rows="1" title="Write a comment" name="comment" placeholder="Write a comment..."></textarea></td></tr>';
                echo '</table></div>';
                echo '<input type="submit" name="action" value="Save Comment" />';
                echo '<hr />';
                echo '</form>';
            }

        }
    ?>

I have been trying all day, please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光与爱终年不遇 2024-11-24 17:26:14

问题是这一行的查询:

$comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");

您没有将注释表连接到生活方式表。
尝试添加联接,将查询更改为:

select *
from comments,lifestyle
where comments.postid=$lifestyleid
and section_id=1
---->>
AND comments.postid = lifestyle.id
order by commentdate DESC

The problem is the query on this line:

$comments=dbAll("select * from comments,lifestyle where comments.postid=$lifestyleid and section_id=1 order by commentdate DESC");

You aren't joining the comments table to the lifestyle table.
Try adding the join, changing the query to:

select *
from comments,lifestyle
where comments.postid=$lifestyleid
and section_id=1
---->>
AND comments.postid = lifestyle.id
order by commentdate DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文