-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
97 lines (78 loc) · 3.3 KB
/
index.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
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe Hub</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<header>
<?php include("admin/includes/nav.php"); ?>
</header>
<!-- <?php
// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);
?> -->
<?php
include('admin/includes/database.php');
if (!$connect) {
die("Database connection failed: " . mysqli_connect_error());
}
// Get the current page number
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1; // Default to page 1 if not set
$recipes_per_page = 15; // Number of recipes to display per page
$offset = ($page - 1) * $recipes_per_page; // Calculate the OFFSET for SQL query
// Get the total number of recipes
$total_query = "SELECT COUNT(*) as total FROM recipes";
$total_result = mysqli_query($connect, $total_query);
$total_row = mysqli_fetch_assoc($total_result);
$total_recipes = $total_row['total'];
// Get the recipes for the current page
$query = "SELECT * FROM recipes LIMIT $recipes_per_page OFFSET $offset";
$result = mysqli_query($connect, $query);
// Display recipes in 3-column grid
echo '<div class="container">';
echo '<h1 class="my-4 text-center">Recipe Hub</h1>';
echo '<div class="row">';
while ($recipe = mysqli_fetch_assoc($result)) {
echo '<div class="col-12 col-md-4 mb-4"> <!-- col-md-4 to show 3 items in a row on medium screens and up -->
<div class="card h-100 shadow-sm">
<img src="' . $recipe['Photo'] . '" class="card-img-top" alt="Recipe Image" style="height: 200px; object-fit: cover;">
<div class="card-body">
<h5 class="card-title">' . $recipe['RecipeName'] . '</h5>
<p class="card-text"><strong>Prep Time:</strong> ' . $recipe['PrepTime'] . ' mins</p>
<p class="card-text"><strong>Servings:</strong> ' . $recipe['Servings'] . '</p>
<form action="details.php" method="get">
<input type="hidden" name="RecipeID" value="' . $recipe['RecipeID'] . '">
<button type="submit" class="btn btn-success w-100">View Details</button>
</form>
</div>
</div>
</div>';
}
echo '</div>'; // Close the row
echo '</div>'; // Close the container
// Pagination links
$total_pages = ceil($total_recipes / $recipes_per_page); // Calculate the total number of pages
echo '<div class="pagination mt-4 d-flex justify-content-center">';
if ($page > 1) {
echo '<a href="?page=' . ($page - 1) . '" class="btn btn-secondary mx-1">Previous</a>';
}
for ($i = 1; $i <= $total_pages; $i++) {
if ($i == $page) {
echo '<span class="btn btn-success mx-1">' . $i . '</span>';
} else {
echo '<a href="?page=' . $i . '" class="btn btn-secondary mx-1">' . $i . '</a>';
}
}
if ($page < $total_pages) {
echo '<a href="?page=' . ($page + 1) . '" class="btn btn-secondary mx-1">Next</a>';
}
echo '</div>';
?>
</div>
</body>
</html>