Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Suming up in php and sql

Status
Not open for further replies.

funkygeezer1975

Technical User
Dec 14, 2004
22
GB
Please can anyone help
See code below i am wanting to add up the number of lines as it works through the while loop any ideas would be greatly appreciated

$sql = "SELECT * FROM delivery WHERE Check_Employee_id ='$_POST[checkemployee]' AND Check_Date = '$date'";
$result = mysql_query($sql,$conn);
while ($newArray = mysql_fetch_array($result)) {
$id = $newArray['DeliveryId'];
$Supplier = $newArray['Supplier'];
$Lines = $newArray['Num_of_lines'];
$stime = $newArray['Check_Start_Time'];
$ftime = $newArray['Check_Finish_Time'];

echo "
<tr><td><center>$id</center></td><td><center>$Supplier</center></td><td><center>$Lines</center> </td><td><center>$stime</center></td><td><center>$ftime</center> </td></tr>";
 
do you want the total number of lines returned by the query? if so, you could use this code:

Code:
$num_lines = mysql_num_rows($result)

otherwise - what are you trying to achieve?

Justin
 
No each delivery has a number of lines i need the total number of lines from each delivery i.e Delivery 1 has 32 lines and delivery 2 has 5 lines need to see the result 37 lines altogether Think that explains it a little clearer thanks for your time though
 
ok. why not just accrue a counter for each line?

i.e.
Code:
$counter=0;
$result = mysql_query($sql,$conn);
    while  ($newArray = mysql_fetch_array($result))   {
        $counter = $counter + $Lines;
        $id = $newArray['DeliveryId'];
        $Supplier = $newArray['Supplier'];
        $Lines = $newArray['Num_of_lines'];
        $stime = $newArray['Check_Start_Time'];
        $ftime = $newArray['Check_Finish_Time'];
        
        echo "
        <tr><td><center>$id</center></td><td><center>$Supplier</center></td><td><center>$Lines</center> </td><td><center>$stime</center></td><td><center>$ftime</center> </td><td>$counter</td></tr>";
}

if i have missed the point again, sorry! it might be useful to understand the structure of what you are trying to do.

hth
Justin
 
Cool thanks for that although i needed to move the counter line so that it would not be one sum behind as shown below $sql = "SELECT * FROM delivery WHERE Check_Employee_id ='$_POST[checkemployee]' AND Check_Date = '$date'";
$result = mysql_query($sql,$conn);
while ($newArray = mysql_fetch_array($result)) {
$counter = $counter + $Lines;
$id = $newArray['DeliveryId'];
$Supplier = $newArray['Supplier'];
$Lines = $newArray['Num_of_lines'];
$stime = $newArray['Check_Start_Time'];
$ftime = $newArray['Check_Finish_Time'];
$num_lines = mysql_num_rows($result);
$counter = $counter + $Lines;
echo "
<tr><td><center>$id</center></td><td><center>$Supplier</center></td><td><center>$Lines</center> </td><td><center>$stime</center></td><td><center>$ftime $counter</center> </td></tr>";

}


Many thanks now should be able to finish site.
 
Slight correction on that code as i left the first $counter = $counter + $lines

oops
took that out and all works great
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top