【问题标题】:accessing results outside of while and for loop php在while和for循环php之外访问结果
【发布时间】:2014-12-16 19:43:18
【问题描述】:

我为我的用户设置了开始和结束时间

时间在 db 中以秒为单位,并在页面中的 gmdate() 中转换 看到整数时请记住。

 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

但我需要将结果放入循环外访问的数组中

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

如果我使用 $start = ""; 请看http://pastie.org/9784700

如果我将 for 循环从 while 循环中取出 请看http://pastie.org/9784732

我怎样才能达到预期的效果?意思是如何在循环之外访问组合循环的结果?

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>"
            }
        }
    }
}

【问题讨论】:

  • @bcesars 循环工作,我想访问循环外的结果

标签: php for-loop while-loop


【解决方案1】:

在脚本的开头,if 之前,添加:

    $excludes = array();

在您的循环中,就在echo 之前,添加:

    $excludes[] = $n_block;

当您退出 if 时,$excludes 将是您想要的整数数组。

如果查询没有找到任何记录,$excludes 将是一个空数组。

【讨论】:

  • 我也试过了,看看对 helicode 说了什么。如果不是,我不介意重复。谢谢你的建议........ helicode 我也试过了。当我 var_dump($excludes);在 forloop 中,我得到了 user2 的重复项,并且缺少 user1 的完成时间和中间时间……在 for 循环和 while 循环之外,我得到了 user1 和 user2 的完成时间
【解决方案2】:
$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);

【讨论】:

  • @helicode 我也试过了。当我 var_dump($excludes);在 forloop 中,我得到了 user2 的重复项,并且缺少 user1 的完成时间和中间时间……在 for 循环和 while 循环之外,我得到了 user1 和 user2 的完成时间
  • 在 for 循环中执行 var_dump($row)。让我们看看你的数据。另外,您是否显示了您的确切 PHP 代码,或者它是否与“实时”代码不同?
  • 没问题,我也准备好了。当我 var_dump($row) 它返回 NULL
  • 它不应该是 Null - 这意味着你没有数据。抱歉,我的意思是 while 循环。将var_dump 放在for 语句之前。
  • @TomRobinson 当 var_dump($row) 在 for 循环之前返回数据库中的所有数据
猜你喜欢
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2018-01-10
  • 2014-06-20
  • 2015-03-19
相关资源
最近更新 更多