-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipe-page.php
More file actions
139 lines (116 loc) · 4.53 KB
/
recipe-page.php
File metadata and controls
139 lines (116 loc) · 4.53 KB
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
<?php
session_start();
require 'db.php';
if (!isset($_SESSION['UserID'])) {
header("Location: login.php");
exit;
}
$user = $_SESSION['UserID'];
$sql = "
SELECT r.RecipeID, r.Title, r.DateCreated,
MAX(n.Timestamp) AS LastNote,
COUNT(n.NoteID) AS NoteCount
FROM Recipe r
JOIN Access a ON r.RecipeID = a.RecipeID
LEFT JOIN Note n ON r.RecipeID = n.RecipeID
WHERE a.UserID = ? AND a.Status = 1
GROUP BY r.RecipeID
ORDER BY r.DateCreated DESC";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i", $user);
$stmt->execute();
$recipes = $stmt->get_result();
// now loop through $recipes in your HTML
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="recipe-container">
<header id="header-auth">
<div>
<h1><span class="dish">Dish</span><span class="covery">covery</span></h1>
<form>
<button type="button" class="logout-btn" onclick="location.href='login.php'">Logout</button>
</form>
</div>
</header>
<section class="recipes-section1">
<h2>My Recipes</h2>
<?php
// Separate owned vs shared
$myRecipes = [];
$sharedRecipes = [];
$recipes->data_seek(0); // Reset pointer
while ($recipe = $recipes->fetch_assoc()) {
// Check if user created this recipe
$checkOwner = $mysqli->prepare("SELECT CreatedBy FROM Recipe WHERE RecipeID = ?");
$checkOwner->bind_param("i", $recipe['RecipeID']);
$checkOwner->execute();
$ownerResult = $checkOwner->get_result()->fetch_assoc();
if ($ownerResult['CreatedBy'] == $user) {
$myRecipes[] = $recipe;
} else {
$sharedRecipes[] = $recipe;
}
}
// Display owned recipes
foreach ($myRecipes as $recipe) {
$lastNote = $recipe['LastNote'] ? date('m/d/Y', strtotime($recipe['LastNote'])) : 'No notes yet';
$dateCreated = date('m/d/Y', strtotime($recipe['DateCreated']));
?>
<div class="card">
<h3 class="recipe-title"><?= htmlspecialchars($recipe['Title']) ?></h3>
<p class="recipe-meta">Created by: <?= htmlspecialchars($_SESSION['ScreenName']) ?></p>
<p class="recipe-meta">Created on: <?= $dateCreated ?></p>
<p class="recipe-meta">Last edited: <?= $lastNote ?></p>
<p class="recipe-meta">Notes: <?= $recipe['NoteCount'] ?></p>
<div class="card-actions">
<button onclick="location.href='view-page.php?id=<?= $recipe['RecipeID'] ?>'">View Recipe</button>
<button onclick="location.href='manage-page.php?id=<?= $recipe['RecipeID'] ?>'">Manage Access</button>
</div>
</div>
<?php } ?>
<div class="card create-card">
<button class="create-btn" onclick="location.href='create-page.php'">+</button>
<p>Create New Recipe</p>
</div>
</section>
<!-- Shared -->
<section class="recipes-section2">
<h2>Shared</h2>
<?php
// Display shared recipes
foreach ($sharedRecipes as $recipe) {
$lastNote = $recipe['LastNote'] ? date('m/d/Y', strtotime($recipe['LastNote'])) : 'No notes yet';
$dateCreated = date('m/d/Y', strtotime($recipe['DateCreated']));
// Get creator name
$getCreator = $mysqli->prepare("SELECT u.ScreenName FROM User u JOIN Recipe r ON u.UserID = r.CreatedBy WHERE r.RecipeID = ?");
$getCreator->bind_param("i", $recipe['RecipeID']);
$getCreator->execute();
$creator = $getCreator->get_result()->fetch_assoc();
?>
<div class="card">
<h3 class="recipe-title"><?= htmlspecialchars($recipe['Title']) ?></h3>
<p class="recipe-meta">Shared by: <?= htmlspecialchars($creator['ScreenName']) ?></p>
<p class="recipe-meta">Created on: <?= $dateCreated ?></p>
<p class="recipe-meta">Last edited: <?= $lastNote ?></p>
<p class="recipe-meta">Notes: <?= $recipe['NoteCount'] ?></p>
<div class="card-actions">
<button onclick="location.href='view-page.php?id=<?= $recipe['RecipeID'] ?>'">View Recipe</button>
</div>
</div>
<?php } ?>
</section>
<!-- Logout -->
<footer id="footer-auth">
<p class="footer-text">CS 215: Assignment 3</p>
</footer>
</div>
</body>
</html>