Problème PHP Calendrier (Cannot Redeclare)

Résolu/Fermé
mister431 Messages postés 56 Date d'inscription jeudi 19 novembre 2020 Statut Membre Dernière intervention 24 juin 2022 - Modifié le 13 juin 2022 à 15:04
jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 - 13 juin 2022 à 15:52
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



J'essaye de suivre la vidéo mais impossible de faire pareil avec une erreur que la personne n'obtient pas (39 min)
comment je peut régler cela voici mon code en 4 partie si vous souhaiter plus de détails n'hésiter pas

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 '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é');
}
return $result;
}
}

Event.php : <?php

namespace Calendar;

class Event {
private $id;
private $name;
private $description;
private $start;
private $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 ('../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);
}

event.php :
<?php
use App\Date\Month;
use Calendar\Events;
require('../src/bootstrap.php');
require ('../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']);
var_dump($event);
}catch(\Exception $e){
echo "ff";
e404();
}
require('../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('../views/footer.php'); ?>

Index.php

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

require ('../src/bootstrap.php');
require ('../src/Calendar/Month.php');
require ('../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('../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('../views/footer.php'); ?>

Configuration: Configuration: Windows / Firefox 101.0
A voir également:

1 réponse

jordane45 Messages postés 38145 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 25 avril 2024 4 650
13 juin 2022 à 15:52
Bonjour,

Déjà.. merci de poster ton code sur le forum en utilisant les BALISES DE CODE
Explications disponibles ici : https://forums.commentcamarche.net/forum/affich-37598670-mise-en-forme-du-forum-et-des-fiches-pratiques-ccm#les-codes-sources


Ensuite, au lieu de faire des require .. fais des require_once

0