Reputation: 4762
For an accounting system, I'm using PHP & MySQL. I've two tables "GROUP" and "ACHEADS".
In the GROUP table, I have:
---------------------
| id (AI) | group |
---------------------
| 1 | Group 1 |
| 2 | Group 2 |
---------------------
In the ACHEADS table, I have:
-----------------------------------------
| id (AI) | ac_head | amount | j_id |
-----------------------------------------
| 1 | Something 1 | 2000 | 1 |
| 2 | Something 2 | 1000 | 1 |
| 3 | Something 3 | 5000 | 2 |
| 4 | Something 4 | 4000 | 2 |
| 5 | Something 5 | 8000 | 2 |
-----------------------------------------
I've joined the two tables as GROUP.id <<->> ACHEADS.j_id
Now I need to preview the data like this:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | |
Something 4 | 4000 | |
Something 5 | 8000 | 17000 |
----------------------------------------------
GRAND TOTAL | | 20000 |
------------------------------------==========
Preview here:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | 3000 |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | 17000 |
Something 4 | 4000 | 17000 |
Something 5 | 8000 | 17000 |
----------------------------------------------
(If you still can't understand me, then on the second phase, I'll post my code.)
Upvotes: 2
Views: 1603
Reputation: 4762
njk rockz!
It worked nicely. Thanks a lot, brother - it helped me a lot, I can't explain.
Here is my final code:
<tr style="background: #000; color:#fff;"> <th style="width:150px;">Particulars</th> <th>Details</th> <th>Amount</th> </tr> <tr> <td>Opening Balance</td> <td></td> <td>500000</td> <!-- till not dynamic --> </tr> <?php $total = 0; $se = "SELECT * FROM group"; $res = mysql_query($se) or die (mysql_error()); while ($row = mysql_fetch_array($res)) { ?> <tr> <td colspan="3" style="font-weight:bold;"><?php echo $row['group']; ?></td> </tr> <tr> <?php $group_total = 0; $se1 = "SELECT ac_head, amount FROM `acheads` WHERE `j_Id` = '".$row['id']."'"; $res1 = mysql_query($se1) or die (mysql_error()); while ($row1 = mysql_fetch_array($res1)) { $group_total = $group_total + $row1['amount']; ?> <td><?php echo $row1['ac_head']; ?></td> <td><?php echo $row1['amount']; ?></td> <td> </td> </tr> <?php } echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>'; } ?> </table> </code>
Upvotes: 0
Reputation: 34054
I would do 2 loops:
id
from GROUP
amount
from ACHEADS
based on j_id
This would look something like (non-tested code):
echo '<table><tr><td>Particulars</td><td>Details</td><td>Total</td></tr>';
$total = 0;
$q1 = "SELECT id FROM `GROUP`";
$res1 = mysqli_query($q1);
while($row1 = mysqli_fetch_assoc($res1)) {
echo
$group_total = 0;
$j_id = $row1[id];
$q2 = "SELECT ac_head, amount FROM ACHEADS WHERE j_id = $j_id";
$res2 = mysqli_query($q2);
while($row2 = mysqli_fetch_assoc($res1)) {
echo '<tr><td>' . $row2[ac_head] . '</td>';
echo '<td>' . $row2[amount] . '</td></tr>';
$group_total = $group_total + $row2[amount];
$total = $total + $row[amount];
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
echo '<tr><td>GRAND TOTAL</td>';
echo '<td colspan="2" align="right">' . $total . '</td></tr>';
echo "</table>";
Upvotes: 2