Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:24:10+00:00 2026-05-23T10:24:10+00:00

I am just begining php and mysql. I have two queries from my database.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:24:10+00:00Added an answer on May 23, 2026 at 10:24 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a list of dates that is drawn from a MySQL database. That
I know PHP and am just beginning with MySql (but plan to use ODBC).
I'm just curious as I am beginning to learn PHP and MySQL, as to
I'm a new guy in PHP OOP concepts. One of the first things that
Just a quick one - I wrote a php script recently that dynamically creates
In one PHP file, I have this code: require_once $_SERVER['DOCUMENT_ROOT'] . '/custom/functions.php'; global $testVar;
I have a small PHP framework that basically just loads a controller and a
So I have this simple PHP loop that is generating html table data with
I have a very painful slow script that gets lots of data from MySQL
I just started learning Zend (& OO PHP for that matter), I have spent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.