0

I have a table which has a field isClaimed that has only two fixed values = CLAIMED or NOT CLAIMED. I have to calculate the total of each field.

FYI, assume this is my table:

name | isClaimed  
Aye  | NOT CLAIMED 
Ian  | CLAIMED  
Jan  | NOT CLAIMED  
Zen  | NOT CLAIMED  
Pom  | CLAIMED  

Total of unclaimed: 3
Total of claimed: 2

And please check my code below:

<?php 

 $sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro 
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId 
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending

$result2 = mysql_query($sql);  

if($result2 === FALSE) {
die(mysql_error()); // TODO: better error handling
} 

// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {

    $firstname = $row2["Firstname"];
    $lastname = $row2["Lastname"];
    $middlename = $row2["Middlename"];
    $barangay = $row2["BarangayName"];
    $level = $row2["LevelName"];
    $allowance = $row2["Allowance"];
    $isClaimed = $row2["isClaimed"];

?>

<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?>   </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>


<?php
// Exit looping
}

?>

<tr>
<td colspan="4" class="spec">Total of unclaimed allowances</td>
<td></td>
 </tr>

<tr>
<td colspan="4" class="spec">Total of claimed allowances</td>
<td></td>
</tr>

I have tried the tutorial from here: http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/ But i can't get it to work in php.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • The tutorial you linked us, none of the code there has been used on in script - What did you try? – Happy Jan 05 '13 at 06:16
  • Please provide your table strucutre with demo data here: http://sqlfiddle.com/ currently i would say its a problem of INNER / LEFT JOIN. But to be shure i need those example data. – GreenRover Jan 05 '13 at 06:18
  • yeah I did what you asked me. Please if you want to view my database structure refer here: pastebin.com/u5HfNyr0 –  Jan 05 '13 at 06:47
  • Use of mysql/ext(`mysql_*`) function is discouraged and depricated in php 5.5 so use pdo or mysqli instead check this post for more reference [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) here is [pdo tutorial](http://stackoverflow.com/a/14110189/1723893) if you want to lean pdo – NullPoiиteя Jan 05 '13 at 06:58

2 Answers2

0

From the tutorial you linked....

$sql = "SELECT
SUM(IF(sca.isClaimed = "CLAIMED", 1,0)) AS claimedTotal,
SUM(IF(sca.isClaimed = "NOT CLAIMED", 1,0)) AS notClaimedTotal,
pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName,       
school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId
ORDER BY pro.LevelId, pro.ScholarId";

And then

echo $row2["claimedTotal"];

and

echo $row2["notClaimedTotal"];

Note that I used the table sca for for the isClaimed value, just a guess...not sure of your table structure, maybe you will need to change sca to reflect the correct table.

Happy
  • 806
  • 4
  • 7
  • Hi when I added this: only 1 record displays. –  Jan 05 '13 at 06:27
  • With your keyboard? ;) Your question has lost me a little, its the second and third line of the query, just change the value. – Happy Jan 05 '13 at 06:43
0
<?php 
$claimedCount = 0;
$unclaimedCount= 0;
$sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro 
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId 
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending

$result2 = mysql_query($sql);  

if($result2 === FALSE) {
    die(mysql_error()); // TODO: better error handling
} 

// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {

    $firstname = $row2["Firstname"];
    $lastname = $row2["Lastname"];
    $middlename = $row2["Middlename"];
    $barangay = $row2["BarangayName"];
    $level = $row2["LevelName"];
    $allowance = $row2["Allowance"];
    $isClaimed = $row2["isClaimed"];

?>

<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?>   </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>


<?php
if($row2["isClaimed"] == "CLAIMED")
    $claimedCount++;
elseif($row2["isClaimed"] == "NOT CLAIMED")
    $unclaimedCount++;
// Exit looping
}

?>

<tr>
    <td colspan="4" class="spec">Total of unclaimed allowances</td>
    <td><?php echo $unclaimedCount;?></td>
</tr>

<tr>
    <td colspan="4" class="spec">Total of claimed allowances</td>
    <td><?php echo $claimedCount;?></td>
</tr>

Note: I have not checked you query. I have just mentioned my suggestion about getting the count that suits your current structure. Moreover, its highly recommended to start using mysqli_* instead of mysql.

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32