dmhall0 Posted February 20, 2012 Share Posted February 20, 2012 I have elevation points in an array. I need to calculate the difference between each point to get the total gain, total loss, and net. Example: point 2 - point 1 = +100 point 3 - point 2 = -10 point 4 - point 3 = -20 ... and so on So my outputs need to be: Total Gain = +100 Total Loss = -30 Total Net = +70 Any help would be great. I am still new to coding but this seems complicated. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/ Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2012 Share Posted February 20, 2012 Where are the values coming from? Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319377 Share on other sites More sharing options...
dmhall0 Posted February 20, 2012 Author Share Posted February 20, 2012 MySQL table that I pulled out with a query. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319378 Share on other sites More sharing options...
requinix Posted February 21, 2012 Share Posted February 21, 2012 Which looks like...? Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319379 Share on other sites More sharing options...
Pikachu2000 Posted February 21, 2012 Share Posted February 21, 2012 That's what I was kind of thinking. You could do the math in the query as well. SELECT point 2 - point 1 AS diff2_1, point 3 - point 2 AS diff3_2, point 4 - point 3 AS diff4_3 FROM table WHERE whatever . . . Then the calculated values would be in the array produced by whichever mysql_fetch_* function you use. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319380 Share on other sites More sharing options...
dmhall0 Posted February 21, 2012 Author Share Posted February 21, 2012 SELECT elevation FROM track_points WHERE activity_id = '813'; There could be thousands of points (rows) for the one activity, so doing the work in the query is pretty much out of the question. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319381 Share on other sites More sharing options...
Pikachu2000 Posted February 21, 2012 Share Posted February 21, 2012 Then you're going to need to elaborate a little more, I believe. Are you trying to go through the array programatically, and just find the difference of a point and the previous point, regardless of the number of points in the array? Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319382 Share on other sites More sharing options...
dmhall0 Posted February 21, 2012 Author Share Posted February 21, 2012 I am wanting to find the difference between every 2 points, just like in my first post example, then sum all the numbers > 0, sum all the numbers < 0, then give me the total. So if there are 1000 points in the array, I would get 500 numbers. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319383 Share on other sites More sharing options...
dmhall0 Posted February 21, 2012 Author Share Posted February 21, 2012 Found a way to solve it via MySQL here. http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-CHP-12-SECT-13.html Thanks for the help though. Quote Link to comment https://forums.phpfreaks.com/topic/257418-calculating-difference-between-points-in-array/#findComment-1319403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.