【问题标题】:grouping in subgroup(looping with php)分组分组(用 php 循环)
【发布时间】:2011-03-02 01:11:55
【问题描述】:

我曾经读过一个关于“如何在 php 中将结果分组到子组中”的问题(在这个论坛上)。我遇到了一个和问题几乎一样的问题。

我有一个这样的 MySQL 表:

+----------+----------+----------+--------------+-------+-------+--------+
|   type   |     ID   | customer |    address   | bill  |service| total  |
+----------+----------+----------+--------------+-------+-------+--------+
|water     |A1        |jhon      |Samrat street |100    |50     |150     |
|electric  |A1        |jhon      |Samrat street |30     |15     |45      |
|electric  |A2        |ana       |zero street   |20     |10     |30      |
|water     |A3        |billy     |west street   |40     |60     |100     |
+----------+----------+----------+--------------+-------+-------+--------+

我想在通知信中显示表格记录,如下所示:

约翰(A1)

萨姆拉特街

+---------+-------+----------+---------+
|type     |bill   |service   |total    |
+---------+-------+----------+---------+
|water    |100    |50        |150      |
|electric |30     |15        |45       |
+---------+-------+----------+---------+
|                 grand total|600      |
+----------------------------+---------+

安娜(A2)

零街道

+---------+-------+----------+---------+
|type     |bill   |service   |total    |
+---------+-------+----------+---------+
|electric |20     |10        |30       |
+---------+-------+----------+---------+
|                 grand total|30       |
+----------------------------+---------+

比利 (A3)

西街

+---------+-------+----------+---------+
|type     |bill   |service   |total    |
+---------+-------+----------+---------+
|water    |40     |60        |100      |
+---------+-------+----------+---------+
|                 grand total|100      |
+----------------------------+---------+

实际上,我是通过使用 FPDF 和 codeigniter 来制作外观的。但我假设它是在 PHP 页面中制作的,因为我只想知道如何使用 foreach 方法(php pure 或 codeigniter)制作循环过程。

我希望有人可以帮助我。 谢谢你的好意..^^'

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    试试这个

    $query = mysql_query('SELECT * FROM table GROUP BY ID');
    
    foreach (mysql_fetch_object($query) as $row){
    
        $data .= '<div>';
    
        $data .= '<p>' . $row->customer . '(' . $row->ID . ')' . '</p>';
    
        $data .= '<p>' . $row->address . '</p>';
    
        $data .= '<table>';
    
        $query1 = mysql_query('SELECT * FROM table WHERE ID = ' . $row->ID . ' AND customer = "' . $row->customer . '" AND address = "' . $row->address . '"');
    
        $data .= '
                    <tr>
                        <td>type</td>
                        <td>bill</td>
                        <td>service</td>
                        <td>total</td>
                    </tr>
                 ';
    
        $total = 0;                //variable initialized for grand total    
        foreach(mysql_fetch_object($sql1) as $row1){
    
        $data .= '
                    <tr>
                        <td>' . $row->type . '</td>
                        <td>' . $row->bill . '</td>
                        <td>' . $row->service . '</td>
                        <td>' . $row->total . '</td>
                    </tr>
                 ';
    
        $total += $row->total;     //adding the total amount for the grand total
    
        }
    
        $data .= '
                    <tr>
                        <td colspan="2" align="right">Grand Total</td>
                        <td colspan="2" align="right">' . $total . '</td>
                    </tr>
                 ';            //This displays the grand total
    
        $data .= '</table>';
    
        $data .= '</div>';
    
    }
    

    此外,您可以浏览User Guide of CodeIgniter以获取更多帮助

    【讨论】:

    • 感谢您的回答。但是,如何从每个 ID 的总数中计算总数。我一直在谷歌上寻找脚本,但我发现的只是简单的例子。我仍然对如何开发脚本感到困惑。
    • 我在上面代码的第二个循环中为每个 ID 的总和添加了一些代码。请检查。
    • 好的。抱歉我没看到,嘿嘿嘿...^^'。谢谢你的帮助
    猜你喜欢
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 2014-05-28
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多