-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_search.php
129 lines (111 loc) · 4.96 KB
/
category_search.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
session_start();
// Function to establish a database connection
function connectToDatabase()
{
$host = 'localhost';
$dbname = 'recipe_login';
$db_username = 'root';
$db_password = 'root';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $db_username, $db_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
} catch (PDOException $e) {
// Handle database connection errors
echo "Connection failed: " . $e->getMessage();
die();
}
}
if (isset($_GET['category']) && $_GET['category'] != '') {
$pdo = connectToDatabase();
$category = $_GET['category'];
echo "<h2>Recipe(s) in the " . htmlspecialchars($category) . " category </h2><hr>";
$recipe_sql = "SELECT recipe_id FROM recipe_categories WHERE Category = :category";
$recipe_stmt = $pdo->prepare($recipe_sql);
$recipe_stmt->bindParam(":category", $category, PDO::PARAM_STR);
$recipe_stmt->execute();
$recipe_result = $recipe_stmt->fetchAll(PDO::FETCH_ASSOC);
if ($recipe_result) {
foreach($recipe_result as $row) {
echo "Recipe ID: " . $row["recipe_id"] . "<br>";
// Fetch category details
$category_sql = "SELECT Category FROM recipe_categories WHERE recipe_id = :recipe_id";
$category_stmt = $pdo->prepare($category_sql);
$category_stmt->bindParam(":recipe_id", $row["recipe_id"], PDO::PARAM_INT);
$category_stmt->execute();
$category_result = $category_stmt->fetchAll(PDO::FETCH_ASSOC);
if ($category_result) {
echo "Categories: ";
$categories = array();
foreach ($category_result as $category_row) {
$categories[] = $category_row['Category'];
}
echo implode(", ", $categories); // use implode to join with a comma and space
echo "<br>";
} else {
echo "No categories found for this recipe id.<br><br>";
}
// Fetch recipe name, description, prep_time, cook_time and rating
$details_sql = "SELECT * FROM recipes WHERE recipe_id = :recipe_id";
$details_stmt = $pdo->prepare($details_sql);
$details_stmt->bindParam(":recipe_id", $row["recipe_id"], PDO::PARAM_INT);
$details_stmt->execute();
$details_result = $details_stmt->fetch(PDO::FETCH_ASSOC);
if ($details_result) {
foreach ($details_result as $column_name => $column_value) {
if ($column_name != 'recipe_id') {
if ($column_name == 'Name') {
echo "<h3>" . $column_value . "</h3>";
} else {
echo $column_name . ": " . $column_value . "<br>";
}
}
}
} else {
echo "No details found for this recipe id.<br>";
}
// Fetch recipe ingredients
$ingredient_sql = "SELECT * FROM recipe_ingredients WHERE recipe_id = :recipe_id";
$ingredient_stmt = $pdo->prepare($ingredient_sql);
$ingredient_stmt->bindParam(":recipe_id", $row["recipe_id"], PDO::PARAM_INT);
$ingredient_stmt->execute();
$ingredient_result = $ingredient_stmt->fetchAll(PDO::FETCH_ASSOC);
if ($ingredient_result) {
echo "<h4>Ingredients:</h4>";
foreach ($ingredient_result as $ingredient_row) {
unset($ingredient_row['recipe_id']); // remove recipe_id
echo implode(", ", $ingredient_row) . "<br>";
}
} else {
echo "No ingredients found for this recipe id.<br>";
}
// Fetch recipe steps
$step_sql = "SELECT * FROM recipe_steps WHERE recipe_id = :recipe_id";
$step_stmt = $pdo->prepare($step_sql);
$step_stmt->bindParam(":recipe_id", $row["recipe_id"], PDO::PARAM_INT);
$step_stmt->execute();
$step_result = $step_stmt->fetchAll(PDO::FETCH_ASSOC);
if ($step_result) {
echo "<h4>Steps:</h4>";
foreach ($step_result as $step_row) {
unset($step_row['recipe_id']); // remove recipe_id
echo implode(", ", $step_row) . "<br>";
}
} else {
echo "No steps found for this recipe id.<br>";
}
echo "<br><hr>";
}
} else {
echo "No recipes found for this category.";
}
}
echo '<button onclick="goBack()">Go Back</button>';
// Include a script to handle the button click
?>
<script>
function goBack() {
window.location.href = window.location.origin + '/recipe_webapp-main/';
}
</script>