So I've been trying to create a recipe website where I'll be uploading some of my recipes. Each recipe has its' own title-photo card with an View Recipe class. Because I don't want to create an additional html file for each recipe,someone suggested that I create a database on mysql and display them based on what user requests, so I did that(through WAMP). I'm completely beginner to all this and I have no idea how can I make it work though. Recipe cards
I have a "Recipes.html" where the user selects which category(breakfast, dinner etc) they want to browse recipes. Inside those categories(in my case I'll use breakfast.html as an example) I have these cards representing each recipe.
What I've tried so far is: I've an .htaccess file that redirects the user from the category's .html file to the respective category's .php file I've created,in our case it redirects from breakfast.html to breakfast.php.This is where I have a php router (along with the cards and all of the rest html stuff) but to be honest I'm lost and I don't know how to use it. Breakfast.php code:
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Breakfast</title>
</head>
<body id="page3">
<div class="all-wrapper">
<div class="navbar">
//nav here
</div>
<section class="morning">
<h1>Breakfast</h1>
<?php
$request = $_SERVER['REDIRECT_URL'];
$url = $_GET['URL'];
$parts = explode('/', $url);
$id = $parts[count($parts) - 1];
switch ($request) {
case '/recipes/1' :
require __DIR__ . '/r/recipe.php';
break;
}
?>
<div class="rec-wrap">
<div class="recipe">
<h2>Food title</h2>
<img class="photo" src="breakfast.jpg">
<a href="/recipes/1">View Recipe</a>
</div>
</section>
</div>
<footer>
</footer>
</body>
</html>
.htaccess code
RewriteEngine On
Redirect /r/breakfast.html /r/breakfast.php
RewriteBase /
RewriteCond %{REQUEST_FILENME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ breakfast.php/ [QSA,L]
I've also created a recipe.php file on which I'll be using as a recipe template,where I'll be displaying the recipe based on which class(so on which recipe) the user clicked. The code of it is:
<!DOCTYPE HTML>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Recipe</title>
</head>
<body>
<div class="all-wrapper">
<div class="navbar">//nav here
</div>
<main>
<table>
<tr>
<th>Title</th>
<th>Ingredients</th>
<th>Method</th>
<th>Author</th>
</tr>
<?php
$_GET['id'];
$conn = mysqli_connect("name", "username", "", "databasename");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT title, ingredients,method, author FROM recipes WHERE $id=id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["title"]. "</td><td>" . $row["ingredients"] . "</td><td>". $row["method"]. "</td><td>" . $row["author"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
</main>
I want PHP to understand which recipe the user wants to read,pass the ID(and redirect too) to recipe.php and recipe.php will do it's job I guess. I'm confused and I need to somehow make it work the easiest way possible since it's my first build. Thank you everyone.