0

我知道这可能很简单,但我是 PHP 和 MySQL 开发的新手,所以我很感激一些帮助。我在 Stack 上找不到我可以理解的现有示例,因此我将其发布在那里,希望得到一些指导。

我有一个包含地址信息的数据库表。我将地址分成两个字段,house_number 和 street_name,我需要做的是显示按街道名称分组的信息,然后按门牌号排序。

我已经模拟了一个我需要页面外观的示例。它在第一列中具有偶数地址,在第二列中具有奇数地址。我正在使用 Twitter Bootstrap 进行格式化。

$address = mysql_query("SELECT id, house_number, street_name, full_address FROM resident WHERE neighborhood_id = $neighborhood_id order by street_name, house_number;");

它应该是什么样子

在此处输入图像描述

下面的 HTML 代码:

<!--STREET GROUPING -->
    <div class="row-fluid">
        <div class="span12">
        <h3>ASHTON PARK WAY</h3>
        </div>
    </div >

    <div class="row-fluid">
        <div class="span6"><!-- EVEN ADDRESSES -->
            <a href="#" class="btn btn-large btn-white btn-block">18102 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18104 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18106 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18108 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18110 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18112 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18114 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18116 Ashton Park Way</a>
        </div><!-- END EVEN ADDRESSES -->

        <div class="span6"><!-- ODD ADDRESSES -->
            <a href="#" class="btn btn-large btn-white btn-block">18101 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18103 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18105 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18107 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18109 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18111 Ashton Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18113 Ashton Park Way</a>
        </div><!-- END ODD ADDRESSES -->

    </div>
<!-- END STREET GROUPING -->

    <hr>

<!--STREET GROUPING -->
    <div class="row-fluid">
        <div class="span12">
        <h3>HAMDEN PARK WAY</h3>
        </div>
    </div >

    <div class="row-fluid">
        <!-- EVEN ADDRESSES -->
        <div class="span6">
            <a href="#" class="btn btn-large btn-white btn-block">18102 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18104 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18106 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18108 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18110 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18112 Hamden Park Way</a>
        </div>
        <!-- END EVEN ADDRESSES -->

        <!-- ODD ADDRESSES -->
        <div class="span6">
            <a href="#" class="btn btn-large btn-white btn-block">18101 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18103 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18105 Hamden Park Way</a>
            <a href="#" class="btn btn-large btn-white btn-block">18107 Hamden Park Way</a>
        </div>
        <!-- END ODD ADDRESSES -->

    </div>
<!-- END STREET GROUPING -->
4

2 回答 2

1

我不确定您要在这里更改什么。Group By函数是 SQL 可能不会做你认为它应该做的事情。它通常最好在上下文中使用,例如

SELECT count(invoicesTotal),sum(invoicesTotal) FROM invoices GROUP BY customer

在您的数据库上,运行

SELECT * FROM resident GROUP BY street_name

您只会取回找到的与 street_name 匹配的第一条记录,这不是您在此上下文中要查找的内容。

我认为您现在使用ORDER BY street_name, house_number的方式是最好的,并使用 foreach 来构建您的表格。

也不要使用 mysql 扩展。使用 mysqli 扩展。mysql 很快就会被弃用。从初学者的角度来看,没有太大的区别。

于 2013-02-11T06:18:28.973 回答
0
$address = mysql_query("SELECT id, house_number, street_name, full_address FROM resident WHERE neighborhood_id = $neighborhood_id group by street_name order by house_number");
于 2013-02-11T06:07:56.100 回答