dsriya5471 2014-12-16 19:43
浏览 34
已采纳

访问while和for循环php之外的结果

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>"
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • dsz90288 2014-12-16 19:53
    关注
    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功