IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

PHP & Base de données Discussion :

Calendrier avec affichage réservations [MySQL]


Sujet :

PHP & Base de données

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de legrandse
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Décembre 2010
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 354
    Par défaut Calendrier avec affichage réservations
    Bonjour à tous,

    J'ai un soucis de compréhension avec une classe php qui affiche un simple calendrier.

    Je souhaiterais afficher en vert les jours qui sont occupé par une réservation.

    Pour me faciliter la compréhension de la classe, je crée un array avec 2 enregistrements 2017-11-15 et 2017-11-20
    Mais pour une raison que j'ignore, le calendrier ne m'affiche en vert que le premier enregistrement de mon array.
    Je fais appel à vous pour m'aider à y voir plus clair


    Voici la classe php:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    <?php
    /**
    *@author  Xu Ding
    *@email   thedilab@gmail.com
    *@website http://www.StarTutorial.com
    **/
     
     
    class Calendar {  
     
        /**
         * Constructor
         */
        public function __construct(){     
            $this->naviHref = htmlentities($_SERVER['PHP_SELF']);
        }
     
        /********************* PROPERTY ********************/  
        private $dayLabels = array("Mon","Tue","Wed","Thu","Fri","Sat","Sun");
     
        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;
            }
     
    		// essai compréhension	 		
    		$arr = array('2017-11-15','2017-11-20');
    		foreach($arr as $date) {
     
                            if ($date == $this->currentDate) {		
     
            	            return '<a href="../addbooking.php?arrival='.$this->currentDate.'"><li id="li-'.$this->currentDate.'" style="background-color:green;"  class="'.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':' ')).
                               ($cellContent==null?'mask':'').'">'.$cellContent.'</li></a>';
    		        }
    		        else {
    			    return '<li id="li-'.$this->currentDate.'" class="'.($cellNumber%7==1?' start ':($cellNumber%7==0?' end ':'  ')).
                               ($cellContent==null?'mask':'').'">'.$cellContent.'</li>';
    			}
    		}//fin foreach
        }
     
        /**
        * 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.'">Prev</a>'.
                        '<span class="title">'.date('Y M',strtotime($this->currentYear.'-'.$this->currentMonth.'-1')).'</span>'.
                    '<a class="next" href="'.$this->naviHref.'?month='.sprintf("%02d", $nextMonth).'&year='.$nextYear.'">Next</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'));
     
        }
     
    }

  2. #2
    Invité de passage
    Inscrit en
    Mars 2008
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Mars 2008
    Messages : 1
    Par défaut il faut converture les dates en datetime pour bien faire les tests
    je pense que le problème vient du test if ($date == $this->currentDate) { essaie couverture les dates en datetime puis refait les tests.

  3. #3
    Expert confirmé
    Avatar de rawsrc
    Homme Profil pro
    Dev indep
    Inscrit en
    Mars 2004
    Messages
    6 142
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Dev indep

    Informations forums :
    Inscription : Mars 2004
    Messages : 6 142
    Billets dans le blog
    12
    Par défaut
    Salut,

    Je comprends mal l'utilité de cette classe dans la mesure où son implémentation ne te laisse pas la possibilité de paramétrer le rendu à la volée...
    Si pour pouvoir utiliser cette classe t'es obligé de coder tes données en dur dedans, m'est d'avis que t'es pas sorti de l'auberge.

  4. #4
    Membre éclairé Avatar de legrandse
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Décembre 2010
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 354
    Par défaut
    J'ai en comparé les valeurs comme ceci sachant que:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $this->currentDate = date('Y-m-d',strtotime($this->currentYear.'-'.$this->currentMonth.'-'.($this->currentDay)));
    J'ai donc adapté ma comparaison :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    $date = date('Y-m-d',strtotime($date));
    	if ($date == $this->currentDate)
    un
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    echo $this->currentDate
    me donne :
    2017-11-01
    2017-11-02
    2017-11-03
    ...

    un me donne uniquement :
    2017-11-15
    2017-11-15
    2017-11-15
    2017-11-15
    ...
    35 fois. Soit le nombre de case du calendrier.

    Je ne comprend pas pourquoi la boucle ne m'affiche pas le second enregistrement de l'array ?

  5. #5
    Modératrice
    Avatar de Celira
    Femme Profil pro
    Développeuse PHP/Java
    Inscrit en
    Avril 2007
    Messages
    8 633
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeuse PHP/Java
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2007
    Messages : 8 633
    Par défaut
    ça dépend probablement de là où tu as mis ton echo.

    Cela dit, comme rawsrc, je ne vois pas bien l'intérêt de cette boucle. Qu'est-ce que tu tentes de faire exactement ?
    Modératrice PHP
    Aucun navigateur ne propose d'extension boule-de-cristal : postez votre code et vos messages d'erreurs. (Rappel : "ça ne marche pas" n'est pas un message d'erreur)
    Cherchez un peu avant poser votre question : Cours et Tutoriels PHP - FAQ PHP - PDO une soupe et au lit !.

    Affichez votre code en couleurs : [CODE=php][/CODE] (bouton # de l'éditeur) et [C=php][/C]

  6. #6
    Membre éclairé Avatar de legrandse
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Décembre 2010
    Messages
    354
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Décembre 2010
    Messages : 354
    Par défaut
    Le but final c'est de comparer les dates du calendrier à celles de ma BDD qui contient des événements.

    Et s'il y a effectivement une égalité alors afficher en vert.

    Mais pour tester j'ai simplement utilisé un array avec 2 dates.
    Actuellement seul le 15/11 est affiché en vert alors qu'il devrait y avoir le 20/11 également.
    Mais je ne vois pas pourquoi.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2007] Affichage d'un calendrier avec plusieurs dates en surbrillance
    Par damsmut dans le forum IHM
    Réponses: 0
    Dernier message: 22/03/2011, 15h36
  2. Réponses: 16
    Dernier message: 07/10/2010, 12h03
  3. Problmème sur liste avec affichage Calendrier
    Par HGDL1 dans le forum SharePoint
    Réponses: 0
    Dernier message: 28/09/2007, 10h51
  4. Cocher une case avec affichage automatique
    Par Toff !!!!! dans le forum Access
    Réponses: 3
    Dernier message: 27/09/2005, 13h36
  5. Probleme avec affichage de date
    Par Wongmaster dans le forum Access
    Réponses: 27
    Dernier message: 24/12/2004, 20h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo