如何重写我的PHP&;MySQL是否将我的HTML列表按相等的列值分组?

如何重写我的PHP&;MySQL是否将我的HTML列表按相等的列值分组?,php,html,mysql,sql,Php,Html,Mysql,Sql,我有一个如下所示的数据库表: Business Phone City State Country Baker 12345678 CityName 1 StateName 1 CountryName 1 Mason 91834919 CityName 2 StateName 2 CountryName 1 Welder 58149918 CityName 3 StateName 1

我有一个如下所示的数据库表:

Business     Phone        City       State         Country

Baker      12345678    CityName 1  StateName 1   CountryName 1
Mason      91834919    CityName 2  StateName 2   CountryName 1
Welder     58149918    CityName 3  StateName 1   CountryName 1
Painter    48194918    CityName 4  StateName 3   CountryName 2
Chef       95982848    CityName 5  StateName 4   CountryName 3
Vendor     96928248    CityName 1  StateName 1   CountryName 1
 CountryName 1
    - StateName 1
        * CityName 1
            * Vendor | 9692824
 CountryName 1
    - StateName 1
        * CityName 1
            * Baker | 12345678
 CountryName 1
    - StateName 1
        * CityName 3
            * Welder | 58149918
 CountryName 1
    - StateName 2
        * CityName 2
            * Mason | 91834919
 CountryName 2
    - StateName 3
        * CityName 4
            * Painter | 48194918
 CountryName 1
    - StateName 1
        * CityName 1
            * Vendor | 9692824
            * Baker | 12345678
        * CityName 3
            * Welder | 58149918 
    - StateName 2
        * CityName 2
            * Mason | 91834919 
 CountryName 2
    - StateName 3
        * CityName 4
            * Painter | 48194918
 CountryName 3
    - StateName 4
        * CityName 5
            * Chef | 95982848
我让PHP和MySQL在我的网页上生成一个无序列表:

<?$con = mysql_connect("localhost","root","testdb");
 $db = mysql_select_db("table",$con);
 $get=mysql_query("SELECT * FROM Sheet1 ORDER BY country,state,region,city ASC");
  $result = '';
 while($row = mysql_fetch_assoc($get))
{
  $result .= "<ul class='tree'><li>" . $row['country'] . "<ul><li>" . $row['state'] . "<ul><li>" . $row['region'] . "<ul><li>" . $row['city'] . "<ul><li>" . $row['business'] . " | Phone #: " . $row['phone'] . "</li></ul></li></ul></li></ul></li></ul></li></ul>";
}?>
我希望它输出如下:

Business     Phone        City       State         Country

Baker      12345678    CityName 1  StateName 1   CountryName 1
Mason      91834919    CityName 2  StateName 2   CountryName 1
Welder     58149918    CityName 3  StateName 1   CountryName 1
Painter    48194918    CityName 4  StateName 3   CountryName 2
Chef       95982848    CityName 5  StateName 4   CountryName 3
Vendor     96928248    CityName 1  StateName 1   CountryName 1
 CountryName 1
    - StateName 1
        * CityName 1
            * Vendor | 9692824
 CountryName 1
    - StateName 1
        * CityName 1
            * Baker | 12345678
 CountryName 1
    - StateName 1
        * CityName 3
            * Welder | 58149918
 CountryName 1
    - StateName 2
        * CityName 2
            * Mason | 91834919
 CountryName 2
    - StateName 3
        * CityName 4
            * Painter | 48194918
 CountryName 1
    - StateName 1
        * CityName 1
            * Vendor | 9692824
            * Baker | 12345678
        * CityName 3
            * Welder | 58149918 
    - StateName 2
        * CityName 2
            * Mason | 91834919 
 CountryName 2
    - StateName 3
        * CityName 4
            * Painter | 48194918
 CountryName 3
    - StateName 4
        * CityName 5
            * Chef | 95982848
要做到这一点,我必须做出哪些改变


我发现这比我现在写的要复杂得多。

您正在选择数据库的所有记录,因此它会随意复制所有行,并向您这样表示。你必须给它正确的顺序。看看多维数组:


一旦你用正确的if-else语句给了它正确的顺序,它就会解决你的问题。

在你建立列表之前,你应该先将它们相应地分组(如你的结构所示),然后你就可以建立列表了。考虑这个例子:

<?php

$original_data = array();
$link = new MySQLI('localhost', 'username', 'password', 'database');
// normal select
$query = mysqli_query($link, 'SELECT * FROM Sheet1 order by country, state, city');
while($row = $query->fetch_assoc()) {
    $original_data[] = $row;
}

$ordered_data = array();
foreach($original_data as $key => $value) {
    // group them
    $ordered_data[$value['country']][$value['state']][$value['city']][] = $value;
}

?>

<!-- print them accordingly -->
<?php foreach($ordered_data as $country => $state_values): ?>
    <ul>
        <li><?php echo $country; ?></li>
        <?php foreach($state_values as $state => $city_values): ?>
        <ul>
            <li><?php echo $state; ?></li>
                <ul>
                    <?php foreach($city_values as $city => $value): ?>
                        <li><?php echo $city; ?></li>
                        <ul>
                            <?php foreach($value as $index => $element): ?>
                                <li><?php echo $element['Business'] . ' | ' . $element['Phone']; ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endforeach; ?>
                </ul>
        </ul>
        <?php endforeach; ?></li>
    </ul>
<?php endforeach; ?>


你能用一个例子进一步解释吗?我在学习这些技术的过程中发现,用一个例子来理解这些技术要简单得多。我如何给它正确的顺序?我试图设置这个多维数组,但我不知道如何将该数据插入html中的
    。感谢您提供这个示例。“li”和“ul”的嵌套并不完全正确,但我能够完全理解这个示例,它确实帮助我实现了我的最终目标。我很感激你的回答。