Prevent Duplicate Name or Re-entry of Name on showing - php

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

Related

Coordinates from MySQL to HTML table (PHP, PDO, MySQL)

I am creating game which uses MySQL database to create "playing field".
In my MySQL table I have two columns pointX and pointY, both INT. I could also use POINT, but in my case these two columns are better solution.
| id | pointX | pointY | player | game |
|----|--------|--------|--------|------|
| 1 | -2 | 1 | 7 | 10 |
| 2 | -3 | 2 | 5 | 10 |
| 3 | 2 | -2 | 2 | 10 |
| 4 | -2 | -1 | 1 | 10 |
I should produce HTML table from this MySQL table. Something like this, but with no coordinateheaders (below those are only for easier understanding):
|-----|----|----|----|----|----|----|
| Y/X | -3 | -2 | -1 | 0 | 1 | 2 |
|-----|----|----|----|----|----|----|
| -2 | | | | | | 2 |
|-----|----|----|----|----|----|----|
| -1 | | 1 | | | | |
|-----|----|----|----|----|----|----|
| 0 | | | | | | |
|-----|----|----|----|----|----|----|
| 1 | | 7 | | | | |
|-----|----|----|----|----|----|----|
| 2 | 5 | | | | | |
|-----|----|----|----|----|----|----|
Plus.. every <td> should have attribute data-cell, which includes coordinates, as example data-cell="-2x-1".
What is the best way to get started?
$rng = $dbh->prepare('
SELECT MIN(pointX) AS minX, MIN(pointY) AS minY,
MAX(pointX) AS maxX, MAX(pointY) AS maxY
FROM field
WHERE game = ?
LOCK IN SHARE MODE
');
$qry = $dbh->prepare('
SELECT pointX, pointY, player
FROM field
WHERE game = ?
ORDER BY pointX, pointY
');
$dbh->beginTransaction();
$rng->execute([$game_id]);
$qry->execute([$game_id]);
$dbh->rollBack();
$limits = $rng->fetch();
$player = $qry->fetch();
echo '<table>';
for ($y = $limits['minY']; $y <= $limits['maxY']; $y++) {
echo '<tr>';
for ($x = $limits['minX']; $x <= $limits['maxX']; $x++) {
printf('<td data-cell="%dx%+dy">', $x, $y);
if ($player and $player['pointX'] == $x and $player['pointY'] == $y) {
echo htmlentities($player['player']);
$player = $qry->fetch();
} else {
echo ' ';
}
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
It looks like you are already off to a good start. Assuming the number of cells for X and Y as $xcells and $ycells, the next step I would take is to build your table like so:
echo "<table>";
for ($i = -3; $i < $ycells; i++) {
echo "<tr>";
for ($j = -3; $j < $xcells; j++) {
echo "<td data-cell='" . $i . "y" . $j . "x'>";
}
echo "</tr>";
}
echo "</table>";

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";
?>

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

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.

array_sum error in looping for PHP And MySQL

I have some mysql table like this:
| IDPRAKTIKAN | NAMAUSER | MODULKE | NILAI | STATUS_TUGASAKHIR | TAHUNAJARAN | SEMESTER |
-----------------------------------------------------------------------------------------------
| 3332081478 | Ade Dwinanto | 0 | 3 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 1 | 55 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 2 | 54 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 3 | 45 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 4 | 62 | 1 | 09/10 | 3 |
| 3332081478 | Ade Dwinanto | 5 | 20 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 0 | 3 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 1 | 55 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 2 | 53 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 3 | 47 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 4 | 60 | 1 | 09/10 | 3 |
| 3332081483 | Arya Prasettyo | 5 | 20 | 1 | 09/10 | 3 |
This is my Data On MySQL (On sqlfiddle.com)
And This is My PHP Code:
<?php
$setting_tahunajaranget = $_GET['setting_tahunajaran'];
$setting_kodepraktikumget = $_GET['setting_kodepraktikum'];
$nama_praktikum = $_GET['nama_praktikum'];
$setting_semesterget = $_GET['setting_semester'];
$query2 = "SELECT `nilai` AS semester FROM `nilaipraktikum` WHERE `modulke`= 0 LIMIT 0, 30 ";
$data = mysql_fetch_row(mysql_query($query2));
$query = "SELECT `idpraktikan`,namauser,`modulke`, `nilai`,`status_tugasakhir`,`tahunajaran`,`semester` FROM `nilaipraktikum` LEFT JOIN tabeluser ON tabeluser.npmuser = nilaipraktikum.idpraktikan WHERE `tahunajaran` = '$setting_tahunajaranget' AND`kodepraktikum` = '$setting_kodepraktikumget' AND `semester` = '$setting_semesterget' ";
$result = mysql_query($query);
if ($data = mysql_fetch_row($result)>=1)
{
$nilai = $data['nilai'];
if ($nilai = $setting_semesterget);
echo "<table width='auto' border='1' cellpadding='3' cellspacing='3' align='center'>
<tr>
<td align='center' valign='middle'><strong>No</strong></td>
<td align='center' valign='middle'><strong>Nama</strong></td>
<td align='center' valign='middle'><strong>NPM</strong></td>";
for($i=1;$i<=$jml_modul;$i++) {
echo "<td align='center' valign='middle'><strong>Modul Ke-".$i."</strong></td>";
}
echo "<td align='center' valign='middle'><strong>Rata-Rata</strong></td>";
if ($setting_status_ta=0)
{
}
if ($setting_status_ta=1)
{
echo"<td align='center' valign='middle'><strong>Nilai Tugas Akhir</strong></td>";
}
echo "<td align='center' valign='middle'><strong>Nilai Akhir</strong></td>
<td align='center' valign='middle'><strong>Grade</strong></td>
</tr>";
while(list($nim,$nama,$modulke,$nilai,$nilai_tugasakhir) = mysql_fetch_row($result)) {
$array_nim[$nim] = $nim;
$array_nama[$nim] = $nama;
$array_nilai[$nim][$modulke]= $nilai;
$array_nilaita[$nim][$nilai_tugasakhir]= $nilai_tugasakhir;
}
foreach($array_nim as $nim) {
echo "<tr><td align='center' valign='middle'>".(++$nomor)."</td>";
echo "<td align='center' valign='middle'>".$nim."</td>";
echo "<td align='center' valign='middle'>".$array_nama[$nim]."</td>";
for($i=1;$i<=$jml_modul;$i++)
{
echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>";
$nilaimodul += (array_sum(array($array_nilai[$nim][$i])));
}
$ratarata = ($nilaimodul);
echo "<td align='center' valign='middle'>".round ($ratarata,2)."</td>";
$ntugasakhir = $array_nilai[$nim][$jml_modul-($jml_modul-1)];
if ($setting_status_ta=0)
{
}
if ($setting_status_ta=1)
{
echo "<td align='center' valign='middle'>".round ($ntugasakhir,2)."</td>";
}
$nialiakhirpraktikum = $ntugasakhir+$ratarata;
echo "<td align='center' valign='middle'>".round ($nialiakhirpraktikum,1)."</td>";
if ($nialiakhirpraktikum>79)
$grade="A";
else if ($nialiakhirpraktikum<=79 AND $nialiakhirpraktikum>67)
$grade="B";
else if ($nialiakhirpraktikum<=67 AND $nialiakhirpraktikum>55)
$grade="C";
else if ($nialiakhirpraktikum<=55 AND $nialiakhirpraktikum>44)
$grade="D";
else
$grade="E";
echo "<td align='center' valign='middle'>".$grade."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br/> <a href='javascript:history.back()'>Klik Disini Untuk Kembali</a>";
}
?>
my question, why for the Rata-Rata (is average NILAI) column summed array results with the results of the previous array?
For more details, please see the picture here. (Link Picture: https://www.diigo.com/item/image/3gviv/3m98?size=o)
whereas the results I want to average a column like this picture. (Link Picture: https://www.diigo.com/item/image/3gviv/6ke4?size=o)
what is wrong with my script?
NB:
column Nilai Tugas Akhir Nilai Akhir Grade is not yet fixed.
Just add initialization variable
$nilaimodul = 0; // try adding this to your code
for($i=1;$i<=$jml_modul;$i++)
{
echo "<td align='center' valign='middle'>".$array_nilai[$nim][$i]."</td>";
$nilaimodul += (array_sum(array($array_nilai[$nim][$i])));
}
$ratarata = ($nilaimodul);

how to group result in subgroups in php

Hey everyone. While I'm trying to learn some PHP and mySQL, I ran into a problem that I've had some difficulty in solving. I need a PHP script that queries mySQL database for country, books, book_price. Here is my dbtable:
+----+-------+----------+------------------------+
| id |Country|Books | Book_price |
+----+-------+----------+------------------------+
| 1 | USA | Zorro | 10 |
| 2 | USA | Zorro | 20 |
| 3 | USA | Zorro | 50 |
| 4 | USA | Leon | 200 |
| 5 | USA | Leon | 240 |
| 6 | ITALY| Tarzan | 70 |
| 7 | ITALY| Tarzan | 30 |
| 8 | ITALY| Tarzan | 100 |
| 9 | ITALY| Cobra | 300 |
| 10 | ITALY| Cobra | 320 |
| 11 | ITALY| Cobra | 350 |
+----+------+-----------+------------------------+
I want to organize the results based upon the country, books, total book, total country and TOTAL GEN (which is sum of all total country) and the result to show like this:
+----------------------------------------------------+
| USA |
+----------------------------------------------------+
| Zorro 10 |
| 20 |
| 50 |
+----------------------------------------------------+
| Total Zorro: 80 |
+----------------------------------------------------+
| Leon 200 |
| 240 |
+----------------------------------------------------+
| Total Leon:440 |
+----------------------------------------------------+
|Total USA: 520 |
+----------------------------------------------------+
|ITALY |
+----------------------------------------------------+
| Tarzan 70 |
| 30 |
| 100 |
+----------------------------------------------------+
| Total Tarzan:200 |
+----------------------------------------------------+
| Cobra 300 |
| 320 |
| 350 |
+----------------------------------------------------+
| Total Cobra: 970 |
+----------------------------------------------------+
|Total ITALY: 1170 |
+----------------------------------------------------+
|TOTAL GEN: 1690 |
+----------------------------------------------------+
Thank you
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("mydbname");
$sql = 'SELECT * FROM table_name';
$result = mysql_query($sql);
$data = array();
while ($row = mysql_fetch_assoc($result)) {
if ( empty($data[ $row['Country'] ]) ) {
$data[ $row['Country'] ] = array();
}
if ( empty( $data[ $row['Country'] ][ $row['Books'] ] ) ) {
$data[ $row['Country'] ][ $row['Books'] ] = array();
}
$data[ $row['Country'] ][ $row['Books'] ][] = $row['Book_price'];
}
$totalSum = 0;
foreach ( $data as $country => $books ) {
echo '<b>' . $country . '</b><br/>';
$totalCountry = 0;
foreach ( $books as $book => $prices ) {
$sum = array_sum( $prices );
echo '<u>' . $book . '</u><br/>';
echo implode(',', $prices) . '<br/>;
echo 'Total ' . $book . ':' . $sum . '<br/>';
$totalCountry += $sum;
}
echo 'Total ' . $country . ':' . $totalCountry . '<br/>';
echo '<hr/>';
$totalSum += $totalCountry;
}
echo 'TOTAL GEN: ' . $totalSum;
Use additional variables that hold the last country/book and their subtotals:
$last = array('Country' => null, 'Books' => null);
$subtotals = array('Country' => 0, 'Books' => 0);
echo '<table>';
while ($row = mysql_fetch_assoc($result)) {
if ($row['Books'] !== $last['Books']) {
if (!is_null($last['Books'])) {
echo '<tr><td colspan="2">Total '.$last['Books'].': '.$subtotals['Books'].'</td></tr>';
}
$last['Books'] = $row['Books'];
$subtotals['Books'] = $row['Book_price'];
} else {
$subtotals['Books'] += $row['Book_price'];
}
if ($row['Country'] !== $last['Country'])) {
if (!is_null($last['Country'])) {
echo '<tr><td colspan="2">Total '.$last['Country'].': '.$subtotals['Country'].'</td></tr>';
}
echo '<tr><th colspan="2">'.$row['Country'].'</th></tr>';
$last['Country'] = $row['Country'];
$subtotals['Country'] = $row['Book_price'];
} else {
$subtotals['Country'] += $row['Book_price'];
}
echo '<tr><td>'.$row['Books'].'</td><td>'.$row['Book_price'].'</td></tr>';
}
echo '</table>';
Try:
SELECT country, books, SUM(price)
FROM sales
GROUP BY country, books WITH ROLLUP;
Rows where books is null will return total for each country
Row where Country and Books are null return total for whole query
Check this out !
<?php
include ("config.php");
function render($price3) {
$output = "<td align='right'>".number_format($price3->book_price, 0, ',', '.')."</td>";
$output .= "</tr>";
return $output;
}
echo "<table border='1'>";
echo "<tr>
<th>books</th>
<th>price</th>
</tr>";
$result = mysql_query("select id, country, books, book_price from test") or die(mysql_error());
$set = array();
while ($record = mysql_fetch_object($result)) {
$set[$record->country][$record->books][] = $record;
}
$sum_country = 0;
foreach ($set as $country => $price1) {
echo "<tr>
<td align='center'>{$country}</font></td>
<td></td>
</tr>";
foreach ($price1 as $books => $price2 ) {
echo "<tr>
<td>{$books}</td>";
foreach ($price2 as $price3) {
echo render($price3);
}
$sum_books = 0;
foreach($price2 as $grbooks){
$sum_books += $grbooks->book_price;
}
echo "<tr><td>Total {$books}</td>
<td align='right'>".number_format($sum_books, 0, ',', '.')."</td>
</tr>";
}
$sum_country += $sum_books;
echo "<tr<td>Total {$country}</td>
<td align='right'>".number_format($sum_country, 0, ',', '.')."</td>
</tr>";
}
echo "</table>";

Categories