KimLamers / educom-verrukkulluk

0 stars 0 forks source link

Als je een recept per Id ophaalt krijg je of 1 recept of NULL terug #21

Open JeroenHeemskerk opened 1 year ago

JeroenHeemskerk commented 1 year ago

In de code hieronder gebruik je een while loop terwijl er altijd maar 0 of 1 in zit. https://github.com/KimLamers/educom-verrukkulluk/blob/b9e68bddebd2c360b913c2447cd426c0e78d1857/lib/recipe.php#L44-L78

In dit soort situaties is het gebruikerlijker om gelijk het ene recept terug te geven of anders NULL;

public function selectRecipeById($recipe_id) { 
     // clean data 
     $recipe_id = mysqli_real_escape_string($this->connection, $recipe_id); 

     // sql query 
     $sql = "SELECT * FROM recipe WHERE id = $recipe_id"; 
     $result = mysqli_query($this->connection, $sql); 

     // sql query ingredients 
     $sql_ingredients = "SELECT * FROM ingredients WHERE recipe_id = $recipe_id"; 
     $resuls_ingredients = mysqli_query($this->connection, $sql_ingredients); 

     $recipe  = mysqli_fetch_array($result));
     if (!recipe) { 
        return NULL;
     } 
     // get user 
     $user = $this->selectUser($recipe['user_id'], MYSQLI_ASSOC); 
     // get kitchen 
     $kitchen = $this->selectKitchenType($recipe['kitchen_id'], MYSQLI_ASSOC); 
     // get type 
     $type = $this->selectKitchenType($recipe['type_id'], MYSQLI_ASSOC); 

     $recipeArray = [ 
             "recipe_id" => $recipe['id'], 
             /* ... */
     ];
     return $recipeArray;
}