gavin stanley
gavin stanley

Reputation: 1102

Create unique single <div> element for all MySQL php results with particular value

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

Upvotes: 0

Views: 1381

Answers (3)

Emily
Emily

Reputation: 446

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>";

}

Upvotes: 1

gavin stanley
gavin stanley

Reputation: 1102

The solution I found was to nest a foreach inside another, the outer loop SELECTS with a GROUP BY. In this example I get one outer for each country, then inside that , every person (or item) within that country (or with that column value). Really useful.

NEW CODE: SOLUTION (use GROUP BY to build outer container)

// create array to build group div
$country = mysql_query("SELECT * FROM table GROUP BY `country`");
// create array for inner contents
$result = mysql_query("SELECT * FROM table ORDER BY name"); 

//array for group(countries in this instance)
$countrys = array();
while ($row = mysql_fetch_assoc($country)) {
$countrys[] = $row;
}
//array for inner contents
$results = array();
while ($row = mysql_fetch_assoc($result)) {
$results[] = $row;
}

$tempCountry="";
foreach ($countrys as $row)
    {   
    $tempCountry = $row['country'];

    echo "<div class=\"".$row['country']."\">";
    echo $row['country'];
    echo "<div class=\"Box\">";
    foreach ($results as $row) {
    if ($tempCountry == $row['country'])
    {
    echo"<div>inner contents of that group</div>";
    }
    }
    echo "</div>";
    echo "</div>";
    }

Upvotes: 0

Valjas
Valjas

Reputation: 5004

Create an empty array outside of your loop. On output push the values of $row into the array inside of your loop. Then only output if value does not already exist in the array. You can use in_array for this.

Upvotes: 0

Related Questions