使用基于<a> class the user clicks

时间:2019-05-02 21:38:45

标签: php html css mysql wamp

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

的PHP显示数据

我有一个“ Recipes.html”,用户可以选择要浏览食谱的类别(早餐,晚餐等)。在这些类别中(以我的情况为例,以Breakfast.html为例),这些卡片代表了每个食谱。

到目前为止,我尝试过的是: 我有一个.htaccess文件,可将用户从类别的.html文件重定向到我创建的相应类别的.php文件,在本例中,它是从breakfast.html重定向到breakfast.php。这是我拥有php的地方路由器(以及卡片和所有其他html内容),但老实说我迷路了,我也不知道如何使用它。 Breakfast.php代码:

<!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代码

RewriteEngine On
Redirect /r/breakfast.html /r/breakfast.php
RewriteBase /
RewriteCond %{REQUEST_FILENME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ breakfast.php/ [QSA,L]

我还创建了一个recipe.php文件,将在该文件上将其用作食谱模板,在该文件中,我将根据用户单击的类(即按哪个食谱)显示该食谱。它的代码是:

<!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>

我想让PHP理解用户想要阅读的食谱,将ID(也重定向到)指向recipe.php,而recipe.php可以做到这一点。我很困惑,我需要以某种方式使它工作成为最简单的方法,因为这是我的第一个构建。谢谢大家。

0 个答案:

没有答案