0

I have start and finish times for my users

Times are in seconds in db and converted in gmdate() in page please keep in mind when you see the integers.

 user1 startTime = 9:00;  finishTime = 10:00;
 user2 startTime = 12:00;  finishTime = 1:00;

 <?php

 if ($results = $db->query("SELECT * FROM times WHERE Id = $user->data()->id")){
    if($results->num_rows){

       $value = 1800;

      while($row = $results->fetch_array()){

         //This will return the startTime, finishTime, and any blocks of time in between them
         for($n_block = $row['timeStart']; $n_block <= $row['timeFinish']; $n_block+=$value){
            echo "$n_block<br>";
        }
     }
   }
 }
 ?>

 The desired results are:
 32400
 34200
 36000
 43200
 45000
 46800

But I need to put the results in an array that is accessed outside the loop

   // need to get above while and for loop results in this array outside the loops
   $excludes = array();

if I use $start = ""; please see http://pastie.org/9784700

if I take the for loop out of the while loop please see http://pastie.org/9784732

How do I achieve the desired results? Meaning how can I access the results of the combined loops outside of the loops?

var_dump($row);

array(18) { 
    [0]=> string(1) "5"
    ["appointmentCount"]=> string(1) "5"
    [1]=> string(2) "23"
    ["technicianId"]=> string(2) "23"
    [2]=> string(2) "22"
    ["customerId"]=> string(2) "22"
    [3]=> string(10) "1417129200"
    ["appointmentDay"]=> string(10) "1417129200"
    [4]=> string(5) "43200"
    ["appointmentStart"]=> string(5) "43200"
    [5]=> string(5) "46800"
    ["appointmentFinish"]=> string(5) "46800"
    [6]=> string(8) "FULL SET"
    ["serviceName"]=> string(8) "FULL SET"
    [7]=> string(2) "50"
    ["serviceCost"]=> string(2) "50"
    [8]=> string(8) "November"
    ["appointmentMonth"]=> string(8) "November" 
} 
array(18) {
    [0]=> string(1) "6"
    ["appointmentCount"]=> string(1) "6"
    [1]=> string(2) "23"
    ["technicianId"]=> string(2) "23"
    [2]=> string(2) "22"
    ["customerId"]=> string(2) "22"
    [3]=> string(10) "1417129200"
    ["appointmentDay"]=> string(10) "1417129200"
    [4]=> string(5) "32400"
    ["appointmentStart"]=> string(5) "32400"
    [5]=> string(5) "36000"
    ["appointmentFinish"]=> string(5) "36000"
    [6]=> string(8) "Nail Tip"
    ["serviceName"]=> string(8) "Nail Tip"
    [7]=> string(2) "30"
    ["serviceCost"]=> string(2) "30"
    [8]=> string(8) "November"
    ["appointmentMonth"]=> string(8) "November" 
}




<?php
if ($appointmentResults = $db->query("SELECT * FROM appointments WHERE technicianId = $userid")){
    if($appointmentResults->num_rows){
        $start = "";
        $finish = "";
        $value = 1800;

        while($row = $appointmentResults->fetch_array()){
            $start .= $row['appointmentStart'] . '<br>';
            $finish .= $row['appointmentFinish'] . '<br>';

            for($n_block = $row['appointmentStart']; $n_block <= $row['appointmentFinish']; $n_block+=$value){
                echo "$n_block</br>"
            }
        }
    }
}
1
  • @bcesars the loops work, I want to access the results outside the loop Commented Dec 16, 2014 at 19:49

2 Answers 2

1

At the beginning of your script, before the if, add:

    $excludes = array();

In your loop, right before echo, add:

    $excludes[] = $n_block;

When you drop out of the if, $excludes will be an array of the integers you want.

If the query fails to find any records, $excludes will be an empty array.

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

1 Comment

I tried that as well see what was said to helicode. If not I do not mind repeating. Thank you for the suggestion.......... helicode I tried that as well. When I var_dump($excludes); in the forloop I get duplicates of user2 and missing finishTime and middle times of user1.... outside for loop and while loop I get finishTimes of user1 and user2
0
$excludes = array();
if($appointmentResults = $db->query("SELECT * FROM appointments WHERE technicianId = $userid")) {
    if($appointmentResults->num_rows) {
        $value = 1800;
        while($row = $appointmentResults->fetch_array()) {
            for($n_block = $row['appointmentStart']; $n_block <= $row['appointmentFinish']; $n_block+=$value){
                echo $n_block."</br>";
                $excludes[] = $n_block;
            }
        }
    }
}
sort($excludes);
print_r($excludes);

18 Comments

@helicode I tried that as well. When I var_dump($excludes); in the forloop I get duplicates of user2 and missing finishTime and middle times of user1.... outside for loop and while loop I get finishTimes of user1 and user2
Do a var_dump($row) inside your for loop. let's see your data. Also, have you shown your exact PHP code or could it differ from the "live" code?
no problem I have that ready as well. When I var_dump($row) it returns NULL
It shouldn't be Null -- that would mean you have no data. Sorry I meant the while loop. Put the var_dump just before the for statement.
@TomRobinson when var_dump($row) before for loop it returns all the data from the db
|

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.