仅突出显示日历中数据的天数

时间:2015-03-25 13:11:37

标签: php loops foreach while-loop calendar

需要帮助,请堆叠器。在一定程度上工作,但陷入无限循环并超时。显示所选月份的天数,我需要签入数据库并用不同的类突出显示有数据的那些。

我知道有一些类似的脚本,但这是响应式的,在sqlite上运行,不使用表。

这是我得到的地方:

#############################################################
$months31 = array("01","03","05","07","08","10","12"); // MONTHS WITH 31 DAYS
if($_SESSION['m'] == 02): // IF FEBRUARY
$daysinmonth = cal_days_in_month (CAL_GREGORIAN , $_SESSION['m'] , $_SESSION['y']); // CHECK DAYS IN FEBRUARY AND CHECK FOR LEAP YEAR
elseif(in_array($_SESSION['m'],$months31 )): // IF IN ARRAY OF MONTHS WITH 31 DAYS
$daysinmonth = 31; // DAYS IN MONTH = 31
else:
$daysinmonth = 30;  // DAYS IN MONTH = 30
endif; 
   ########## ADD DAYS OF SELECTED MONTH ############
$d = 1;
$day = $d ;
$selectedmonth = $_SESSION['m'];
$selectedyear = $_SESSION['y']; 
##################################################
while($d <= $daysinmonth):
####################### GET DATA FROM DATABASE FOR SELECTED YEAR AND MONTH #################
$calshow = $caldb->query("SELECT day FROM calandar WHERE month = '$selectedmonth' AND year = '$selectedyear' ");
foreach($calshow as $cal):
$showday = $cal['day'];


############   HIGHLIGHTS CURRENT DAY ############
if($d == $curday && $_SESSION['m']== $curmonth && $_SESSION['y'] == $curyear):
$today = 'sel'; 
else:
 $today = ''; 
 endif; ?>


<a href="####">
  <li class="day <?php echo $today;?><?php if($showday == $d): echo 'data'; endif;?>"><?php echo $d++;?></li></a>
<?php endforeach; 
endwhile;?>

2 个答案:

答案 0 :(得分:0)

这是你的问题:

while($d <= $daysinmonth):

您没有在循环中增加$d,因此永远不会结束。除了我也看不到你设置$daysinmonth的位置。

您可能希望以这样的方式结束循环:

$d++;
endwhile;?>

或者您可能需要在$d循环中重置并增加foreach()

答案 1 :(得分:0)

Hurray排序!!!在db中有一行,时间戳为y-m-d,然后执行rowcount 对于当月的日期戳记记录 由于我使用的是SQLITE和PDO,我需要使用$ rows = $ calshow - &gt; fetchAll(); 和查询使用count($ rows)!= 0 - 当你知道如何简单; - )

这里的工作脚本http://i3ry.com/cal/如果它对任何有相同问题的人都有用(记录良好)

$day = $d ;
##################################
while($d <= $daysinmonth):
$selectedmonth = $_SESSION['m'];
$selectedyear = $_SESSION['y'];
$datestamp = $selectedyear.'-'.$selectedmonth.'-'.$d;
### check if there are appointments ###
$calshow = $caldb->query("SELECT datestamp FROM calandar WHERE datestamp = '$datestamp' AND month = '$selectedmonth' AND year = '$selectedyear'");
$rows = $calshow ->fetchAll(); // COUNT ROWS WHICH MATCH DATESTAMP QUERY

### GET DATA FROM DATABASE FOR SELECTED YEAR AND MONTH ###
if($d == $curday && $_SESSION['m']== $curmonth && $_SESSION['y'] == $curyear &&  count($rows)!= 0):
$daycss = 'tdyes';
elseif ($d == $curday && $_SESSION['m']== $curmonth && $_SESSION['y'] == $curyear):
$daycss = 'tdno';
elseif (count($rows) != 0):
$daycss = 'dyes';
else:
$daycss = 'dno';
endif;
### echo count($rows); ?> 

<a href="?sh=k&ds=<?php echo $_SESSION['y']."-". $_SESSION['m']."-". $d?>&m=<?php echo $_SESSION['m'];?>&y=<?php echo $_SESSION['y'];?>"><li class="day <?php echo $daycss;?>"><?php echo $d;?></li></a>
<?php  $d++; endwhile; ?>