开发者

Problems with Nested foreach loop query from multiple tables in mysql

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

T开发者_StackOverflow中文版EST 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜