开发者

grouping in subgroup(looping with php)

开发者 https://www.devze.com 2023-02-14 10:10 出处:网络
I once read a question about \"how to group result in subgroups in php\"(in this forum). I had a problem that is almost the same as the question.

I once read a question about "how to group result in subgroups in php"(in this forum). I had a problem that is almost the same as the question.

I have a MySQL table like this :

+----------+----------+----------+--------------+-------+-------+--------+
|   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     |
+----------+----------+----------+--------------+-------+-------+--------+

I want to show table records in a notification letter, like this:

jhon (A1)

Samrat street

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

ana (A2)

zero street

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

billy (A3)

west street

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

actually, I make the appearance by using FPDF and codeigniter. but I assume it was made in PHP page, because I just want to know how to make the process of looping using foreach method (php pure or codeigniter).

I hope somebody can help me. Thanks for 开发者_JAVA技巧your kindness..^^'


Try this one

$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>';

}

Moreover, you can browse User Guide of CodeIgniter for further help

0

精彩评论

暂无评论...
验证码 换一张
取 消