Jump to content

Bit confused about getting current "row" and data from previous row?


gibbonuk

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.