Skip to content
Advertisement

SELECT *, SUM(NET) AS TOTAL FROM TABLE1 WHERE CLAUSE

<?php require_once('../includes/connection.php');?>
<?php require_once('../includes/header.php');?>

<?php
$color="1";
$respo = $_GET['respo'];
$data = explode("+", $respo);
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";

**$viewrecord = "SELECT *, (pr.roll1 + pr.roll2 + pr.roll3 + pr.roll4 + pr.roll5 + pr.roll6) AS rolls FROM tbl_payroll dv join tbl_payroll pr on pr.dv_id = dv.dv_id WHERE dv.respo='".mysql_real_escape_string($data[0])."' && year(dv.date_added)='$year' && month(dv.date_added)='$month'";**
$run_viewrecord = mysql_query($viewrecord) or die(mysql_error());

{
echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
------------HEADER--------- etc....

-------THERE SHOULD BE A IF STATEMATE HERE-----------------
(where if no records match "dv.dv_id=pr.pr.dv_id". It would still display records from tbl_dv..)

while ($row = mysql_fetch_row($run_viewrecord)) {

if($color==1){
echo "<tr bgcolor='#ffffff'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";

$color="2";
}   else {
echo "<tr bgcolor='#ebeaea'>";
echo "<td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
**--------- I WANT TO DISPLAY THE ROLLS HERE --------------------**
echo "</td></tr>";

$color="1";
}
}
echo '</table>';
echo '<td><tr><table><br /><br />';
}
?>

I was hoping to add an IF statement before WHILE. Which will still display records even there is no match dv_id on both table2. It should still display records.. The COLUMN ROLLS IF No match it will display a 0.00 value. LINK>> http://i599.photobucket.com/albums/tt79/emcevo/viewphpdisplay_zpsfc6a8174.jpg

//Total NET
<?php
$qry2 = "SELECT *, SUM(net) as sum_net FROM tbl_dv";
$run2 = mysql_query($qry2) or die(mysql_error());

while ($row = mysql_fetch_array($run2)) {
?>
<tr>
<td colspan="5" style="text-align:right;" /><b>TOTAL NET</b></td>
<td colspan="6" style="text-align:left;font-size: 14px;" /><b><?php echo  number_format($row['sum_net'],2); ?></b></td>
</tr><?php }?>
</td></tr></table>

How can I display the TOTAL NET below code:

$qry2 = "SELECT *, SUM(net) as sum_net FROM tbl_dv";. 

I top codes are all working.. The bold Section BUTTOM code is the Problem.

Advertisement

Answer

I guess you get downvotes because your whole php code-block is unnecessary as far I can see. Your query “$qry2” is incorrect; you can’t sum without a group by (unless you only do a sum). eg:

select dv_id, sum(net) as sum_net from tbl_dv group by dv_id
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement