Group by Similar 2 fields in php and result with total count? - php

How to Group two Similar fields in php?
I tried with GROUP BY DATE(bill.date) , bill.agent_id but it is not working for me
Table Structure 1: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: http://i.stack.imgur.com/38tKh.jpg (table name is bill )
Current Result
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 0 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 0 |
| Agent 1 | 0 | 0 | 12 | 0 | 0 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
| Agent 2 | 0 | 0 | 0 | 9 | 14 |
+---------+----+----+----+-----+----+
1,2,3,4,5 .... are the days from date
But I want To get Like The Following
+---------+----+----+----+-----+----+----+
| | 1 | 2 | 3 | 4 | 5 |total
+---------+----+----+----+-----+----+----+
| Agent 1 | 35 | 0 | 12 | 0 | 0 |47 |
| Agent 2 | 0 | 10 | 0 | 0 | 14 |28 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |100 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 | 9 |
+---------+----+----+----+-----+----+----+
Php Code pasted below that I am using now .
<table width="100%" border="1" cellspacing="4" cellpadding="1">
<tr>
<td> </td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php echo $i; ?></td>
<?php
}
?>
<td>Total</td>
</tr>
<?php
$query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
"FROM bill, bill_agents ".
"WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ".
"GROUP BY bill.agent_id , DATE(bill.date) ".
// "GROUP BY bill.agent_id , DATE(bill.date) ".
"ORDER BY bill.date ASC";
$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4)>0){
mysql_data_seek($result4, 0);
?>
<?php $total_1 = 0; while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
<?php $date = $row4['date'];
$var = $date;
$date = date("d-m-Y", strtotime($var) );
$date=substr($date, 0, -8);
echo $date;
?>
<tr>
<td><?php echo $row4['name']; ?></td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php if ($date == $i) echo $row4['total']; ?></td>
<?php
}
?>
<td></td>
</tr>
<?php } } ?>
<tr>
<td colspan="31"></td>
<td>Total</td>
<td></td>
</tr>
</table>

Because I don't know if I understood your question here is a short sample which could point you in the right direction:
$query4 = "SELECT bill.agent_id, "
. " bill.date, "
. " SUM(bill.amount + bill.cheque) AS total, "
. " bill_agents.id, "
. " bill_agents.name "
. "FROM bill, bill_agents "
. "WHERE bill.agent_id = bill_agents.id "
. "AND YEAR(date) = YEAR(CURDATE()) "
. "AND MONTH(date) = MONTH(CURDATE()) "
. "GROUP BY bill.agent_id, DATE(bill.date) "
. "ORDER BY bill.date ASC";
$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4) > 0) {
mysql_data_seek($result4, 0);
$agents = array();
while (true == ($row4 = mysql_fetch_assoc($result4))) {
$agents[$row4['agent_id']][$row4['date']] = $row4;
}
var_dump($agents);
}
Take a look at the output and see if you can work further with that $agents variable.

Related

Prevent Duplicate Name or Re-entry of Name on showing

EDIT: Is it possible to query this? I have 2 table.
tithing table
tithings.MarrID = FK to Marriage table
tithings.Tithing_ID PK
tithingspayment table
tithingspayment.TP_ID = PK
tithingspayment.Tithing_ID = FK to tithings table "Tithing_ID"
Paid_Amount
Paid_Month_Year = Date
Marr_ID | Tithing_ID | TP_ID | Tithing_ID | Paid_Amount | Paid_Month_Year | TP_ID | Tithing_ID | Paid_Amount | Paid_Month_Year |
1 | 2 | 5 | 2 | 10.00 | Jan-2014 | 3 | 2 | 10.00 | Feb-2014 |
Sorry for the long codes; what I'm trying to ask is that I wanted not to show same name in my table:
<?php
$con=mysqli_connect("localhost","root","","RMS");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 30;
$sql = "SELECT *
FROM tithings
INNER JOIN marriage ON tithings.Marr_ID = marriage.MarrID
INNER JOIN tithings_payment ON tithings_payment.Tithing_ID = tithings.Tithing_ID WHERE YEAR( STR_TO_DATE( Dateregistered, '%Y' ) ) <= $year LIMIT $start_from, 30";
$result = mysqli_query($con,$sql);
echo "<div class='datagrid'><table>
<thead><tr>
<th>Family Name</th>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
<th>Operations</th>
</tr></thead>";
while($row = mysqli_fetch_array($result))
{
$id = $row['Tithing_ID'];
echo "<tbody><tr>";
$MonthRegistered = date("F", strtotime($row['Dateregistered']));
$YearRegistered = date("Y", strtotime($row['Dateregistered']));
$PaidMonthYear = $row['Paid_Month_Year'];
$PaidAmount = $row['Paid_Amount'];
$January = date("Y-m", strtotime($row['Paid_Month_Year']));
if($January == "$year-01")
{$January = $row['Paid_Amount'];}
else{$January = 0;}
$February = date("Y-m", strtotime($row['Paid_Month_Year']));
if($February == "$year-02")
{$February = $row['Paid_Amount'];}
else{$February = 0;}
$March = date("Y-m", strtotime($row['Paid_Month_Year']));
if($March == "$year-03")
{$March = $row['Paid_Amount'];}
else{$March = 0;}
$April = date("Y-m", strtotime($row['Paid_Month_Year']));
if($April == "$year-04")
{$April = $row['Paid_Amount'];}
else{$April = 0;}
$May = date("Y-m", strtotime($row['Paid_Month_Year']));
if($May == "$year-05")
{$May = $row['Paid_Amount'];}
else{$May = 0;}
$June = date("Y-m", strtotime($row['Paid_Month_Year']));
if($June == "$year-06")
{$June = $row['Paid_Amount'];}
else{$June = 0;}
$July = date("Y-m", strtotime($row['Paid_Month_Year']));
if($July == "$year-07")
{$July = $row['Paid_Amount'];}
else{$July = 0;}
$August = date("Y-m", strtotime($row['Paid_Month_Year']));
if($August == "$year-08")
{$August = $row['Paid_Amount'];}
else{$August = 0;}
$September = date("Y-m", strtotime($row['Paid_Month_Year']));
if($September == "$year-09")
{$September = $row['Paid_Amount'];}
else{$September = 0;}
$October = date("Y-m", strtotime($row['Paid_Month_Year']));
if($October == "$year-10")
{$October = $row['Paid_Amount'];}
else{$October = 0;}
$November = date("Y-m", strtotime($row['Paid_Month_Year']));
if($November == "$year-11")
{$November = $row['Paid_Amount'];}
else{$November = 0;}
$December = date("Y-m", strtotime($row['Paid_Month_Year']));
if($December == "$year-12")
{$December = $row['Paid_Amount'];}
else{$December = 0;}
if ($YearRegistered <= $year)
{$FamilyHead = $row['FName_Groom']. ", " . $row['LName_Groom'];
$operation = "<td class=operations>" . "<a class='detail' href='../record_detail.php?id=$id'>Details</a> | &nbsp<a class='pay' href='payment.php?id=$id'>Pay</a> | &nbsp<a class='edit' href='../record_edit.php?id=$id'>Edit</a></td>";
}else{
$FamilyHead = "<style>
tbody{
display:none;
}
</style>";
$operation = "";
}
echo "<td>" . $FamilyHead ."</td>";
echo "<td>" . $January . "</td>";
echo "<td>" . $February . "</td>";
echo "<td>" . $March ."</td>";
echo "<td>" . $April . "</td>";
echo "<td>" . $May . "</td>";
echo "<td>" . $June . "</td>";
echo "<td>" . $July ."</td>";
echo "<td>" . $August . "</td>";
echo "<td>" . $September . "</td>";
echo "<td>" . $October ."</td>";
echo "<td>" . $November . "</td>";
echo "<td>" . $December . "</td>";
echo $operation;
}
Tithing(Table)
Marr_ID(FK) | Tithing_ID (PK) | Dateregistered | Pledge | Barangay | Address |
5 | 1 | Sept, 13 2014 | 15.00 | Barangay1 | Address |
6 | 2 | Jan, 13 2014 | 15.00 | Barangay1 | Address |
Marriage (Table)
MarrID (PK) | FName_Groom | LName_Groom | MName_Groom |
5 | Name(5) | LName(5) | MName(5) |
6 | Name(6) | LName(6) | MName(6) |
Tithing_payment(Table)
Tithing_P_ID(PK) | Tithing_ID(FK) | Payment_Date_Received | Paid_Amount | Paid_Month_Year |
1 | 1 | Sept, 13 2014 | 15.00 | Sept,01 2014 |
2 | 1 | Sept, 13 2014 | 15.00 | Oct,01 2014 |
3 | 2 | Sept, 13 2014 | 15.00 | Jan,01 2014 |
4 | 2 | Sept, 13 2014 | 15.00 | Feb,01 2014 |
My Query Statement is
$sql = "SELECT *
FROM tithings
INNER JOIN marriage ON tithings.Marr_ID = marriage.MarrID
INNER JOIN tithings_payment ON tithings_payment.Tithing_ID = tithings.Tithing_ID WHERE YEAR( STR_TO_DATE( Dateregistered, '%Y' ) ) <= $year LIMIT $start_from, 30";
The Paid_Month_Year = Month(Jan-Dec) I used PHP if else statement
And the result is this.
Family Name | Jan | Feb | Mar | Apr | May | June | July | Aug | Sep | Oct | Nov | Dec |
Name(5) | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 15.00 | 0 | 0 | 0 |
Name(5) | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 15.00 | 0 | 0 |
Name(6) | 15.00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Name(6) | 0 | 15.00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Note: I tried Distinct In query didn't work.
What I wanted my output to look like is this;
Family Name | Jan | Feb | Mar | Apr | May | June | July | Aug | Sep | Oct | Nov | Dec |
Name(5) | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 15.00 | 15.00 | 0 | 0 |
Name(6) | 15.00 | 15.00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Something like this should work, you have to do something with the columns which you don't use in the group statement. In your case it makes sense to take the SUM, because someone could pay something twice in the same month, and also in another one.
SELECT
Family_Name,
SUM(Jan),
SUM(Feb)
....
SUM(Dec)
FROM
tithings
INNER JOIN marriage ON tithings.Marr_ID = marriage.MarrID
INNER JOIN tithings_payment ON tithings_payment.Tithing_ID = tithings.Tithing_ID
WHER
YEAR( STR_TO_DATE( Dateregistered, '%Y' ) ) <= $year
GROUP BY
Family_Name
LIMIT $start_from, 30

How to count the total of each group categorize by set from mysql

I may not use the proper subject of the problem. But here's the detail. I've got 3 tables of data 2 of them are set name and group name. The rest is data - user db. Here's the db.
set_name
+--------+-----------+
| set_id | set_title |
+--------+-----------+
| 1 | Set A |
+--------+-----------+
| 2 | Set B |
+--------+-----------+
group_db
+--------+-----------+--------+
| grp_id | grp_title | set_id |
+--------+-----------+--------+
| 1 | Grp. A | 1 |
+--------+-----------+--------+
| 2 | Grp. B | 1 |
+--------+-----------+--------+
| 3 | Grp. C | 1 |
+--------+-----------+--------+
| 4 | Grp. D | 1 |
+--------+-----------+--------+
| 5 | Grp. E | 1 |
+--------+-----------+--------+
| 6 | Grp. F | 2 |
+--------+-----------+--------+
user_db
+--------+-----------+
| usr_id | grp_id |
+--------+-----------+
| 1 | 1 |
+--------+-----------+
| 2 | 1 |
+--------+-----------+
| 3 | 2 |
+--------+-----------+
| 4 | 1 |
+--------+-----------+
| 5 | 3 |
+--------+-----------+
| 6 | 4 |
+--------+-----------+
| 7 | 5 |
+--------+-----------+
| 8 | 5 |
+--------+-----------+
| 9 | 5 |
+--------+-----------+
| 10 | 6 |
+--------+-----------+
According to the information provided above. I expect a summary table in which count all user and categorize by group and set. For example:
+-----+--------------------------------------------+--------+
| SET | Set A. | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM | 3 | 1 | 1 | 1 | 3 | 1 |
+-----+--------------------------------------------+--------+
|TOTAL| 9 | 1 |
+-----+--------------------------------------------+--------+
And this is how I do.
<table>
<tr>
<?
$sql_set=mysqli_query($con,"SELECT *,count(group_db.grp_id) AS nGrp\n"
. "FROM set_name\n"
. "INNER JOIN group_db ON set_name.set_id=group_db.set_id\n"
. "GROUP BY set_name.set_id\n"
. "ORDER BY set_name.set_id asc");
echo "<td>SET</td>";
while($rec_set=mysqli_fetch_array($sql_set)){
echo "<td colspan=\"$rec_set[nGrp]\">$rec_set[set_title]</td>";
}
?>
</tr>
<tr>
<?
$sql_sGrp=mysqli_query($con,"SELECT * from group_db\n"
. "WHERE set_id='$rec_set[set_id]'\n"
. "ORDER BY grp_title asc");
echo "<td>GROUP</td>";
while($rec_sGrp=mysqli_fetch_array($sql_sGrp)){
echo "<td>$rec_sGrp[grp_title]</td>";
}
?>
</tr>
</table>
That's it. I don't know how to go further. Please be advice.
Ps. Should I make them all in multilevel array to make it easier?
I would do something like:
SELECT
*
FROM
user_db u
JOIN
group_db g ON u.grp_id = g.grp_id
JOIN
set_name s ON g.set_id = s.set_id
(EDIT: changed qry to this ^ which can be seen here: http://sqlfiddle.com/#!2/e749f/4)
And then in PHP:
$newArray = array();
while($rec_set=mysqli_fetch_array($sql_set)){
$newArray[$rec_set['set_title']][$rec_set['grp_title']] += 1;
}
which should give you a nice multidimensional array of the results that you can parse through however you want
And to give a table that looks like:
+-----+--------------------------------------------+--------+
| SET | Set A. | Set B. |
+-----+--------------------------------------------+--------+
|GROUP| Grp. A | Grp. B | Grp. C | Grp. D | Grp. E | Grp. F |
+-----+--------------------------------------------+--------+
| NUM | 3 | 1 | 1 | 1 | 3 | 1 |
+-----+--------------------------------------------+--------+
|TOTAL| 9 | 1 |
+-----+--------------------------------------------+--------+
I would use:
<tr>
<td>SET</td>
<?php foreach($newArray as $set => $group): ?>
<td colspan="<?=count($newArray[$set])?>"><?=$set?></td>
<?php endforeach; ?>
</tr>
<tr>
<td>GROUP</td>
<?php foreach($newArray as $set => $group): ?>
<?php foreach($group as $group_name => $amount): ?>
<td><?=$group_name?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<tr>
<td>NUMBER</td>
<?php foreach($newArray as $set => $group): ?>
<?php foreach($group as $group_name => $amount): ?>
<td><?=$amount?></td>
<?php $totals[$set] += $amount;?>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<tr>
<td>TOTAL</td>
<?php foreach($newArray as $set => $group): ?>
<td colspan="<?=count($newArray[$set])?>"><?=$totals[$set]?></td>
<?php endforeach; ?>
</tr>
However, now that I look at how you would actually display it, if you really wanted a table that looked like you put, then a multidimensional array would probably not be the best way to loop through your data since all these loops are UGLY! (And it does not scale too well horizontally as you add more and more sets and groups). I did not check it for accuracy.
echo '<table>';
$rows = array('SET', 'GROUP', 'NUM', 'TOTAL');
$setids = array();
$grp_usercounts = array();
$set_usertotals = array();
foreach($rows as $key => $row){
echo "<tr> $rows </td>";
switch ($key){
case 0: //SET
$sql = "SELECT s.set_id, set_title, count(g.grp_id) nGrp
FROM set_name s
JOIN group_db g ON s.set_id = g.set_id
group by set_id";
$sql_set = mysqli_query($con, $sql);
while($rec_set=mysqli_fetch_array($sql_set)){
echo '<td colspan="'.$rec_set['nGrp'].'">'. rec_set['set_title'].'</td>';
$setids[$rec_set['set_id']] = $rec_set['nGrp'];
}
break;
case 1://GROUP
foreach($setids as $setid => $val){
$sql = "SELECT g.grp_id, grp_title, count(usr_id) nUsr
FROM group_db g
JOIN user_db u ON u.grp_id = g.grp_id
where set_id = $setid
group by g.grp_id order by grp_title";
$sql_set = mysqli_query($con, $sql);
$total = 0;
while($rec_set=mysqli_fetch_array($sql_set)){
echo '<td>'. $rec_set['grp_title'].'</td>';
$grp_usercounts[$rec_set['grp_id']] = $rec_set['nUsr'];
$total += $rec_set['nUsr'];
}
$set_usertotals[$setid] = $total;
}
break;
case 2://NUM
foreach($grp_usercounts as $key => $grp_usercount){
echo '<td>'. $grp_usercount .'</td>';
}
break;
case 3: //TOTAL
foreach($set_usertotals as $setid => $set_usertotal){
echo '<td colspan="'.$setids[$setid].'">'. $set_usertotal .'</td>';
}
break;
}
}
unset($setids);
unset($grp_usercounts);
unset($set_usertotals);
echo '</table>';

how to make simple php and mysql query to make table of product and stock

i need a help
assumptions that i have 2 table
table1 : product
-----------------------------
| id | Code | any else field |
-----------------------------
| 10 | GA 12 | -------------- |
| 11 | GA 1 | -------------- |
-----------------------------
table2 : stock
---------------------------------------------
| id | product_id | color | size | stock |
---------------------------------------------
| 1 | 10 | Black | M | 12 |
| 2 | 10 | Yellow | L | 10 |
| 3 | 10 | Black | S | 12 |
| 4 | 10 | Yellow | XL | 10 |
| 5 | 10 | Red | S | 10 |
| 6 | 10 | Black | XL | 12 |
| 7 | 10 | Yellow | M | 10 |
| 8 | 10 | Red | L | 12 |
| 9 | 10 | Red | XL | 12 |
| 10 | 10 | Gray | M | 12 |
---------------------------------------------
from the table we can see
for product_id 10 have 4 colors
Black with size S M and XL
Red with size S L and XL
yellow M L and XL
Gray M
that mean size range for product_id 10 is S - XL
from that 2 table i need a result on my website as below
and i have try using php and mysql query as below:
function getProduct(){
$Condb = #new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);
$sql = "
SELECT product.*
FROM product
";
$res2 = $Condb->query($sql);
while ($row = $res2->fetch_assoc()){
$return_data[] = $row;
}
$res2->close();
$Condb->close();
$return ['data'] = $return_data;
return $return;
}
--
function list_stock($product_id){
$Condb = #new MySQLi(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$Condb->connect_errno and die ('Connect Failed : '.$Condb->connect_error);
//Distinct to get size range from product_id
$sql = "
SELECT DISTINCT size FROM stock WHERE product_id='$product_id'
ORDER BY uk ASC
";
$res2 = $Condb->query($sql);
$return_size_range = array();
while ($row = $res2->fetch_assoc()){
$return_size_range[] = $row['size'];
}
$res2->close();
$return_stock[] = array();
$return_color = array();
$sql = "
SELECT * FROM stock
WHERE product_id='$product_id'
GROUP BY product_id, color, size
ORDER BY warna, uk ASC
";
$res2 = $Condb->query($sql);
$n = 0;
$w = 0;
while ($row = $res2->fetch_assoc()){
$size_index = 0;
if($n%count($return_size_range)== 0){
$color=$row['color'];
$return_color[$w]=$color;
$w=$w+1;
$n=0;
}
while($size_index == 0){
if($return_size_range[$n] == $row['size']){
$sz = $row['size'];
$return_stock[$n]['size' . $color] = $sz;
$return_stock[$n]['stock' . $color . $sz] = $row['stock'];
$size_index=1;
}else{
$size_index=0;
}
$n += 1;
}
}
$res2->close();
$Condb->close();
$return ['color'] = $return_color;
$return ['size'] = $return_size_range;
$return ['stock'] = $return_stock;
return $return;
}
for html i use below code :
<?php
$product = getProduct();
$product_detail = $product['data'];
?>
<table>
<tr>
<!-- header of table -->
<tr>
<?php
for($p=0 ; $p<count($produk_detail); $p++){
?>
<tr>
<!-- produk_detail -->
<tr>
<tr>
<td> <!-- with colspan from produk detail column -->
<?php
$stock_detail = list_stock($product_detail[$p]['id']);
$size = $stock_detail['size'];
$color = $stock_detail['color'];
$stock = $stock_detail['stock'];
?>
<table>
<tr>
<td>color</td>
<?php
for($s=0; $s<count($size); $s++){
echo '<td>' . $size[$s] . '</td>';
}
?>
</tr>
<?php
for($c=0; $c<count($color); $c++){
?>
<tr>
<td><?php echo $color[$c]; ?></td>
<?php
for($st=0; $st<count($size); $st++){
echo '<td>';
echo isset($stock[$st]['stock' . $color[$c] . $size[st]]) ? $stock[$st]['stock' . $color[$c] . $size[st]] : 0;
echo '</td>';
}
?>
</tr>
<?php
}
?>
<tr>
</tr>
</table>
</td>
<tr>
<?php
}
?>
<table>
based on above source code, i success to get the result as image i attached,
but the code is very long, and make loading slow to get the product and stock detail,
is this possible to make more simple of source code(php) and mysql query from my source code??
or is this possible to get result as image attched with simple php and mysql query??
thanks

Group by Similar 2 fields in php?

How to Group two Similar fields in php?
I tried with GROUP BY DATE(bill.date) , bill.agent_id but it is not working for me
Table Structure 1: http://i.stack.imgur.com/yvBF0.jpg (table name is bill_agents )
Table Structure 2: http://i.stack.imgur.com/38tKh.jpg (table name is bill )
Current Result
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 0 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 0 |
| Agent 1 | 0 | 0 | 12 | 0 | 0 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
| Agent 2 | 0 | 0 | 0 | 9 | 14 |
+---------+----+----+----+-----+----+
But I want To get Like The Following
+---------+----+----+----+-----+----+
| | 1 | 2 | 3 | 4 | 5 |
+---------+----+----+----+-----+----+
| Agent 1 | 35 | 0 | 12 | 0 | 0 |
| Agent 2 | 0 | 10 | 0 | 0 | 14 |
| Agent 3 | 0 | 0 | 0 | 100 | 0 |
| Agent 6 | 0 | 0 | 0 | 9 | 0 |
+---------+----+----+----+-----+----+
Php Code pasted below that I am using now .
<table width="100%" border="1" cellspacing="4" cellpadding="1">
<tr>
<td> </td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php echo $i; ?></td>
<?php
}
?>
<td>Total</td>
</tr>
<?php
$query4 = "SELECT bill.agent_id, bill.date, SUM(bill.amount + bill.cheque) AS total, bill_agents.id,bill_agents.name ".
"FROM bill, bill_agents ".
"WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ".
"GROUP BY bill.agent_id , DATE(bill.date) ".
// "GROUP BY bill.agent_id , DATE(bill.date) ".
"ORDER BY bill.date ASC";
$result4 = mysql_query($query4) or die('Error, query failed1');
if (mysql_num_rows($result4)>0){
mysql_data_seek($result4, 0);
?>
<?php $total_1 = 0; while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)){?>
<?php $date = $row4['date'];
$var = $date;
$date = date("d-m-Y", strtotime($var) );
$date=substr($date, 0, -8);
echo $date;
?>
<tr>
<td><?php echo $row4['name']; ?></td>
<?php
for ($i=01; $i<=31; $i++)
{?>
<td><?php if ($date == $i) echo $row4['total']; ?></td>
<?php
}
?>
<td></td>
</tr>
<?php } } ?>
<tr>
<td colspan="31"></td>
<td>Total</td>
<td></td>
</tr>
</table>
My understanding is, you need total amount processed by a agent and in a particular month of year.
Then change your query as follows,
<?php
$query = "SELECT b.date,b.agent_id,(SUM(b.amount) + SUM(b.cheque)) AS total,ba.id,ba.name
FROM bill_agent ba LEFT JOIN bill b ON b.agent_id = ba.id
WHERE YEAR(b.date) = YEAR(CURDATE()) AND MONTH(b.date) = MONTH(CURDATE())
GROUP BY b.date";
?>

How to echo specific data in loop with a condition specific to the last row

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 |
------------------------------------==========
Challenges
The table will be dynamic and will generate within a PHP loop (I'm
using a WHILE loop)
Remember: it's a table and if I miss echoing a td, then the table will break up
Problems
When I'm using the loop it's echoing the data on the Details td
accurately. But the sum of the details row according to j_id is also
echoing in each td
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 |
----------------------------------------------
My thoughts
If I can check whether it is the last data of the query, if isset,
then echo the total amount with it's td. (But remember the
Challenge#2)
Does it require a foreach loop?
I failed
I tried checking max(id), it works fine in SQL, but can't use it in
condition within a loop.
(If you still can't understand me, then on the second phase, I'll post my code.)
I would do 2 loops:
Fetch id from GROUP
Fetch 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>";
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>

Categories