我如何重写我的PHP& MySQL以相同的列值对我的HTML列表进行分组? [英] How can I rewrite my PHP & MySQL to group my HTML list by equal column values?

查看:129
本文介绍了我如何重写我的PHP& MySQL以相同的列值对我的HTML列表进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 商业电话城市州国家

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

我有这个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><李> 中。 $ row ['state']。 < UL><李> 中。 $ row ['region']。 < UL><李> 中。 $ row ['city']。 < UL><李> 中。 $ row ['business']。 |电话号码:。 $ row ['phone']。 < /立GT;< / UL>< /立GT;< / UL>< /立GT;< / UL>< /立GT;< / UL>< /立GT;< / UL> ;;
}?>

这就是我的html:

 <?php echo $ result; ?> 

它的输出是这样的:

  CountryName 1 
- StateName 1
* CityName 1
*供应商| 9692824
CountryName 1
- StateName 1
* CityName 1
* Baker | 12345678
国家名1
- StateName 1
* CityName 3
* Welder | 58149918
CountryName 1
- StateName 2
* CityName 2
*梅森| 91834919
CountryName 2
- StateName 3
* CityName 4
* Painter | 48194918

我希望它输出如下:

  CountryName 1 
- StateName 1
* CityName 1
*供应商| 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
*厨师| 95982848

我必须做些什么才能完成这项工作?

我收集到的信息比我目前写出来的要复杂得多。 解决方案

在构建列表之前,你应该首先对它们进行分组(如你的结构所示​​),然后你可以建立这个列表。考虑这个例子:

 <?php 

$ original_data = array();
$ link = new MySQLI('localhost','username','password','database');
//正常选择
$ 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;
}

?>

<! - - 相应地打印它们 - >
<?php foreach($ ordered_data as $ country => $ state_values):?>
< ul>
< li><?php echo $ country; ?>< /锂>
<?php foreach($ state_values as $ state => $ city_values):?>
< ul>
< li><?php echo $ state; ?>< /锂>
< ul>
<?php foreach($ city_values as $ city => $ value):?>
< li><?php echo $ city; ?>< /锂>
< ul>
<?php foreach($ value as $ index => $ element):?>
< li><?php echo $ element ['Business']。 '| '。 $元件[电话]; ?>< /锂>
<?php endforeach; ?>
< / ul>
<?php endforeach; ?>
< / ul>
< / ul>
<?php endforeach; ?>< /锂>
< / ul>
<?php endforeach; ?>

输出示例


I have a database table that looks like this:

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

I have this PHP and MySQL generating an unordered list on my webpage:

<?$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>";
}?>

And this is my html:

<?php echo $result; ?>

It outputs like this:

 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

I would like it to output like this:

 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

What changes must I make to accomplish this?

I gathered that this is much more complicated than what I have currently written out.

解决方案

Before you build the list, you should group them accordingly first (as your structure shows), from then you can build the list. Consider this example:

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

Sample Output

这篇关于我如何重写我的PHP&amp; MySQL以相同的列值对我的HTML列表进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆