从另一个PHP页面调用函数

时间:2020-01-07 08:31:15

标签: php

我一直在开发一个电子商务应用程序,我在索引页面中取出了所有HTML,并创建了一个名为component()的函数,如下所示:

function component($productName,$productPrice,$productImage){
    $element = "
    <div class=\"col-md-3 col-sm-6 my-3 my-md-0\">
    <form action=\"index.php\" method=\"POST\">
        <div class=\"card shadow\">
            <div>
                <img src=\"$productImage\" alt=\"image1\" class=\"img-fluid card-img-top\">
            </div>
            <div class=\"card-body\">
                <h5 class=\"card-title text-sm-center\">$productName</h5>
                <h6 class=\"text-sm-center\">
                   <i class=\"fas fa-star\"></i>
                   <i class=\"fas fa-star\"></i>
                   <i class=\"fas fa-star\"></i>
                   <i class=\"fas fa-star\"></i>
                   <i class=\"far fa-star\"></i>
                </h6>
                <p class=\"card-text text-sm-center\">
                    Some Quick example text to build on the card
                </p>
                <h5 class=\"text-sm-center\">
                    <small><s class=\"text-secondary\">$519</s></small>
                    <span class=\"price\">$productPrice</span>
                </h5>

                <button class=\"btn btn-warning\" type=\"submit\" name=\"add\">Add to Cart<i class=\"fas fa-shopping-cart\"></i></button>
            </div>
        </div>    
    </form>
</div>

    ";

    echo $element;
}

?>

现在,我正在尝试像这样访问index.php中组件的HTML内容:

<?php  require "component.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Shopping Cart</title>

    <link rel="stylesheet" href="./dist/css/all.min.css">
    <link rel="stylesheet" href="./dist/css/style.css">

    <link rel="stylesheet" href="./dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" integrity="sha256-46qynGAkLSFpVbEBog43gvNhfrOj+BmwXdxFgVK/Kvc=" crossorigin="anonymous" />
    <!--<link rel="stylesheet" href="./dist/css/fa.5.11.2.css">-->


</head>
<body>
<div class="container">
    <div class="row text-center py-5">
      <?php

       component(productName:"Product 1", productPrice:599, productImage:"./upload/product1.png" );

      ?>
    </div>      
</div>


    <script src=".dist/js/jquery.min.js"></script>
    <script src=".dist/js/popper.min.js"></script>
    <script src=".dist/js/bootstrap.min.js"></script>
</body>
</html>

我收到此错误:

错误消息:解析错误:语法错误,第24行的C:\ xampp \ htdocs \ shopping \ index.php中出现意外的':',期望为','或')'

请,我需要帮助才能继续。

谢谢

1 个答案:

答案 0 :(得分:0)

使用下面提到的HTML。

<?php  require "component.php"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Shopping Cart</title>

    <link rel="stylesheet" href="./dist/css/all.min.css">
    <link rel="stylesheet" href="./dist/css/style.css">

    <link rel="stylesheet" href="./dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" integrity="sha256-46qynGAkLSFpVbEBog43gvNhfrOj+BmwXdxFgVK/Kvc=" crossorigin="anonymous" />
    <!--<link rel="stylesheet" href="./dist/css/fa.5.11.2.css">-->


</head>
<body>
<div class="container">
    <div class="row text-center py-5">
      <?php

       component("Product 1", 599,"./upload/product1.png" );

      ?>
    </div>      
</div>


    <script src=".dist/js/jquery.min.js"></script>
    <script src=".dist/js/popper.min.js"></script>
    <script src=".dist/js/bootstrap.min.js"></script>
</body>
</html>

您需要更改下面提到的调用函数。

component("Product 1", 599,"./upload/product1.png" );