-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_recipes.php
61 lines (49 loc) · 2.15 KB
/
fetch_recipes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
session_start();
$host = 'localhost';
$port = 3309;
$db = 'recipe_finder';
$user = 'root';
$pass = '';
$conn = new mysqli($host, $user, $pass, $db, $port);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['ingredients'])) {
$ingredients = filter_input(INPUT_POST, 'ingredients', FILTER_SANITIZE_STRING);
$ingredientArray = array_map('trim', explode(',', $ingredients));
$placeholders = implode(',', array_fill(0, count($ingredientArray), '?'));
$sql = "SELECT r.id, r.name, r.instructions, r.cooking_time, r.servings
FROM recipes r
JOIN recipe_ingredients ri ON r.id = ri.recipe_id
JOIN ingredients i ON ri.ingredient_id = i.id
WHERE i.name IN ($placeholders)
GROUP BY r.id
HAVING COUNT(DISTINCT i.id) = ?";
$stmt = $conn->prepare($sql);
$paramTypes = str_repeat('s', count($ingredientArray)) . 'i';
$params = array_merge($ingredientArray, [count($ingredientArray)]);
$stmt->bind_param($paramTypes, ...$params);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$recipeName = htmlspecialchars($row['name']);
$instructions = htmlspecialchars($row['instructions']);
$cookingTime = htmlspecialchars($row['cooking_time']);
$servings = htmlspecialchars($row['servings']);
echo '<div class="recipe-item">';
echo "<h4>$recipeName</h4>";
echo "<p>$instructions</p>";
echo "<p><strong>Cooking Time:</strong> $cookingTime minutes</p>";
echo "<p><strong>Servings:</strong> $servings</p>";
echo "<button class='download-recipe-btn' data-name='$recipeName' data-instructions='$instructions' data-cooking-time='$cookingTime' data-servings='$servings'>Download $recipeName</button>";
echo '</div>';
}
} else {
echo '<p>No recipes found for the provided ingredients.</p>';
}
$stmt->close();
}
$conn->close();
?>