Reupload Problème Calendrier PHP

Résolu
Bonjour, je fait un calendrier en PHP avec l'aide de la vidéo suivante :
https://www.youtube.com/watch?t=1896&v=qzE949YCYic&feature=youtu.be
(Je suis entre 33 min et 39 min)

J'obtiens cette erreur :


Quand je clique sur les détails de mes evenement:



Pouvez m'aider s'il vous plait

Mon Code est en Plusieurs Partie (Je n''est pas tout upload seulement le plus important )

index :
        <?php
use App\Date\Month;
use Calendar\Events;

        require_once('../src/bootstrap.php');
        require_once('../src/Calendar/Month.php');
        require_once('../src/Calendar/Events.php');
        
        $pdo = get_pdo();
        $events = new  Calendar\Events($pdo);
        $month = new Calendar\Month($_GET['month'] ?? null, $_GET['year'] ?? null);
        $start = $month->getStartingDay();
        $start = $start->format('N') === '1' ? $start : $month->getStartingDay() ->modify('last monday');
        $weeks = $month->getWeeks();
        $end = (clone $start)->modify('+'. (6 + 7 * ($weeks-1)) .' days');
        $events = $events->getEventsBetweenByDay($start, $end);
        require_once('../views/header.php');
        ?>

        <div class="d-flex flex-row align-items-center justify-content-between mx-sm-3">
        <h1><?= $month->toString(); ?></h1>
        <div>
            <a href="?month=<?= $month->previousMonth()->month; ?>&year=<?= $month->previousMonth()->year; ?>" class="btn btn-primary"><</a>
            <a href="?month=<?= $month->nextMonth()->month; ?>&year=<?= $month->nextMonth()->year; ?>" class="btn btn-primary">></a>
        </div>
        </div>

        <table class="calendar__table calendar__table--<?= $month->getWeeks(); ?>weeks">
            <?php for ($i = 0; $i < $month->getWeeks(); $i++) : ?>
                <tr>
                    <?php 
                    foreach($month->days as $k => $day): 
                        $date = (clone $start)->modify("+" . ($k + $i * 7). "days");
                        $eventsForDay = $events[$date->format("Y-m-d")] ?? [];
                    ?>
                    <td class="<?= $month->withinMonth($date) ? '' : 'calendar__othermonth'; ?>">
                        <?php if ($i === 0) : ?>
                            <div class="calendar__weekday"><?= $day; ?></div>
                        <?php endif; ?>
                        <div class="calendar__day"><?= $date->format('d'); ?></div>
                        <?php foreach ($eventsForDay as $event) : ?>
                            <div class="calendar__event">
                                <?= (new DateTime($event['start']))->format('H:i')?> - <a href="/calendrier/public/event.php?id=<?= $event['id']; ?>"><?= ($event['name']); ?></a>
                            </div>
                        <?php endforeach; ?>     
                    </td>
                    <?php endforeach; ?>
                </tr>
            <?php endfor; ?>     
        </table>
        <?php require_once('../views/footer.php'); ?>

----------------------------------------
event.php :
<?php
use App\Date\Month;
use Calendar\Events;
require_once('../src/bootstrap.php');
require_once('../src/Calendar/Events.php');
$pdo = get_pdo();
$events = new Calendar\Events($pdo);
if(!isset($_GET['id'])){
    header('location: /404.php');
}
try{
    
    $event = $events->find($_GET['id']);
}catch(\Exception $e){
    e404();
}
require_once('../views/header.php');
?>


<h1><?= ($event->getName()); ?></h1>
<ul>
<li>Date : <?= $event->getStart()->format('d/m/Y'); ?></li>
<li>Heure de début : <?= $event->getStart()->format('H:i'); ?></li>
<li>Heure de fin : <?= $event->getEnd()->format('H:i'); ?></li>
<li>Description :<br>
<?= H($event->getDescription()); ?>
</li>
</ul> 

<?php require_once('../views/footer.php'); ?>

-------------------------------------------

Events.php:

<?php 
namespace Calendar;

use PDO;

class Events {

    private $pdo;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * Récup les évenements entre deux dates
     * @param \DateTime $start
     * @param \DateTime $end
     * @return array
     */
    public function getEventsBetween(\DateTime $start, \DateTime $end): array{
        $sql = "SELECT * FROM events WHERE start BETWEEN '{$start->format('Y-m-d 00:00:00')}' AND '{$end->format('Y-m-d 23:59:59')}'" ;
        $statement = $this->pdo->query($sql);
        $results = $statement->fetchAll();
        return $results;
    }

    /**
     * Récup les évenements entre deux dates indexé par jour
     * @param \DateTime $start
     * @param \DateTime $end
     * @return array
     */
    public function getEventsBetweenByDay(\DateTime $start, \DateTime $end): array{
        $events = $this->getEventsBetween($start, $end);
        $days = [];
        foreach($events as $event){
            $date = explode(' ', $event['start'])[0];
            if(!isset($days[$date])){
                $days[$date] = [$event];
            }else{
                $days[$date][] = $event;
            }
        }
        return $days;
    }   
    
    /**
     * Récupére un évenement 
     * @param int $id
     * @return array
     * @throws \Exception
     */
    public function find (int $id): \Calendar\Event{ 
        require_once('Event.php');
        $statement = $this->pdo->query("SELECT * FROM events WHERE id = $id LIMIT 1");
        $statement->setFetchMode(\PDO::FETCH_CLASS, \Calendar\Event::class);
        $result = $statement->fetch();

        if($result === false){
            throw new \Exception('Aucun résultat a était trouvé');
        }
        $re = new \Calendar\Event($result['id'],$result['name'],$result['description'],$result['start'],$result['end']);
        return $re;
    }
}


Event.php

 <?php

namespace Calendar;

class Event {
    private $id;
    private $name;
    private $description;
    private $start;
    private $end;

    function __construct($id, $name, $description, $start, $end)
    {
        $this->$id=$id;
        $this->$name=$name;
        $this->$description=$description;
        $this->$start=$start;
        $this->$end=$end;
    }

    public function getId(): int{
        return $this->id;
    }

    public function getName(): string{
        return $this->name;
    }

    public function getDescription(): ?string{
        return $this->description;
    }

    public function getStart(): \DateTime{
        return new \DateTime($this->start);
    }

    public function getEnd(): \DateTime{
        return new \DateTime($this->end);
    }
}

?>


bootstrap.php :

 <?php
function e404(){
    require_once('../public/404.php');
    exit();
}

function dd($vars){
    foreach($vars as $var){
        echo "<pre>";
        print_r($var);
        echo "</pre>";
    }
} 
function get_pdo (): PDO{
    return new PDO('mysql:host=localhost;dbname=chat', 'root','', [
        PDO:: ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
        PDO:: ATTR_DEFAULT_FETCH_MODE => \PDO:: FETCH_ASSOC
    ]);
}

function h (?string $value): string{
    if($value == null){
        return '';
    }
    return htmlentities($value);
}

-------------------------
Month.php :

<?php
namespace Calendar;

class Month {
    public $days = ['Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi', 'Dmanche'];
    
    private $months = ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre'];
    public $month;
    public $year;

    /**
     * Month constructor. 
     * @param int $month Le mois compris entre 1 et 12
     * @param int $year l'année 
     * @throw \Exception
     */
    public function __construct(?int $month = null, ?int $year = null){
        if ($month === null || $month <1 || $month >12){
            $month = intval(date('m'));
        }
        if($year === null){
            $year = intval(date('Y'));
        }
        $this->month = $month;
        $this->year = $year;
    }

    /**
     * Renvoi le premier jour du mois
     * @return \DateTime
     */
    public function getStartingDay (): \DateTime{
        return new \DateTime("{$this->year}-{$this->month}-01");
    }

    /**
     * Retourne le mois en toute lettre
     * @return string 
     */
    public function toString(): string {
       return $this ->months[$this->month - 1] . ' ' . $this->year;
    }

    /**
     * Renvoi le nombre de semaine dans le mois
     * @return int
     */
    public function getWeeks(): int{
        $start = $this->getStartingDay();
        $end = (clone $start)->modify('+1 month -1 day');
        $weeks = intval($end->format('W')) - intval($start->format('W')) +1; 
        if ($weeks < 0){
            $weeks = intval($end->format('W'));
        }
        return $weeks;
    }

    /**
     * Le jour est dans le mois en cours ?
     * @param \DateTime $date
     * @return bool
     */
    public function withinMonth (\DateTime $date ): bool{
        return $this->getStartingDay()->format('Y-m') === $date->format('Y-m');
    }

    /**
     * Renvoi le mois suivant
     * @return Month
     */
    public function nextMonth(): Month {
        $month = $this->month + 1;
        $year = $this->year;
        if($month > 12){
            $month =1;
            $year += 1;
        }
        return new Month($month, $year);
    }

    /**
     * Renvoi le mois précédent
     * @return Month
     */
    public function previousMonth(): Month {
        $month = $this->month - 1;
        $year = $this->year;
        if($month < 1){
            $month =12;
            $year -= 1;
        }
        return new Month($month, $year);
    }
}


Configuration: Windows / Firefox 101.0

1 réponse

  1. Modérateur
    Bonjour,

    Tu as chargé le fichier Calendar\Events .. mais là .. il te parle du fichier Calendar\Event
    Il semble que tu aies donc oublié de le require.

    1. Bonjour Jordane je te remercie encore une fois pour tes réponses mais je suis désoler je n'arrive pas a trouver ou il manque un require. Pouvez vous détailliez ou je pourrais le rajouter ?
    2. Modérateur
      @mister431Le message d'erreur contient le numéro (ainsi que le nom du fichier ) de ligne concernée par le souci...
      il faut donc, dans ce fichier, require les DEUX fichiers demandés
       require_once('../src/Calendar/Events.php');
       require_once('../src/Calendar/Event.php');
      

      A moins que ce fichier n'existe pas dans ton répertoire .. et que tu as juste oublié de mettre un S au nom du fichier lors de son appel ?
      Ca .. il n'y a que toi qui pourras y répondre.
    3. @jordane45Bonjour Merci de votre aide il fallait bien rajouter ses require_once pour regler ce bug mais maintenant un bug récurent apparait après cela :


      Comment regler se bug array s'il vous plait ?
    4. Modérateur
      @mister431Nouveau problème = ouverture d'une nouvelle question.
      Merci.

      PS: Pense à mettre celle-ci en résolue.