2013-03-07 65 views
0

我想简单的PHP日历。 例如:blog.tiger.com.pl/wp-content/uploads/2012/03/gcalendar.png 但是只有一天,例如:6/26。简单的PHP日历

我:

$hours = range(1, 24); 

和:

显示这个时间用HTML表格:

<table> 
<?php foreach($hours as $hour){ ?> 
    <tr><td><?php echo $hour ?> </td></tr> 
<?php } ?> 
</table> 

,我有:

$reservations = array(array('name' => 'first reservation for user Paul', 'from' => 6, 'to' => 8), 
        array('name' => 'second reservation for my group', 'from' => 11, 'to' => 14) ); 

但我不t知道如何添加$reservations我的foreach与$hours。如果是保留,我希望从6到8和11到14以及6和8之间显示来自索引名称的文本的红色背景,11到14之间显示来自索引名称的文本。最好的方法是使用这个文本的rowspan,但是怎么做?

+1

的谷歌日历犯规的符合一个'简单的日历上的图像'。在一个特定的小时内是否可以有多个事件?还是只是保留了? – 2013-03-07 10:28:25

+1

您可能需要的小时数为0到23,而不是1到24.在您的foreach($ hours as $ hour)中,您需要遍历'$ reservations'并检查预留是否与小时匹配,如果是,添加'​​'并使用HTML(或CSS)为背景着色。如果不是,你仍然需要一个'​​' – Mawg 2013-03-07 10:29:13

回答

1

我会尝试建立的数组“繁忙时间”,这样的事情:

(编辑以包括文本)

<?php 
$busyhours = array; 
foreach ($reservations as $reservation) { 
    $hours = $reservation['to'] - $reservation['from']; 
    while ($hours) { 
     $busyhours[] = $reservation['to'] + $hours; 
     $hours--; 
    } 
    $busyhours[$reservation['to']] = $reservation['name']; //made the busy hour the key. 
} 
?> 

<table> 
    <?php foreach($hours as $hour){ ?> 
     <tr><!-- changed in_array() to array_key_exists b/c of above change --> 
      <td <?php if (array_key_exists($hour, $busyhours)) { echo "class='red-bg'"; } ?> > 
       <?php 
        echo $hour; 
        echo "<p>" . $busyhours[$hour] . "</p>"; 
       ?> 
      </td> 
     </tr> 
    <?php } ?> 
</table> 
+0

emoty谢谢,但我怎么能为此添加文本? – 2013-03-07 10:43:26

+0

我做了一些改变,我认为这会工作:) – Bill 2013-03-07 11:00:02