0

我已经在这件事上工作了一段时间,但无法弄清楚。

我想要做的是运行一个函数,它将调用我在特定日期的所有记录并将它们输出到一个 html 表中。

我可以通过在循环内运行查询来做到这一点,但我想学习如何使用数组来完成这一点。

function getAll(){
    include('inc/connect.php');
    $a = $data->query("SELECT * FROM bookings WHERE active = 'true'");
    $args = array();

    while($b = $a->fetch_array()){
        $t1 = strtotime($b["endtime"]);
        $t2 = strtotime($b["starttime"]);
        $diff = $t1 - $t2;
        $hours = $diff / ( 60 * 60 );
        $args[] = array('start' => $b["starttime"],
                        'end' => $b["endtime"],
                        'span' => $hours
                    );
    }
    return $args;
}

以上是我运行以获取所有数组并返回数组的函数,但我对数组非常陌生,不知道如何针对下面运行它们。

$interval = 1800; // Interval in seconds

$date_first     = "02-09-2013 00:00:00";
$date_second    = "03-09-2013 00:00:00";

$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);

$go = getAll();

for ($i = $time_first; $i <= $time_second; $i += $interval){
    $timeOut = date('d-m-Y H:i', strtotime("+30 minutes", $i));

    //////I WOULD LIKE TO RUN THE ARRAY CALLED BY $go HERE TO SEE IF start == $timeOut/////
}

我试图将以下结果作为 td colspan 放到 html 表中来表示时间。在此先感谢您的帮助。

在此处输入图像描述

4

1 回答 1

0

很确定这不是处理这些数据的正确方法,但如果你只是想玩数组,

尝试这个:

$interval = 1800; // Interval in seconds

$date_first     = "02-09-2013 00:00:00";
$date_second    = "03-09-2013 00:00:00";

$time_first     = strtotime($date_first);
$time_second    = strtotime($date_second);

$go = getAll();


for ($i = $time_first; $i <= $time_second; $i += $interval){
    $timeOut = date('d-m-Y H:i', strtotime("+30 minutes", $i));

    //////I WOULD LIKE TO RUN THE ARRAY CALLED BY $go HERE TO SEE IF start == $timeOut/////

    // To process all entries in an array use the FOREACH verb
    // foreach entry in the array it will return the array key and the value associated with that key
    // as 2 new variables. As the value is another array I have called it $innerArray

    foreach ( $go as $key => $innerArray ) {

        if ( $innerArray['start'] == $timeout ) {

            $msg = 'timeout = ' . $timeout;
            $msg .= ' $go key = ' . $key;
            $msg .= ' innerArrays[start] = ' . innerArrays['start'];
            $msg .= ' innerArrays[end] = ' . innerArrays['end'];
            $msg .= ' innerArrays[span] = ' . innerArrays['span'];
            // not sure it you are using the CLI or Web Server so feel free to change this to something more simple
            $msg .= strtoupper(PHP_SAPI) == 'CLI' ? PHP_EOL : '<br>';

            echo $msg;
        }
    }
}
于 2013-09-14T10:44:49.310 回答