From 752796fab323070b8c9e58a69f3f16827afa3573 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Wed, 25 Jan 2023 16:14:46 +0100 Subject: [PATCH] Show 3 random recipes on home page --- Controllers/DefaultController.php | 24 +++--------------------- Models/RecipeModel.php | 19 +++++++++++++++++++ Views/common/recipe.php | 11 +++++++---- Views/home/view.php | 4 ++-- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/Controllers/DefaultController.php b/Controllers/DefaultController.php index 04045d8..b97cf75 100644 --- a/Controllers/DefaultController.php +++ b/Controllers/DefaultController.php @@ -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)); } } diff --git a/Models/RecipeModel.php b/Models/RecipeModel.php index afcef50..8c4e6d9 100644 --- a/Models/RecipeModel.php +++ b/Models/RecipeModel.php @@ -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; + } } diff --git a/Views/common/recipe.php b/Views/common/recipe.php index 7b46cc1..a11cdf0 100644 --- a/Views/common/recipe.php +++ b/Views/common/recipe.php @@ -1,7 +1,10 @@ -"> - " alt=""> + + + <?= $O_recipe->S_NAME ?>
-

-

+

S_NAME ?>

+

I_NOTE ?>

diff --git a/Views/home/view.php b/Views/home/view.php index 71e6826..7e2f6ab 100644 --- a/Views/home/view.php +++ b/Views/home/view.php @@ -7,8 +7,8 @@

Nos idées recettes:

$O_recipe)); } ?>