php - Not getting sum of rows from table -
the results of <span>$invoicepartpaid1</span>
, <span>$invoicepartpaid2</span>
blank. there should sums. suggestions?
$query="select distinct company, car_moto, packet companies userid='$userid' order company, car_moto, packet"; $result=mysql_query($query); $num=mysql_numrows($result); $i=0; while ($i < $num) { $f0=mysql_result($result,$i,"company"); $f1=mysql_result($result,$i,"car_moto"); $f2=mysql_result($result,$i,"packet"); $sql_1 = "select sum(price_protect) partpaid1 companies company='$f0' , car_moto='$f1' , packet='$f2' , userid='$userid'"; $sql_2 = "select sum(clear_protect) partpaid2 companies company='$f0' , car_moto='$f1' , packet='$f2' , userid='$userid'"; $result_1=mysql_query($sql_1) or die('error query failed'); $result_2=mysql_query($sql_2) or die('error query failed'); while ($row_1 = mysql_fetch_array($result_1)) { $invoicepartpaid1 = $row_1['partpaid1']; } while ($row_2 = mysql_fetch_array($result_2)) { $invoicepartpaid2 = $row_2['partpaid2']; } echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>"; echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>"; $i++; }
replace line
$num=mysql_numrows($result);
by
$num=mysql_num_rows($result);
full code
<?php $query="select distinct company, car_moto, packet companies userid='$userid' order company, car_moto, packet"; $result=mysql_query($query); $num=mysql_num_rows($result); $i=0; while ($row=mysql_fetch_array($result)) {// loop results replacing $i < $num $f0=$row["company"]; $f1=$row["car_moto"]; $f2=$row["packet"]; $sql_1 = "select sum(price_protect) partpaid1 companies company='$f0' , car_moto='$f1' , packet='$f2' , userid='$userid'"; $sql_2 = "select sum(clear_protect) partpaid2 companies company='$f0' , car_moto='$f1' , packet='$f2' , userid='$userid'"; $result_1=mysql_query($sql_1) or die('error query failed'); $result_2=mysql_query($sql_2) or die('error query failed'); $invoicepartpaid1=0;$invoicepartpaid2=0; while ($row_1 = mysql_fetch_array($result_1)) { $invoicepartpaid1 = $row_1['partpaid1']; } while ($row_2 = mysql_fetch_array($result_2)) { $invoicepartpaid2 = $row_2['partpaid2']; } echo "<tr><td align='center'><span>$f0</span></td><td align='center'><span>$f1</span></td><td align='center'><span>$f2</span></td>"; echo "<td align='center'><span>$invoicepartpaid1</span></td><td align='center'><span>$invoicepartpaid2</span></td></tr>"; $i++; } ?>
Comments
Post a Comment