Jump to content

MYSQL query to create nested lists.


poleposters

Recommended Posts

Hi,

 

I was wondering iff there was a way to structure a query to create a set of nested lists.

 

ie. I have a table with the columns NAME and CITY.

 

I want to structure a query to retrieve records to create a list grouped by location that displays every name within that location.

 

I tried the GROUP by clause, however that only returns one value for each location.

 

  • SYDNEY
    • Name-one
    • name-two

    [*]MELBOURNE

    • name-three
    • name-four
    • name-five

Link to comment
Share on other sites

try

<?php
$con = mysql_connect('localhost','root','');
mysql_select_db('test');
$sql = "SELECT `CITY`, GROUP_CONCAT(name SEPARATOR '</li><li>') AS list FROM `data_table` GROUP BY CITY";
$result = mysql_query($sql);
echo "<ul>\n";
while ($row = mysql_fetch_assoc($result)){
echo "<li>$row[CITY]</li>\n";
echo "<ul><li>$row[list]</li></ul>\n";
}
echo "</ul>";
?>

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.