Problem: I have a foreach loop in a while loop. for each row selected from the database the foreach loop has to done. but when I echo $row it seems like it immedieatly puts out all the rows and then afterwards goes trough the foreach just once. I've read through the manuals but it did not help me unfortunately.
Code:
$conn = oci_connect('login', 'pass', '127.0.0.1/xe');
$stid = oci_parse($conn, 'select TOKEN, SECRET from TOKENS');
oci_execute($stid);
$time=20:00
while(($row = oci_fetch_row($stid)) != false) {
$fitbit->setOAuthDetails($row[0], $row[1]);
//retreive steps. (with date and time)
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
$n=0;
foreach ($steps as $value){
$sqlArray[$n]['dateTime']=$value->dateTime;
$sqlArray[$n]['time'] = $time;
$sqlArray[$n]['steps'] = $value->value;
$n++;
}
}
It's probably me overlooking something but I hope my question can be answered.
Updated code:
$time="12:00";
$n=0;
$x=0;
while(($row = oci_fetch_row($stid)) != false) {
$sqlArray[$n]['token'] = $row[0];
$sqlArray[$n]['secret'] = $row[1];
echo 'hoi';
$fitbit->setOAuthDetails($sqlArray[$n]['token'], $sqlArray[$n]['secret']);
//retreive steps. (with date and time)
$steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
foreach ($steps as $value){
$sqlArray[$n][$x]['dateTime']=$value->dateTime;
$sqlArray[$n][$x]['time'] = $time;
$sqlArray[$n][$x]['steps'] = $value->value;
$x++;
}
//retreive calories and add to sqlArray where date matches. (used for testing now, when acces to intraday api the outcommented code below will be used.
$calories = $fitbit->getTimeSeries('caloriesOut', '2015-06-01', '2015-07-01');
$x=0;
foreach ($calories as $value){
$checkdate=$value->dateTime;
if ($sqlArray[$n][$x]['dateTime'] == $checkdate){
$sqlArray[$n][$x]['calories'] = $value->value;
}
$x++;
}
$n++;
};
Now my output is as follows:
Array
(
[0] => Array
(
[token] => token
[secret] => secret
[0] => Array
(
[dateTime] => 2015-06-01
[time] => 12:00
[steps] => 8046
[calories] => 2785
)
Which iteraties nicely over the tokens from the database. Though, After the first iteration the calories are dropped:
[1] => Array
(
[token] => token
[secret] => secret
[31] => Array
(
[dateTime] => 2015-06-01
[time] => 12:00
[steps] => 8046
)
I guess I'm doing something wrong again... I think it has to do with the $x variable, but I am not sure.
Yep it's me! I placed $x=0; in the while loop now and it works good! Thanks for the help!
$time='20:00';check nowvar_dump($n)in yourfor. It will help to know what's happening.