I have a task that I need to implement, in a nutshell I want to be able to read multiple date values from a table in mySQL using PHP then manipulate these values to get date difference in days, save these new days (int values) into another array for further manipulation (linear regression). My problem is I cant get to the point where I can save initial values retrieved from the DB into an array, but when I try to manipulate these values and save them into a second array, it does not work. here is my simple code:
$result = mysql_query("SELECT TO_DAYS(date) FROM ordering WHERE id=1",$conn);
$num_rows = mysql_num_rows($result);
// mysql_query($result,$conn) or die(mysql_error();
//echo $num_rows;
$data = array();
$days=array();
while (($row = mysql_fetch_array($result, MYSQL_ASSOC)) !== false){
$data[] = $row; // add the row in to the results (data) array
}
print_r($data); // print result
//This part is not working
for ($x=$num_rows;$x>0;$x--){
$days[x]=(($data[$x])-($data[$x-1]));
}
print_r($days);
mysql_close($conn);
If I don't include the for loop in my code, I get an output on the screen as follows:
Array (
[0] => Array ( [TO_DAYS(date)] => 735599 )
[1] => Array ( [TO_DAYS(date)] => 735630 )
[2] => Array ( [TO_DAYS(date)] => 735658 )
[3] => Array ( [TO_DAYS(date)] => 735689 )
[4] => Array ( [TO_DAYS(date)] => 735735 )
[5] => Array ( [TO_DAYS(date)] => 735780 )
[6] => Array ( [TO_DAYS(date)] => 735811 )
)
Please I really need some help here. Thank You - Hamood