重复 HTML 元素,并将 php 变量传递给 PHP 中的函数

我正在尝试创建一个简单的产品目录网站,我想为每个产品显示卡片。我试图通过 PHP 模板变量显示卡片,并将名称、img src、价格等作为参数传递给打印卡片的函数。


这是我的index.php代码:


<?php

require('./php/product_card.php');

?>


<!DOCTYPE html>

<html>


<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <!-- index.css link ref -->

    <link rel="stylesheet" href="css/index.css">

    <!-- Bootstrap Link Tag -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">

    <!-- FontAwesome Link Tag -->

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/brands.min.css" integrity="sha512-AMDXrE+qaoUHsd0DXQJr5dL4m5xmpkGLxXZQik2TvR2VCVKT/XRPQ4e/LTnwl84mCv+eFm92UG6ErWDtGM/Q5Q==" crossorigin="anonymous" />

    <title>Emmaarz</title>

</head>


<body>

    <div class="container">

        <div class="row text-center py-6 mt-5">

            <?php

                product_card("Bed Cover 1", "img_bc/1.jpeg", "Product Description in less than 100 characters (optional)", "399", "299");

                product_card("Bed Cover 2", "img_bc/2.jpeg", "Product Description in less than 100 characters (optional)", "599", "399");

            ?>

        </div>

    </div>

</body>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>

<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>


</html>

当我只是 echo $prod_name; 时 它显示了我传递的产品的名称,但它在 $prod_card 变量中不起作用。看起来是什么问题?如何才能实现这一点呢?



森栏
浏览 112回答 3
3回答

偶然的你

就这样吧。您的变量被视为普通文本字符。您需要将 PHP 变量附加到 HTML 字符串中<?phpfunction product_card($prod_name, $prod_img, $prod_desc, $prod_price, $prod_price_disc) {&nbsp; &nbsp; $prod_card = '&nbsp; &nbsp; <div class="col-md-3 col-sm-6 my-3 my-md-1">&nbsp; &nbsp; &nbsp; &nbsp; <form action="index.php" method="get">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="$prod_img" alt="error" class=" img-fluid card-img-top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="card-title">'.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod_name.'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="card-text">'.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod_desc.'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5><s class="text-secondary">'.$prod_price_disc.'</s><span class="mx-2"></span><span class="price">'.$prod_price.'</span></h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; ';&nbsp; &nbsp; echo $prod_card;}?>

人到中年有点甜

您必须将字符串与变量连接起来,例如 Echo 'fix string'.$var;&nbsp;在您的情况下,在每个 var $prod_img, $prod_name 之前以 ' 和 add 结束。在你的情况下 <img src="'.$prod_img.'" 等等

不负相思意

<div class="col-md-3 col-sm-6 my-3 my-md-1">&nbsp; &nbsp; <form action="index.php" method="get">&nbsp; &nbsp; &nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="$prod_img" alt="error" class=" img-fluid card-img-top">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-body">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5 class="card-title">'.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod_name.'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="card-text">'.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $prod_desc.'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h5><s class="text-secondary">$prod_price_disc</s><span class="mx-2"></span><span class="price">$prod_price</span></h5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </form></div>或许?我不确定但是
打开App,查看更多内容
随时随地看视频慕课网APP