如何扩展现有的 PHP 日历类

fcb1900

我正在使用startutorial 中的免费 PHP 日历类

private function _showDay($cellNumber)我开发了一个小函数,将实际一天涂成红色。

if(date("Y-m-d") == $this->currentDate) { $heute = "background-color: red;"; }

return '<li id="li-'.$this->currentDate.'" class="'.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                ($cellContent==null?'mask':''). '"style="'. $heute .'">'.$cellContent.'</li>';

这是有效的。

现在我想将 db 中的所有条目计数到实际的日期/日期。

我的日历条目的 mysql 数据库如下所示:

id | headline | text | timestamp (datum)

我的计数看起来像这样

$offen = "SELECT COUNT(*) AS anzahl 
          FROM info_info 
          WHERE `datum` > '2018-09-06 00:00:00' AND `datum` < '2018-09-06 23:59:59'";

$resultoffen = $con->query($offen);

    if ($resultoffen->num_rows > 0) {
        while($row = $resultoffen->fetch_assoc()) {
        $ungelesen = $row["anzahl"];
        echo $ungelesen;
        }
    }

我的完整自定义脚本如下所示:

<?php

class Calendar {  

    /**
     * Constructor
     */
    public function __construct(){     
        $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
    }

    /********************* PROPERTY ********************/  
    private $dayLabels = array("Mo","Di","Mi","Do","Fr","Sa","So");

    private $currentYear = 0;

    private $currentMonth = 0;

    private $currentDay = 0;

    private $currentDate = null;

    private $daysInMonth = 0;

    private $naviHref = null;

    /********************* PUBLIC **********************/  

    /**
    * print out the calendar
    */
    public function show() {
        $year  == null;

        $month == null;

        if(null==$year&&isset($_GET['year'])){

            $year = $_GET['year'];

        }else if(null==$year){

            $year = date("Y",time());  

        }          

        if(null==$month&&isset($_GET['month'])){

            $month = $_GET['month'];

        }else if(null==$month){

            $month = date("m",time());

        }                  

        $this->currentYear=$year;

        $this->currentMonth=$month;

        $this->daysInMonth=$this->_daysInMonth($month,$year);  

        $content='<div id="calendar">'.
                        '<div class="box">'.
                        $this->_createNavi().
                        '</div>'.
                        '<div class="box-content">'.
                                '<ul class="label">'.$this->_createLabels().'</ul>';   
                                $content.='<div class="clear"></div>';     
                                $content.='<ul class="dates">';    

                                $weeksInMonth = $this->_weeksInMonth($month,$year);
                                // Create weeks in a month
                                for( $i=0; $i<$weeksInMonth; $i++ ){

                                    //Create days in a week
                                    for($j=1;$j<=7;$j++){
                                        $content.=$this->_showDay($i*7+$j);
                                    }
                                }

                                $content.='</ul>';

                                $content.='<div class="clear"></div>';     

                        $content.='</div>';

        $content.='</div>';
        return $content;   
    }

    /********************* PRIVATE **********************/ 
    /**
    * create the li element for ul
    */

    private function _showDay($cellNumber){

        if($this->currentDay==0){

            $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));

            if(intval($cellNumber) == intval($firstDayOfTheWeek)){

                $this->currentDay=1;

            }
        }



        if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) ){

            $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));

            $cellContent = $this->currentDay;

            $this->currentDay++;   

        }else{

            $this->currentDate = null;

            $cellContent=null;
        }

        //aktuellen tag rot markieren
        if(date("Y-m-d") == $this->currentDate) { $heute = "background-color: red;"; }


        //anzahl der kalendereinträge
        $offen = "SELECT COUNT(*) AS anzahl 
          FROM info_info 
          WHERE `datum` > '".$this->currentDate." 00:00:00' AND `datum` < '".$this->currentDate." 23:59:59'";

        $resultoffen = $con->query($offen);

        if ($resultoffen->num_rows > 0) {
            while($row = $resultoffen->fetch_assoc()) {
            $ungelesen = $row["anzahl"];
            echo $ungelesen;
            }
        }

        //gib kalender zurück
        return '<li id="li-'.$this->currentDate.'" class="'.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                ($cellContent==null?'mask':''). '"style="'. $heute .'">'.$cellContent.'</li>';
    }

    /**
    * create navigation
    */
    private function _createNavi(){

        $nextMonth = $this->currentMonth==12?1:intval($this->currentMonth)+1;

        $nextYear = $this->currentMonth==12?intval($this->currentYear)+1:$this->currentYear;

        $preMonth = $this->currentMonth==1?12:intval($this->currentMonth)-1;

        $preYear = $this->currentMonth==1?intval($this->currentYear)-1:$this->currentYear;

        return
            '<div class="header">'.
                '<a class="prev" href="'.$this->naviHref.'?month='.sprintf('%02d',$preMonth).'&year='.$preYear.'">Zurück</a>'.
                    '<span class="title">'.date('m/Y',strtotime($this->currentYear.'-'.$this->currentMonth.'-1')).'</span>'.
                '<a class="next" href="'.$this->naviHref.'?month='.sprintf("%02d", $nextMonth).'&year='.$nextYear.'">Nächster</a>'.
            '</div>';
    }

    /**
    * create calendar week labels
    */
    private function _createLabels(){  

        $content='';

        foreach($this->dayLabels as $index=>$label){

            $content.='<li class="'.($label==6?'end title':'start title').' title">'.$label.'</li>';

        }

        return $content;
    }



    /**
    * calculate number of weeks in a particular month
    */
    private function _weeksInMonth($month=null,$year=null){

        if( null==($year) ) {
            $year =  date("Y",time()); 
        }

        if(null==($month)) {
            $month = date("m",time());
        }

        // find number of days in this month
        $daysInMonths = $this->_daysInMonth($month,$year);

        $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7);

        $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths));

        $monthStartDay = date('N',strtotime($year.'-'.$month.'-01'));

        if($monthEndingDay<$monthStartDay){

            $numOfweeks++;

        }

        return $numOfweeks;
    }

    /**
    * calculate number of days in a particular month
    */
    private function _daysInMonth($month=null,$year=null){

        if(null==($year))
            $year =  date("Y",time()); 

        if(null==($month))
            $month = date("m",time());

        return date('t',strtotime($year.'-'.$month.'-01'));
    }

}

但是该类没有加载。我没有输出。

在php日志文件中看到以下内容:

[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: year in C:\inetpub\wwwroot\info\class_calendar.php on line 33
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: month in C:\inetpub\wwwroot\info\class_calendar.php on line 35
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: year in C:\inetpub\wwwroot\info\class_calendar.php on line 37
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: year in C:\inetpub\wwwroot\info\class_calendar.php on line 41
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: month in C:\inetpub\wwwroot\info\class_calendar.php on line 47
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: month in C:\inetpub\wwwroot\info\class_calendar.php on line 51
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Notice:  Undefined variable: con in C:\inetpub\wwwroot\info\class_calendar.php on line 136
[13-Sep-2018 09:59:15 Europe/Berlin] PHP Fatal error:  Uncaught Error: Call to a member function query() on null in C:\inetpub\wwwroot\info\class_calendar.php:136
Stack trace:
#0 C:\inetpub\wwwroot\info\class_calendar.php(78): Calendar->_showDay(1)
#1 C:\inetpub\wwwroot\info\calendar.php(47): Calendar->show()
#2 {main}
  thrown in C:\inetpub\wwwroot\info\class_calendar.php on line 136

怎么了?

fcb1900

我让它工作了。这是我的自定义代码:

<?php

/**
*@author  Xu Ding
*@email   [email protected]
*@website http://www.StarTutorial.com
**/

class Calendar {  

    /**
     * Constructor
     */
    public function __construct(){     
        $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
        $this->con = new mysqli("localhost","XXXX","XXXX","XXXX");
    }

    /********************* PROPERTY ********************/  
    private $dayLabels = array("Mo","Di","Mi","Do","Fr","Sa","So");
    private $currentYear = 0;    
    private $currentMonth = 0;     
    private $currentDay = 0;     
    private $currentDate = null;     
    private $daysInMonth = 0;     
    private $naviHref = null;

    /********************* PUBLIC **********************/  

    /**
    * print out the calendar
    */
    public function show() {
        $year  == null;

        $month == null;

        if(null==$year&&isset($_GET['year'])){

            $year = $_GET['year'];

        }else if(null==$year){

            $year = date("Y",time());  

        }          

        if(null==$month&&isset($_GET['month'])){

            $month = $_GET['month'];

        }else if(null==$month){

            $month = date("m",time());

        }                  

        $this->currentYear=$year;

        $this->currentMonth=$month;

        $this->daysInMonth=$this->_daysInMonth($month,$year);  

        $content='<div id="calendar">'.
                        '<div class="box">'.
                        $this->_createNavi().
                        '</div>'.
                        '<div class="box-content">'.
                                '<ul class="label">'.$this->_createLabels().'</ul>';   
                                $content.='<div class="clear"></div>';     
                                $content.='<ul class="dates">';    

                                $weeksInMonth = $this->_weeksInMonth($month,$year);
                                // Create weeks in a month
                                for( $i=0; $i<$weeksInMonth; $i++ ){

                                    //Create days in a week
                                    for($j=1;$j<=7;$j++){
                                        $content.=$this->_showDay($i*7+$j);
                                    }
                                }

                                $content.='</ul>';

                                $content.='<div class="clear"></div>';     

                        $content.='</div>';

        $content.='</div>';
        return $content;   
    }

    /********************* PRIVATE **********************/ 
    /**
    * create the li element for ul
    */

    private function _showDay($cellNumber){

        if($this->currentDay==0){

            $firstDayOfTheWeek = date('N',strtotime($this->currentYear.'-'.$this->currentMonth.'-01'));

            if(intval($cellNumber) == intval($firstDayOfTheWeek)){

                $this->currentDay=1;

            }
        }



        if( ($this->currentDay!=0)&&($this->currentDay<=$this->daysInMonth) ){

            $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));

            $cellContent = $this->currentDay;

            $this->currentDay++;   

        }else{

            $this->currentDate = null;

            $cellContent=null;
        }

        //aktuellen tag rot markieren
        if(date("Y-m-d") == $this->currentDate) { $heute = "background-color: red;"; }

        //anzahl der kalendereinträge
        $offen = "SELECT COUNT(*) AS anzahl 
          FROM info_info 
          WHERE `datum` > '".$this->currentDate." 00:00:00' AND `datum` < '".$this->currentDate." 23:59:59'";       

        $resultoffen = $this->con->query($offen);
        if ($resultoffen->num_rows > 0) {
            while($row = $resultoffen->fetch_assoc()) {
            //$ungelesen = $row["anzahl"];
            if ($row["anzahl"] == "0") {
                $ungelesen = "";
            }
            else {
                //$ungelesen = $row['anzahl'];
                $eintrag = "background-color: green;";
            }
            }
        }

        //gib kalender zurück
        return '<li id="li-'.$this->currentDate.'" class="'.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                ($cellContent==null?'mask':''). ' "style="'.$heute.$eintrag.'">'.$cellContent.$ungelesen.'</li>';
    }

    /**
    * create navigation
    */
    private function _createNavi(){

        $nextMonth = $this->currentMonth==12?1:intval($this->currentMonth)+1;

        $nextYear = $this->currentMonth==12?intval($this->currentYear)+1:$this->currentYear;

        $preMonth = $this->currentMonth==1?12:intval($this->currentMonth)-1;

        $preYear = $this->currentMonth==1?intval($this->currentYear)-1:$this->currentYear;

        return
            '<div class="header">'.
                '<a class="prev" href="'.$this->naviHref.'?month='.sprintf('%02d',$preMonth).'&year='.$preYear.'">Zurück</a>'.
                    '<span class="title">'.date('m/Y',strtotime($this->currentYear.'-'.$this->currentMonth.'-1')).'</span>'.
                '<a class="next" href="'.$this->naviHref.'?month='.sprintf("%02d", $nextMonth).'&year='.$nextYear.'">Nächster</a>'.
            '</div>';
    }

    /**
    * create calendar week labels
    */
    private function _createLabels(){  

        $content='';

        foreach($this->dayLabels as $index=>$label){

            $content.='<li class="'.($label==6?'end title':'start title').' title">'.$label.'</li>';

        }

        return $content;
    }



    /**
    * calculate number of weeks in a particular month
    */
    private function _weeksInMonth($month=null,$year=null){

        if( null==($year) ) {
            $year =  date("Y",time()); 
        }

        if(null==($month)) {
            $month = date("m",time());
        }

        // find number of days in this month
        $daysInMonths = $this->_daysInMonth($month,$year);

        $numOfweeks = ($daysInMonths%7==0?0:1) + intval($daysInMonths/7);

        $monthEndingDay= date('N',strtotime($year.'-'.$month.'-'.$daysInMonths));

        $monthStartDay = date('N',strtotime($year.'-'.$month.'-01'));

        if($monthEndingDay<$monthStartDay){

            $numOfweeks++;

        }

        return $numOfweeks;
    }

    /**
    * calculate number of days in a particular month
    */
    private function _daysInMonth($month=null,$year=null){

        if(null==($year))
            $year =  date("Y",time()); 

        if(null==($month))
            $month = date("m",time());

        return date('t',strtotime($year.'-'.$month.'-01'));
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Typescript中扩展现有的类

来自分类Dev

在Python中扩展现有的类实例

来自分类Dev

如何在打字稿中扩展现有的类

来自分类Dev

扩展现有的类并添加clone()方法C#

来自分类Dev

Xamarin / C#扩展现有类

来自分类Dev

C# - 扩展现有框架密封类

来自分类Dev

如何扩展现有的自定义元素?

来自分类Dev

打字稿:如何扩展现有的模块定义?

来自分类Dev

如何在Laravel中扩展现有的外墙?

来自分类Dev

如何扩展现有的Docker映像?

来自分类Dev

如何扩展现有的zsh完成功能?

来自分类Dev

如何扩展现有的交换文件?

来自分类Dev

扩展现有的complexType

来自分类Dev

扩展现有的complexType

来自分类Dev

如何在现有的第三方扩展中重写/扩展Magento类

来自分类Dev

c# .net:使用初始化时应覆盖的方法扩展现有类

来自分类Dev

日历类如何计算月份?

来自分类Dev

如何在javascript中扩展现有的构造函数?

来自分类Dev

如何通过自适应支付扩展现有的贝宝?

来自分类Dev

在Docker Hub上扩展现有的Docker映像

来自分类Dev

扩展现有的Gradle任务并覆盖配置

来自分类Dev

扩展现有的蚂蚁路径标签

来自分类Dev

如何“扩展”现有类的现有方法?

来自分类Dev

如何“扩展”现有类的现有方法?

来自分类Dev

如何正确接口现有的密封类?

来自分类Dev

如何使用JMX监视现有的Java类?

来自分类Dev

如何从库中覆盖现有的核心类?

来自分类Dev

如何正确接口现有的密封类?

来自分类Dev

如何添加新的物理卷以扩展现有的LUKS加密的lvm(卷组)并维护加密?