gibbonuk Posted November 9, 2011 Share Posted November 9, 2011 Hi, im trying to get my head around this problem but unfortunatly its startig to hurt so come here for a bit of help. Im using this structure to get my results from my DB: <?php $query = "SELECT * FROM numbers"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $test[] = $row['number']; } ?> But what i want to do but dont know how is something like this: <?php $query = "SELECT * FROM numbers"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $test[] = $row['number']; $test2[] = $row['number']+THE NUMBER FROM THE PREVEIOUS ROW??; } ?> How do i go about this? Thanks Andy Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2011 Share Posted November 9, 2011 Without an example showing what data you have and what result you want, it's not really possible to provide an answer. Do you want to add up all the numbers? Do you want to add up each consecutive pair of numbers? Do you want to add up all the numbers up to that point? Quote Link to comment Share on other sites More sharing options...
gibbonuk Posted November 9, 2011 Author Share Posted November 9, 2011 Hi PFMaBiSmAdm, i really would post some data but i think it would be meaningless, as theres a few variables and loops within that i cant imagine make any sense to any but me. lol But I have a single database with 6 columns and many rows, all filled with small numbers (from 1 - 10). At the moment im just fetching a range of data back so say 10 rows and then putting them into an array in the structure above. But now i want to add another array that stores the current number + the last previous number for each column. Thanks Andy Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted November 9, 2011 Share Posted November 9, 2011 You would simply store (in a variable $last_num) the current value as the last value (after you have used the previous last value.) Which beings up the question, what do you want the 'last value' to initially be the first time through the loop for the first row of data? You could also do this after the data is stored in the first array by looping over the elements of the array and assessing the current key and the current key - 1 to get the previous value. Quote Link to comment Share on other sites More sharing options...
gibbonuk Posted November 9, 2011 Author Share Posted November 9, 2011 Ok well what im trying to do is store the value as it is and then the average of that last X values. So for this, i figured in the loop i need to get the current value (simple) and then some how the last X amount of values to calculate the average. So i end up with two arrays: array1 is filled with the actual numbers array2 is the same size as array1 but the values are the averages of the last X values. does that make any more sense? Yes, i guess calulating the averages from the arrays i have filled is one way, is it the best? can this not be done on the fly by getting the current row + the previous row(s)? Thanks Andy Quote Link to comment Share on other sites More sharing options...
fenway Posted November 9, 2011 Share Posted November 9, 2011 "previous row" has no meaning to a dataase. Quote Link to comment 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.