Forum Moderators: coopster

Message Too Old, No Replies

Way to get value of next record

while printing out current value?

         

knighty

6:30 pm on Mar 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




I'm adding up figures for certain job IDs of which there are many by comparing the last records jobID with the current.

for example

SELECT * from blah order by recordID

if ($lastrecordID!="currentrecordID"){Do stuff}

This works fine until I get to the last record where because there are no more records it will not trigger the if statement.

Is there a way to find out what the next record will be so that if there isnt one I can print out the total?

OR is there a better way of doing this?

bcolflesh

7:22 pm on Mar 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dear Knighty,
Couldn't you GROUP BY recordID, then get the SUM of the figure field you are adding up?

Regards,
Brent

jatar_k

7:23 pm on Mar 6, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ypou could maybe do something like

if ($lastrecordID!="currentrecordID" ¦¦!isset(currentrecordID)){
Do stuff
if (!isset(currentrecordID)) {
print totals;
}
}

there are a ton of methods for it, you could else if (!isset(currentrecordID)) or you could put a check in the bottom of the original if but that depends on other logic, to name a few.

knighty

9:13 am on Mar 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys but none of those suggestions are gonna work :(

bcolflesh - totals are in hour:minute format and wont add them up properly

jatar_k - I'm looping through results so it will never trigger the!isset part

//mysql stuff here
for ($i=0; $i < $num; $i++) {
$jobName=mysql_result($result,$i,"jobName");
$jobID=mysql_result($result,$i,"jobID");
$total=mysql_result($result,$i,"total");

if ($prevjobID!=$jobID){ print out totals }

id1 1:00
id1 4:00
id3 2:00 (id3!=id1 so it will spit out totals for id1)
1d3 1:15

the last ids will never get printed cos nothing to compare it against

ie last loop would look like - id3 DOES EQUAL id3 so nothing happens

-------------------------------------------------

Hmmmmm, just copied the [do stuff] bit and placed it after the closing loop brace so it will always print out totals - works fine now :)