dongpao2871 2019-05-02 21:38
浏览 81

使用PHP根据用户单击的<a>类显示数据

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿