-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipes.php
185 lines (171 loc) · 7.95 KB
/
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
session_start();
require_once('includes/db.php');
require_once('includes/functions.php');
require_once('classes/Recipe.php');
$meta_title = "Recipes | Cook Book";
$meta_description = " Explore our collection of delicious recipes ranging from appetizers to desserts.";
include 'inc/header.php'; // Include header
// Fetch all recipes from the database
$recipe = new Recipe($conn);
// Pagination
$limit = 6; // Number of recipes per page
$current_page = isset($_GET['page']) ? $_GET['page'] : 1; // Get current page from URL parameter
$offset = ($current_page - 1) * $limit; // Calculate offset
// Get filter and sort parameters
$category = isset($_GET['category']) ? $_GET['category'] : null;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'newest';
// Fetch recipes with applied filters
$recipes = $recipe->getFilteredRecipes($category, $sort, $limit, $offset);
// Count the total number of filtered recipes
$total_filtered_recipes = $recipe->countFilteredRecipes($category, $sort);
// Calculate the total number of pages based on the filtered results
$total_pages = ceil($total_filtered_recipes / $limit);
// Ensure current page is within valid range
$current_page = min(max(1, $current_page), $total_pages);
// Calculate the offset for pagination
$offset = ($current_page - 1) * $limit;
?>
<!-- Page Content -->
<section class="section bg-light">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 class="mb-4">All Recipes</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<form action="recipes.php" method="GET">
<div class="row">
<div class="col-md-4 mb-3">
<div class="form-floating">
<select id="category" name="category" class="form-select">
<option value="" <?php if(isset($_GET['category']) && $_GET['category'] == '') echo 'selected'; ?>>All
Categories</option>
<option value="Breakfast"
<?php if(isset($_GET['category']) && $_GET['category'] == 'Breakfast') echo 'selected'; ?>>Breakfast
</option>
<option value="Lunch"
<?php if(isset($_GET['category']) && $_GET['category'] == 'Lunch') echo 'selected'; ?>>Lunch
</option>
<option value="Dinner"
<?php if(isset($_GET['category']) && $_GET['category'] == 'Dinner') echo 'selected'; ?>>Dinner
</option>
<option value="Dessert"
<?php if(isset($_GET['category']) && $_GET['category'] == 'Dessert') echo 'selected'; ?>>Dessert
</option>
</select>
<label for="category" class="form-label">Filter by Category:</label>
</div>
</div>
<div class="col-md-4 mb-3">
<div class="form-floating">
<select id="sort" name="sort" class="form-select">
<option value="newest"
<?php if(isset($_GET['sort']) && $_GET['sort'] == 'newest') echo 'selected'; ?>>Newest</option>
<option value="oldest"
<?php if(isset($_GET['sort']) && $_GET['sort'] == 'oldest') echo 'selected'; ?>>Oldest</option>
</select>
<label for="sort" class="form-label">Sort by:</label>
</div>
</div>
<div class="col-md-4 mb-3 d-flex align-items-end justify-content-end">
<button type="submit" class="btn btn-primary h-100">Apply Filters</button>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<?php foreach ($recipes as $recipe) : ?>
<div class="col-md-4 mb-4">
<div class="card recipe-card shadow-sm">
<?php
// Display author information
$user_id = $recipe['user_id'];
$user_query = "SELECT username, email FROM users WHERE id = ?";
$user_statement = $conn->prepare($user_query);
$user_statement->bind_param("i", $user_id); // "i" indicates integer type for user_id
$user_statement->execute();
$user_result = $user_statement->get_result();
$user_details = $user_result->fetch_assoc();
$author_name = $user_details['username'];
$author_email = $user_details['email'];
$user_statement->close();
?>
<?php if (!empty($recipe['image_url'])) : ?>
<?php
// Original image URL from the database
$image_url = $recipe['image_url'];
// Transformation parameters
$transformation = 'c_fill,h_200,w_350'; // Landscape ratio and automatic subject detection
// Add transformation to the image URL
$transformed_url = preg_replace('/(upload\/)/', '$1' . $transformation . '/', $image_url);
?>
<div class="recipe-image">
<a href="recipe.php?slug=<?php echo $recipe['slug']; ?>">
<img src="<?php echo $transformed_url; ?>" class="card-img-top" alt="<?php echo $recipe['title']; ?>">
</a>
</div>
<?php else : ?>
<!-- Placeholder image if no image is available -->
<div class="recipe-image">
<a href="recipe.php?slug=<?php echo $recipe['slug']; ?>">
<img src="https://via.placeholder.com/350x200" class="card-img-top" alt="Placeholder Image">
</a>
</div>
<?php endif; ?>
<div class="card-body">
<a href="recipe.php?slug=<?php echo $recipe['slug']; ?>">
<h3 class=" card-title"><?php echo $recipe['title']; ?></h3>
</a>
<p class="card-text"><?php echo $recipe['description']; ?></p>
<ul class="meta">
<li>
<img src="https://www.gravatar.com/avatar/<?php echo md5(strtolower(trim($author_email))); ?>?s=40"
alt="<?php echo $author_name; ?>'s Avatar" class="rounded-circle"> <?php echo $author_name; ?>
</li>
<li>
<?php echo date('F j, Y', strtotime($recipe['created_at'])); ?>
</li>
<li>
<?php echo $recipe['category']; ?>
</li>
</ul>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<!-- Pagination links -->
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
<!-- Previous Page Link -->
<li class="page-item <?php echo ($current_page == 1) ? 'disabled' : ''; ?>">
<a class="page-link"
href="recipes.php?page=<?php echo $current_page - 1; ?><?php echo isset($_GET['category']) ? '&category=' . $_GET['category'] : ''; ?><?php echo isset($_GET['sort']) ? '&sort=' . $_GET['sort'] : ''; ?>"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<!-- Pagination Links -->
<?php for ($i = 1; $i <= $total_pages; $i++) : ?>
<li class="page-item <?php echo ($i == $current_page) ? 'active' : ''; ?>">
<a class="page-link"
href="recipes.php?page=<?php echo $i; ?><?php echo isset($_GET['category']) ? '&category=' . $_GET['category'] : ''; ?><?php echo isset($_GET['sort']) ? '&sort=' . $_GET['sort'] : ''; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
<!-- Next Page Link -->
<li class="page-item <?php echo ($current_page == $total_pages || $total_pages == 0) ? 'disabled' : ''; ?>">
<a class="page-link"
href="recipes.php?page=<?php echo $current_page + 1; ?><?php echo isset($_GET['category']) ? '&category=' . $_GET['category'] : ''; ?><?php echo isset($_GET['sort']) ? '&sort=' . $_GET['sort'] : ''; ?>"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</section>
<?php include 'inc/footer.php'; // Include footer ?>