dongmu2174 2012-09-21 07:46 采纳率: 100%
浏览 123
已采纳

突出显示日历中的当前日期

I have a PHP code that generates a calendar. I'm trying to highlight the current date in this following code:

<?php
require("aacfs.php");

define("ADAY", (60*60*24));

if ((!isset($_POST['month'])) || (!isset($_POST['year']))) {
    $nowArray = getdate();
    $month = $nowArray['mon'];
    $year = $nowArray['year'];
} else {
    $month = $_POST['month'];
    $year = $_POST['year'];
}
$start = mktime(12,0,0,$month,1,$year);
$firstDayArray = getdate($start);
?>
<html>
<head>
<title><?php echo "Calendar: ".$firstDayArray['month']." " . $firstDayArray['year']; ?></title>
</head>
<script type="text/javascript">
function eventWindow(url) {
    var width = 500;
    var height = 500;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    event_popupWin = window.open(url, "subWind", windowFeatures, "POS", 'event', 'resizable=yes,scrollbars=yes,toolbar=no,width=400,height=400');
    event_popupWin.opener = self;
}
</script>
<script type="text/javascript">

    function hiliteToday(){
        var nDate = new Date();
        nDate = nDate.getDate();
        var nTable = document.getElementById('currMonth');
        nRows = nTable.rows.length;
        nCells = nTable.rows[0].cells.length;
        for (i=0; i<nRows; i++)
            {
             for (n=0; n<nCells; n++)
                {
                 var tmp = nTable.rows[i].cells[n].innerHTML;
                 tmp = tmp.split("<");
                 if (tmp[0] == nDate){nTable.rows[i].cells[n].style.backgroundColor = 'cyan'}
                }

            }
    }

    window.onload=hiliteToday;

</script>
<body>
<h1><center>Select a Month/Year</center></h1>
<center><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="month">
<?php
$months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

for ($x=1; $x<=count($months); $x++){
    echo "<option value=\"$x\"";
    if ($x == $month){
        echo " selected";
    }
    echo ">".$months[$x-1]."</option>";
}
?>
</select>
<select name="year">
<?php
for ($x=2012; $x<=2027; $x++){
    echo "<option";
    if ($x == $year){
        echo " selected";
    }
    echo ">$x</option>";
}
?>
</select>
<input type="submit" name="submit" value="Go">
</form></center>
<br />
<?php
$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<center><table border=\"1\" cellpadding=\"5\" id=\"currMonth\"><tr>
";
foreach ($days as $day) {
    echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
          <strong>$day</strong></td>
";
}

for ($count=0; $count < (6*7); $count++) {
    $dayArray = getdate($start);
    if (($count % 7) == 0) {
        if ($dayArray["mon"] != $month) {
            break;
        } else {
            echo "</tr><tr>
";
        }
    }
    if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
        echo "<td>&nbsp;</td>
";
    } else {
        $chkEvent_sql = "SELECT acode FROM reservation WHERE month(etd) = '".$month."' AND dayofmonth(etd) = '".$dayArray["mday"]."' AND year(etd) = '".$year."' ORDER BY etd";
        $chkEvent_res = mysql_query($chkEvent_sql) or die(mysql_error());

        if (mysql_num_rows($chkEvent_res) > 0) {
            $event_title = "<br/>";
            while ($ev = mysql_fetch_array($chkEvent_res)) {
                $event_title .= stripslashes($ev["acode"])."<br/>";
            }
            mysql_free_result($chkEvent_res);
        } else {
            $event_title = "";
        }

        echo "<td valign=\"top\"><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</td>
";

        unset($event_title);

        $start += ADAY;
    }
}
echo "</tr></table></center>";
mysql_close();
?>
</br><center><a href="javascript:self.close()"><font face="consolas"><b>Close</b></font></a></center>
</body>
</html>

but it doesn't seem to work. The javasript function hiliteToday() is supposed to do work since I'm binding it in the <table id='currMonth'> but I don't get any result. What am I doing wrong? Please help me. Thanks in advance!

EDIT: Here's how the calendar looks like:

enter image description here

  • 写回答

2条回答 默认 最新

  • duankang5882 2012-09-21 08:07
    关注

    You are checking with if (tmp[0] == nDate), if the table cell has the current day. In example "21" for today. But you never set only days to your table cells.

    Change your 2nd for-loop to:

             for (n=0; n<nCells; n++)
                {
                 var tmp = nTable.rows[i].cells[n].innerHTML;
                 if (tmp.search(nDate)!=-1){nTable.rows[i].cells[n].style.backgroundColor = 'cyan'}
                }
    

    Your content of each cell is something like

    <a href="">DAYOFMONTH</a>Title
    

    You are splitting for ">" now, so you got your day of month in tmp[1]. With "search" you search in a string for an regular expression.

    EDIT:

    <script type="text/javascript">
    
        function hiliteToday(){
            var nDate = new Date();
            var sDate = nDate.getMonth()+1 + "_" + nDate.getDate();
            document.getElementById(sDate).style.backgroundColor = 'cyan';
        }
    
    window.onload=hiliteToday;
    
    </script>
    

    Change your PHP echo to:

    echo "<td id=\"".$month."_".$dayArray["mday"]."\" valign=\"top\"><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>asdas</td>
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体