2

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!

2
  • 1
    $time='20:00'; check now Commented Jul 6, 2015 at 10:36
  • 1
    For debug, you can var_dump($n) in your for. It will help to know what's happening. Commented Jul 6, 2015 at 10:40

2 Answers 2

1

Please do like below:-

$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';
$new_array = array(); // create a new array
$i = 0;
while(($row = oci_fetch_row($stid)) != false) {

    $new_array[$i][] = $row[0]; // assign values to that new array
    $new_array[$i][] = $row[1]; // assign values to that new array
    $i++;
}
$sqlArray = array(); // create another new array
$n=0;
foreach($new_array as $array){
    $fitbit->setOAuthDetails($array[0], $array[1]); // iterate on first array values
    //retreive steps. (with date and time)
    $steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');
    $sqlArray[$n]['dateTime']=$value->dateTime; // assign value to new array
    $sqlArray[$n]['time'] = $time; // assign value to new array
    $sqlArray[$n]['steps'] = $value->value; // assign value to new array
    $n++;
}

Now what ever you want to do do on $sqlArray which is multidimensional array and you can check it's structure by echo "<pre/>";print_r($sqlArray);

Sign up to request clarification or add additional context in comments.

2 Comments

Yay this helped me partway down my problem!
Yay this helped me partway down my problem! ? what it means?
0

Try this:

$n=0;  // Initiate $n

while(($row = oci_fetch_row($stid)) != false) {

    $fitbit->setOAuthDetails($row[0], $row[1]);
    $steps = $fitbit->getTimeSeries('steps', '2015-06-01', '2015-07-01');

    // Dont initiate $n here

    foreach ($steps as $value){
        $sqlArray[$n]['dateTime']=$value->dateTime;
        $sqlArray[$n]['time'] = $time;
        $sqlArray[$n]['steps'] = $value->value;  
        // Dont increment $n here
    }

    $n++;  // Increment $n 'outside' your foreach

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.