Listing issue, GROUP mysql

好久不见. 提交于 2020-01-06 14:44:15

问题


Here is a mock-up example of Mysql table:

| ID  |  Country  |  City      |
________________________________

| 1   | Sweden    | Stockholm  |
| 2   | Sweden    | Stockholm  |
| 3   | Sweden    | Lund       |
| 4   | Sweden    | Lund       |
| 5   | Germany   | Berlin     |
| 6   | Germany   | Berlin     |
| 7   | Germany   | Hamburg    |
| 8   | Germany   | Hamburg    |

Notice how both rows Country and city have repeated values inside them.

Using GROUP BY country, city in my PDO query, the values will not repeat while in loop. Here is PDO for this:

$query = "SELECT id, city, country FROM table GROUP BY country, city";
$stmt = $db->query($query);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :

The above code will result in an output like this (some editing in-between). GROUP BY works but the country repeats:

Sweden - Stockholm
Sweden - Lund
Germany - Berlin
Germany - Hamburg

Using bootstrap collapse and above code, I separate the country from the city with a simple drop down collopase. Here is code:

<li>
<a data-toggle="collapse" data-target="#<?= $row['id']; ?>" 
href="search.php?country=<?= $row['country']; ?>">
<?= $row['country']; ?>
</a>

<div id ="<?= $row['id']; ?>" class="collapse in"> //collapse div here 
<a href="search.php?city=<?= $row['city']; ?>">
<?= $row['city']; ?><br></a>
</div> //end
</li>   

It then looks something like this (once collapse is initiated):

Sweden 
> Stockholm
Sweden
> Lund
Germany
>Berlin 
Germany
>Hamburg

Here is where I face the problem. The above lists the values Sweden and Germany 2 times. I want Sweden and Germany to only list one time, and the cities listed below, so the desired look is to be this:

Sweden // Lists one time
> Stockholm
> Lund
Germany // Lists one time
>Berlin 
>Hamburg

I have tried using DISTINCT, GROUP_CONTACT and other methods, yet none get my desired output (above). Suggestions? Below is my current full code in action:

<?  
$query = "SELECT id, city, country FROM table GROUP BY country, city";
$stmt = $db->query($query);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :

?>
<li>
    <a data-toggle="collapse" data-target="#<?= $row['id']; ?>" 
    href="search.php?country=<?= $row['country']; ?>">
    <?= $row['country']; ?>
    </a>

    <div id ="<?= $row['id']; ?>" class="collapse in"> //collapse div here 
    <a href="search.php?city=<?= $row['city']; ?>">
    <?= $row['city']; ?><br></a>
    </div> //end
    </li>       
        <?  endwhile ?>     

回答1:


So, as discussed your mysql result isn't the problem. you have some logic to change. I don't have a enviroment to check, please let me know if it wont owrk.

$currCountry = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
?>
<li>
    <?if ( $currCountry != $row['country'] ) {?>
        <a data-toggle="collapse" data-target="#<?= $row['id']; ?>" 
         href="search.php?country=<?= $row['country']; ?>">
         <?= $row['country']; ?>
         </a>
    <?
    }
    $currCountry = $row['country'];
    ?>

<div id ="<?= $row['id']; ?>" class="collapse in"> //collapse div here 
<a href="search.php?city=<?= $row['city']; ?>">
<?= $row['city']; ?><br></a>
</div> //end
</li>       
<?  endwhile ?>  


来源:https://stackoverflow.com/questions/19626632/listing-issue-group-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!