Created
November 8, 2017 12:20
-
-
Save smwhr/d0fd50e6003af0aec1975cfda7277d1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Capsule\Manager as Capsule; | |
require_once("bootstrap.php"); | |
/// manière naïve et rapide mais pas efficace | |
/* | |
$film = Film::get($slug); //1 | |
$seances = $film->getAllSeances(); //1 | |
foreach($seances as $seance){ | |
$cinema = $seance->getCinema(); | |
} | |
*/ | |
//http://troisyaourts.com/~smwhr/supinternet/film_seance_cinema.sql | |
// TODO : faire le moins de requête possible | |
/** | |
* @in : | |
- date du jour | |
- listes des cinemas, films, séances (ou structure de donnée) | |
- film (id) | |
* @out : | |
- liste des cinémas diffusant le film dans le futur | |
> afficher les séances si il y en a cette semaine (= jusqu'au mercredi suivant) | |
> afficher la date de la première prochaine séance | |
**/ | |
/* | |
Conseils : | |
- séparer l'affichage de l'algorithme lui-même | |
- partir de la boucle d'affichage | |
*/ | |
$film_id = $argv[1]; | |
$today = date("Y-m-d"); | |
$week = [ | |
"2017-11-08", //Me | |
"2017-11-09", //Je | |
"2017-11-10", //Ve | |
"2017-11-11", //Sa | |
"2017-11-12", //Di | |
"2017-11-13", //Lu | |
"2017-11-14", //Ma | |
]; | |
$lastday = end($week); | |
// QUERY | |
$seances = Capsule::select( | |
"SELECT film_id, jour, start_horaire, cinema_id, name | |
FROM `seance` s | |
JOIN cinema c ON c.id = s.cinema_id | |
WHERE s.film_id = ? | |
AND s.jour >= ? | |
ORDER BY c.name ASC, s.jour ASC, s.start_horaire ASC | |
", | |
[$film_id, $today] | |
); | |
// TRAITEMENT | |
$group_by_cinema = []; | |
foreach($seances as $seance){ | |
$cinema_id = $seance->cinema_id; | |
// je vérifie si le cinéma existe déjà dans ma liste | |
if ( !isset($group_by_cinema[$cinema_id])){ | |
$group_by_cinema[$cinema_id] = [ | |
"name" => $seance->name, | |
"next" => null, | |
"seances" => [] | |
]; | |
} | |
$seance_jour = $seance->jour; | |
if($seance_jour > $lastday){ | |
if(is_null($group_by_cinema[$cinema_id]["next"])){ | |
$group_by_cinema[$cinema_id]["next"] = $seance_jour; | |
} | |
continue; | |
} | |
if ( !isset( | |
$group_by_cinema[$cinema_id]["seances"][$seance_jour] | |
)){ | |
$group_by_cinema[$cinema_id]["seances"][$seance_jour] = []; | |
} | |
$group_by_cinema[$cinema_id]["seances"][$seance_jour][] = substr($seance->start_horaire, 0, 5); | |
} | |
/* | |
DESIRED DATA STRUCTURE | |
$group_by_cinema = [ | |
"24" => ["name" => "Comoedia", | |
"seances" => [ | |
"2017-11-11" => ["10:55", "15:00"], | |
"2017-11-12" => ["10:55"], | |
] | |
], | |
"57" => ["name" => "Le Méliès", | |
"seances" => [], | |
"next" => "2017-11-29" | |
], | |
"1" => ["name" => "Studio des Ursulines", | |
"seances" => [ | |
"2017-11-08" => ["17:30"], | |
"2017-11-13" => ["17:30"] | |
] | |
], | |
];*/ | |
// AFFICHAGE | |
foreach($group_by_cinema as $cinema){ | |
echo $cinema["name"]."\n"; | |
if(empty($cinema["seances"])){ | |
echo "Pas de séance avant le ".$cinema["next"]; | |
echo "\n"; | |
}else{ | |
foreach($week as $day){ | |
echo " ".$day; | |
if(isset($cinema["seances"][$day])){ | |
echo " : ". | |
implode(", ",$cinema["seances"][$day]); | |
}else{ | |
echo " pas de séance"; | |
} | |
echo "\n"; | |
} | |
} | |
echo "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment