mysql中多个表的嵌套foreach循环查询的问题
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是这一行的查询:
您没有将注释表连接到生活方式表。
尝试添加联接,将查询更改为:
The problem is the query on this line:
You aren't joining the comments table to the lifestyle table.
Try adding the join, changing the query to: