Show 3 random recipes on home page

This commit is contained in:
Thomas Rubini 2023-01-25 16:14:46 +01:00
parent d356a4d816
commit 752796fab3
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
4 changed files with 31 additions and 27 deletions

View File

@ -5,26 +5,8 @@ final class DefaultController
public function defaultAction(Array $A_urlParams = null, Array $A_postParams = null)
{
$array_recipes = array(
array(
"RECIPE_LINK" => "/recipe/view/1",
"NAME" => "Pâte à crêpe",
"IMG_LINK" => "/static/img/recipes/1.jpg",
"NOTE" => "4.5"
),
array(
"RECIPE_LINK" => "/recipe/view/1",
"NAME" => "Pâte à crêpe",
"IMG_LINK" => "/static/img/recipes/1.jpg",
"NOTE" => "4.5"
),
array(
"RECIPE_LINK" => "/recipe/view/1",
"NAME" => "Pâte à crêpe",
"IMG_LINK" => "/static/img/recipes/1.jpg",
"NOTE" => "4.5"
),
);
View::show("home/view", $array_recipes);
$A_recipes = RecipeModel::getRandomRecipes(3);
View::show("home/view", array("RECIPES" => $A_recipes));
}
}

View File

@ -191,4 +191,23 @@ final class RecipeModel
return $A_recipes;
}
public static function getRandomRecipes($I_n)
{
$O_model = Model::get();
$stmt = $O_model->prepare("
SELECT * FROM RECIPE
ORDER BY RAND()
LIMIT :n
");
$stmt->bindParam("n", $I_n, PDO::PARAM_INT);
$stmt->execute();
$A_recipes = array();
foreach($stmt->fetchAll() as $row){
array_push($A_recipes, self::createFromRow($row, $row["ID"]));
}
return $A_recipes;
}
}

View File

@ -1,7 +1,10 @@
<a href="<?= $A_view["RECIPE_LINK"]?>">
<img src="<?= $A_view["IMG_LINK"] ?>" alt="<?= $A_view["NAME"]?>">
<?php
$O_recipe = $A_view["RECIPE"];
?>
<a href="<?= $O_recipe->getLink() ?>">
<img src="<?= $O_recipe->getImageLink() ?>" alt="<?= $O_recipe->S_NAME ?>">
<section>
<h2> <?= $A_view["NAME"]?> </h2>
<p> <?= $A_view["NOTE"]?> </p>
<h2> <?= $O_recipe->S_NAME ?> </h2>
<p> <?= $O_recipe->I_NOTE ?> </p>
</section>
</a>

View File

@ -7,8 +7,8 @@
<section>
<h1> Nos idées recettes: </h1>
<?php
foreach ($A_view as $recipe){
View::show("common/recipe", $recipe);
foreach ($A_view["RECIPES"] as $O_recipe){
View::show("common/recipe", array("RECIPE" => $O_recipe));
}
?>
</section>