2013-07-22 32 views
0

我试图写一个脚本来拉日期未来7天,并把它们放到一个div对于每一日期,接下来5天:获取预约使用PHP

echo '<div class="dateboxcontainer">'; 
    for($i=0; $i<=6; $i++){ 
     echo '<div class="datebox"><div class="topdate">'.strtoupper(date("D d", mktime(0, 0, 0, 0, date("d")+$i, 0))."\n"). 
     '</div><div class="bottomdate">An appointment for the day</div></div>'; 
    } 
    echo '</div>'; 

enter image description here

林现在想从我的数据库由两场“datedroppingoff”和“datepickingup”,这是此格式“2013年7月10日14时29分28秒”中提取数据。

林种坚持,虽然作为即时通讯不知道该写什么查询把约会每天到每一天的div其中,“一些信息”目前是。

即时猜测它会是这样的

Select * FROM jobdetails WHERE datedroppingoff OR datepickingup = WHATEVER DAY IS BEING ECHO'D OUT 

,但即时通讯不能肯定我怎么可以比较被回显出存储在jobdetails该行的日期的日期?

编辑>>>>>

感谢下面的答案,四设法想出下面,回声出日期框好,但犯规中的任何数据带来的,所以如果我林不知道SQL部分是否正确?

  echo '<div class="dateboxcontainer">'; 
    $eventdata = <<<SQL 
     SELECT * 
     FROM `jobdetails` 
    SQL; 
    if(!$events = $db->query($eventdata)){ 
     die('There was an error running the query [' . $db->error . ']'); 
    } 
    // read first event 
    if ($nextEvent = mysql_fetch_assoc($events)) { // here is the first one 
     extract($nextEvent); // prepare its variables 
     // use the event date it to control the inner loop 
     $nextDate = $datedroppingoff; 
    } else // no events? 
     $nextDate = 0; // prepare a fake date value 

    // calculate today date 
    $currentDate = mktime(); 

    // loop on the dates for the next 7 days 
    for ($i = 0 ; $i < 7; $i++) { 

     $currentEvents = ""; 

     // loop to print every event for current day (first one already extracted) 
     while ($nextDate == date("Y-m-d", $currentDate)) { // next event occurs today 

      // here prepare the var containing the event description 
      // BTW, I'd use a list for the events 
      $currentEvents .= "· $name<br>"; // use every field you need from current DB row 

      // read next event 
      if ($nextEvent = mysql_fetch_assoc($events)) { // here is the next one 
       extract($nextEvent); // prepare its variables 
       // use the event date it to control the inner loop 
       $nextDate = $datedroppingoff; 
      } else // no more events? 
       $nextDate = 0; // prepare a fake date value 
     } 

     echo " 
      <div class='datebox'> 
       <div class='topdate'>" . strtoupper(date("D d m Y", $currentDate)) . "</div> 
       <div class='bottomdate'>$currentEvents</div> 
      </div>"; 

     $currentDate = strtotime("+1 day", $currentDate); 
    } 
    echo '</div>'; 
+1

'WHERE(日期(datedroppingoff)> = 'STARTDATE' 和日期(datedroppingoff)<= '结束日期')​​OR(日期(datepickingup)> = 'STARTDATE' 和日期(datepickingup)<=' enddate')'..为了便于格式化,请查看'DateTime'和可能的'DateInterval'对象。 – Wrikken

+0

但是,如何在未来7天将其添加到for循环中,那就是即时通讯有什么问题? –

回答

0

我似乎做的工作结束时使用这个!:)

// Date box container 
    echo '<div class="dateboxcontainer">'; 
    // Loop through and create a date for the next 7 days 
    $days = new DatePeriod(new DateTime, new DateInterval('P1D'), 7); 
    foreach ($days as $day) { 
    echo '<div class="datebox">'; 
    echo '<div class="topdate">'; 

      echo strtoupper($day->format('D d')) . PHP_EOL; 
    echo '</div>'; 


       // Get the names for each day 
      $theday = strtoupper($day->format('Y-m-d'));  
      $sqldate = <<<SQL 
     SELECT * 
     FROM `jobdetails` 
     WHERE datedroppingoff = '$theday' OR datepickingup = '$theday' 
    SQL; 
    if(!$resultdate = $db->query($sqldate)){ 
     die('There was an error running the query [' . $db->error . ']'); 
    } 
    while($rowdate = $resultdate->fetch_assoc()){ 
     echo $rowdate['name']; 
      } 
     // 

    echo '</div>'; 
    } 
    echo '</div>'; 
    // 
1

你可以使用像这样:

SELECT * FROM `jobdetails` WHERE (`datedroppingoff ` > '2013-07-01 00:00:00' AND `datedroppingoff ` '2013-07-02 00:00:00') OR (`datepickingup ` > '2013-07-01 00:00:00' AND `datepickingup ` '2013-07-02 00:00:00'); 

这将每天要重复(即针对每个5天)

要做到一个很好的循环,使用数组填充了接下来的5个日期作为字符串。然后对日期执行foreach并运行此查询。

1

如果你想从现在和未来5天内的所有日期,您可以使用:

WHERE 
    `datedroppingoff` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY) OR 
    `datepickingup ` BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 5 DAY) 
+0

如何将它包装到for循环中以便它出现在日历div中? –

+3

可能最好这样做(不是循环查询,但是在您开始循环之前),然后处理结果集中的数据,而不是每天迭代运行查询。 –

1

让我们来看看我是怎么想继续(顺便说一下,这是我的第一个答案,所以我米有点兴奋......)

首先,这种超快脚本的一些基本假设(对不起,现在没有那么多的时间来测试它彻底)。

  • 您的预约表中有约会的日期
  • 你的结果资源($事件)字段只包含本期周
  • 结果行通过日期字段,按升序排序行(从最旧到最新)

试一下这个(我改了一下你的原码,抱歉)

// read first event 
if ($nextEvent = mysql_fetch_assoc($events)) { // here is the first one 
    extract($nextEvent); // prepare its variables 
    // use the event date it to control the inner loop 
    $nextDate = $DB_field_with_event_date; 
} else // no events? 
    $nextDate = 0; // prepare a fake date value 

// calculate today date 
$currentDate = mktime(); 

// loop on the dates for the next 7 days 
for ($i = 0 ; $i < 7; $i++) { 

    $currentEvents = ""; 

    // loop to print every event for current day (first one already extracted) 
    while ($nextDate == date("Y-m-d", $currentDate)) { // next event occurs today 

     // here prepare the var containing the event description 
     // BTW, I'd use a list for the events 
     $currentEvents .= "· $your_desc_field<br>"; // use every field you need from current DB row 

     // read next event 
     if ($nextEvent = mysql_fetch_assoc($events)) { // here is the next one 
      extract($nextEvent); // prepare its variables 
      // use the event date it to control the inner loop 
      $nextDate = $DB_field_with_event_date; 
     } else // no more events? 
      $nextDate = 0; // prepare a fake date value 
    } 

    echo " 
     <div class='datebox'> 
      <div class='topdate'>" . strtoupper(date("D d m Y", $currentDate)) . "</div> 
      <div class='bottomdate'>$currentEvents</div> 
     </div>"; 

    $currentDate = strtotime("+1 day", $currentDate); 
} 

试戴假数据几次,它应该工作。恕我直言,不如直接别名日期字段直接从DB获得nextDate变种,所以避免了行,

  $nextDate = $DB_field_with_event_date; 
+0

谢谢你,即时通讯只是要编辑我的问题,并添加SQL数据库查找,因为我认为即时通讯制作一团糟! :-S –

+0

脚本中存在一个问题:您获得的是所有约会,而不是本周的约会(第二个假设)。你应该使用其他答案之一。无论如何,如果你可以添加一个“jobdetails”的表转储,我想我也可以编写DB查找部分来集成在这里。 – Francis

+0

我总是毫不犹豫地在一个循环中插入一个数据库查询,我发现它很费时间并且系统超出范围(如果你有3个月的时间来显示,你可能会调用SQL服务器7次,太糟糕了)。通常我更喜欢得到正确的结果,然后遍历它们。 – Francis