我曾经读过一个关于“如何在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 |
+----------+----------+----------+--------------+-------+-------+--------+
我想在通知信中显示表记录,如下所示:
jhon(A1)
Samrat街
+---------+-------+----------+---------+
|type |bill |service |total |
+---------+-------+----------+---------+
|water |100 |50 |150 |
|electric |30 |15 |45 |
+---------+-------+----------+---------+
| grand total|600 |
+----------------------------+---------+
ana(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)进行循环过程。
我希望有人可以帮助我。 谢谢你的善意.. ^^'
答案 0 :(得分:0)
试试这个
$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以获取进一步的帮助