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 8219335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:07:21+00:00 2026-06-07T13:07:21+00:00

Here is my problem. I am returning results from a mysql database and using

  • 0

Here is my problem. I am returning results from a mysql database and using a for loop to echo the results. Though its getting a little complicated because I am using some table data to nest inside other results. This code returns “Pablo Picasso” inside a div called “Spain”, fine, but if there is “El Greco” in Spain too, then I get two “Spain” divs, rather than just the one.

So: I’d like to only return a result once for each unique value in a table column, rather than for every one.

$results = array();
while ($row = mysql_fetch_assoc($result)) {
$results[] = $row;
}

foreach ($results as $row)
    {
    echo "<div class=\"".$row['country']."\">".$row['country'];
    echo "<div class=\"Box\">";
    $tempCountry = $row['country'];

    foreach ($results as $row)
    {
    if ($row['country']== $tempCountry) echo "<div>artists name</div>";
    }

    echo "</div>";echo "</div>";
}

I’m wondering if it is the construction of the nested loop, or something else, I don’t know!!! 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-06-07T13:07:28+00:00Added an answer on June 7, 2026 at 1:07 pm

    I agree with @Kalpesh that you need to order the results by country.

    Right now you have multiple nested loops when all you really should need is one. Try using just one loop to go through the data. On every iteration, check the $tempCountry value to see if it different compared to the current row’s country. If it is, you need to close the current <div>, open a new <div> and update the $tempCountry value. Otherwise, echo the name of the artist.

    EDIT: Psuedocode added below

    • Retrieve data from database (the query should sort the data by country)
    • Initialize $tempCountry to null
    • Loop over every row
      • If $tempCountry equals this row’s country
        1. Print the artist
      • Else
        1. Set $tempCountry equal to this row’s Country
        2. Close the div tag
        3. Open a new div tag
        4. Print the artist

    Note that you do not want to close the div tag on the first time through the loop. Also, you need to close the div tag after the loop finishes.

    foreach ($results as $row) {
    
        if ($tempCountry == $row['country']) {
    
        echo "<div class=\"Box\">";
    
            echo "<div>artist</div></div>";
    
        }
    
        else {
    
            $tempCountry == $row['country'];
    
            echo "</div><div class=\"".$row['country']."\">".$row['country'];
    
            echo "<div class=\"Box\">";
    
            echo "<div>artist</div></div>";
    
        }
    
        echo "</div>";
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to LINQ and am having a problem getting proper results from a
I am trying to get serialized results from a WCF service from database using
i dont understand the problem with returning multiple rows: here is my table BBC:
I'm working on a simple subtraction problem, but unfortunately it keeps returning NaN Here
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
I have a problem here I can't solve. I have a database of houses
enter code here My problem is this: in this database the junction table contains
Here is the main problem. I have very large database (25,000 or so) of
Newbie here. The code below is doing a great job returning the results I
I am having a bit of a problem with my MYSQL query, here I

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.