创建约会数组,按日期排序(MySQL / PHP)

时间:2012-09-15 10:10:31

标签: php javascript mysql

我正在建立一个用户可以预约约会的网站。

我目前正在为该项目构建一个javascript应用程序,用户可以从日历中选择日期并查看可用的约会。当我构建日历时,我需要按可用的约会对日期进行着色(例如,绿色,如果有的话)。

为了做到这一点,我需要迭代一个包含所有可用预留的javascript数组。目前它看起来像这样:

[对象,对象,对象...]

对象是包含约会信息的javascript对象。这是为JSON服务的php服务:

<?php
require_once('../include/dbconnect.php');

$sql = "SELECT appointment.example,... 
person.example,... 
FROM appointment, person
WHERE appointment.reserved=0";

$stmt = $db->prepare($sql);
$stmt->execute();

$array = array();
while($row = $stmt->fetchObject()){
array_push($array, $row);
}

echo json_encode($array);
?>

所以这最终将我们带到了这个问题。

为了更容易的javascript数组扫描,我需要一个数组/对象包括按日期排列/排序的约会。然后当我创建一个表示日期的元素时,我可以检查对象是否匹配数据。这样的数据:

{
15.09.2012 : Object,
16.09.2012 : Object{
    appointment1 : Object,
    appointment2 : Object
} 

}

在数据库中,约会具有属性“date”,该属性当前是类似“16.09.2012”的字符串。我还应该将其更改为unix时间戳吗?

如何更改PHP服务以输出包含在日期下提交的约会的JSON对象?

3 个答案:

答案 0 :(得分:1)

一种可能的解决方案是在php中使用关联数组:

$assoc = array("key" => "value");

当您获取数据库记录时,您可以执行以下操作:

$array = array();
while($row = $stmt->fetchObject()){
$array[$row -> date] = $row;
}

echo json_encode($array);

对于排序,您可以使用ksorthttp://php.net/manual/en/function.ksort.php)php函数按键对数组进行排序。

现在你将拥有一个Javascript对象而不是Javascript数组。 现在,您可以在javascript循环(How to Loop through plain JavaScript object with objects as members?

中使用for ..迭代对象

答案 1 :(得分:1)

您可以尝试如下:     

$sql = "SELECT appointment.example,... 
person.example,... 
FROM appointment, person
WHERE appointment.reserved=0 ORDER BY appointment.date_field ASC";

$stmt = $db->prepare($sql);
$stmt->execute();

$array = array();
while($row = $stmt->fetchObject()){
$array[$row->date_field][]=$row;
array_push($array, $row);
}

echo json_encode($array);
?>

答案 2 :(得分:1)

您不需要构造由日期键组成的对象。代替:

  1. 在当前结构中包含日期,并按日期对数组进行排序:

    <?php
    require_once('../include/dbconnect.php');
    
    $stmt = $db->query('
      SELECT   DATE(appointment.datetime) AS date,
               appointment.datetime,
               appointment.reserved,
               appointment.example,     -- do you really want to send sensitive
               person.example           -- data about appointments to the browser?
      FROM     appointment JOIN person ON ...
      WHERE    appointment.reserved = 0 -- should this be = 1?
      ORDER BY appointment.datetime
    ');
    
    echo json_encode($stmt->fetchAll(PDO::FETCH_OBJ));
    ?>
    
  2. 然后在进行数组时跳过匹配的日期:

    // loop over all dates displayed in calendar
    for (
      currentDate  = startDate, i = 0;
      currentDate <= endDate;
      currentDate.setDate(currentDate.getDate() + 1)
    ){
    
      // determine whether the current date is booked
      // and advance pointer to next date in array
      for (
        booked = false;
        i < arr.length && arr[i].date == currentDate;
        ++i
      ) booked |= arr[i].reserved;  // or just `booked = true` if query
                                    // returned only booked appointments
    
      // apply appropriate CSS to currentDate
      // ...
    }
    
  3. 您甚至可以考虑通过首先返回仅预定日期的JSON数组来减少客户端开销(在这种情况下,上面的内部循环可以用简单的if语句替换):

    SELECT   DISTINCT DATE(appointment.datetime) AS date
    FROM     appointment JOIN person ON ...
    WHERE    appointment.reserved = 0 -- should this be = 1?
    ORDER BY date
    

    然后,一旦用户选择了某个日期,请在该日期进一步查询预订:

    SELECT   appointment.datetime
    FROM     appointment JOIN person ON ...
    WHERE    appointment.reserved = 0 -- should this be = 1?
         AND DATE(appointment.datetime) = ?
    ORDER BY appointment.datetime