Jump to content

group_contact issue


sethcodes

Recommended Posts

Hey, I am using GROUP_CONCAT and AS to list some data, here is my code:

 

<?  
$query = "SELECT id, country, group_concat(city) AS city, country FROM retailers GROUP BY country";
$stmt = $db->query($query);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
?>
<li>
<a data-pjax="content" data-toggle="collapse" data-target="#<?= $row['id']; ?>" href="retailers.php?country=<?= $row['country']; ?>">
<?= $row['country']; ?></a>


<div id ="<?= $row['id']; ?>" class="collapse in">
<a data-pjax="content" href="retailers.php?city=<?= $row['city']; ?>">
<?= $row['city']; ?></a><BR>
</div>
</li> 
<?  endwhile ?> 


</ul> 
 
This gets sort of what I am trying to achieve, but the issue I face now is all the results from $row['city']  does not output correctly. Instead it is just one big link and they are seperated by comma, when there is no comma in my code. 

Any ideas? My original post here http://stackoverflow.com/questions/19626632/listing-issue-group-mysql will give you a complete idea of what I want to achieve. 
Link to comment
Share on other sites

However.. working with what you have, it seems to me what you want to do is this (though tbh I read your SO post and i'm still not 100% clear on what you're trying to do. Perhaps a better explanation of what you are actually trying to accomplish would be helpful):

 

<?  
$query = "SELECT id, country, group_concat(city) AS city, country FROM retailers GROUP BY country";
$stmt = $db->query($query);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
?>
<li>
<a data-pjax="content" data-toggle="collapse" data-target="#<?= $row['id']; ?>" href="retailers.php?country=<?= $row['country']; ?>">
<?= $row['country']; ?></a>


<div id ="<?= $row['id']; ?>" class="collapse in">
<?php 
  $cities = explode(',',$row['city']);
  foreach ($cities as $city) {
?>
    <a data-pjax="content" href="retailers.php?city=<?= $city ?>"><?= $city; ?></a><BR>
<?php
  }
?>
</div>
</li> 
<?  endwhile ?> 


</ul> 
Link to comment
Share on other sites

However.. working with what you have, it seems to me what you want to do is this (though tbh I read your SO post and i'm still not 100% clear on what you're trying to do. Perhaps a better explanation of what you are actually trying to accomplish would be helpful):

 

<?  
$query = "SELECT id, country, group_concat(city) AS city, country FROM retailers GROUP BY country";
$stmt = $db->query($query);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
?>
<li>
<a data-pjax="content" data-toggle="collapse" data-target="#<?= $row['id']; ?>" href="retailers.php?country=<?= $row['country']; ?>">
<?= $row['country']; ?></a>


<div id ="<?= $row['id']; ?>" class="collapse in">
<?php 
  $cities = explode(',',$row['city']);
  foreach ($cities as $city) {
?>
    <a data-pjax="content" href="retailers.php?city=<?= $city ?>"><?= $city; ?></a><BR>
<?php
  }
?>
</div>
</li> 
<?  endwhile ?> 


</ul> 

Yes, this is what I am looking to do, is this method above the best approach though? It works fine and thank you, but is their an alternative way to your above example?

Link to comment
Share on other sites

Eh, TBH IDK. I'm not a sql expert. If you just needed 2 dimensions (e.g. country and cities) then I would leave out the group_concat() and then put the results in a 2d array and then have a nested loop (or just do a nested loop with a condition for the countries if you don't need to save the results for something later). But with that 3d dimension there (the id).. I guess what you've got now is probably a decent enough way to handle it. IOW I can't really think of a "better" way to handle it.. but again, I'm not a sql expert. You may wanna try posting in the sql forum here..there's a bunch of really frakking smart folks there that would be able to tell you if there's a better way!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.