php - 如何在函数字符串PHP中调用函数
问题描述
我做了功能来知道用户是否登录..取决于添加到购物车将可用于添加到购物车或注册页面。
function showCart($post_id){
if(isset($_SESSION["user_name"])){
echo '<form action="" method="post">
<button type="submit" class="btn btn-cart" name="add_cart" value='.$post_id.'>Add to Cart</button>
</form>';
} else {
echo '<button type="submit" class="btn btn-cart"> <a href="log_in.php"> Add to Cart </a> </button>';
}
}
产品页面上的购物车功能必须显示...
function component($post_id, $post_cover, $post_title, $post_category, $cat_name, $product_cost){
$str = "
<div class=\"swiper-slide\">
<!-- Projects Wrap Start -->
<div class=\"projects-wrap style-01 wow move-up\">
<div class=\"projects-image-box\">
<div class=\"projects-image\">
<a href=\"product.php?view_post=$post_id\">
<img class=\"img-fluid\" src=\"images/blog/$post_cover\" alt=\"$post_cover\">
</a>
</div>
<div class=\"content\">
<h6 class=\"heading\">
<a href=\"product.php?view_post=$post_id\">
$post_title;
</a>
</h6>
<span class=\"course_name\">
<div class=\"tag\">in
<a href=\"listing.php?view_all_cat=$post_category\">
$cat_name
</a>
</div>
</span>
<div class=\"course_struct\">
<div id=\"rating\">
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star-half-alt\"></i>
</div>
showCart($post_id); // Here i want to execute that cart button depedning on user login
</div>
<div class=\"box-projects-arrow\">
<div class=\"cost_text\">₹ $product_cost/-</div>
<span class=\"button-text\">
<a href=\"product.php?view_post=$post_id\">
Explore
</a>
</span>
</div>
</div>
</div>
</div>
</div>
";
echo $str;
}
如果我将函数放在函数字符串中,它会像文本一样呈现,我是否在这里遗漏了任何错误...
解决方案
更改showCart()
使其返回字符串而不是打印它。然后你可以将它与其他字符串连接起来。
function showCart($post_id){
if(isset($_SESSION["user_name"])){
return '<form action="" method="post">
<button type="submit" class="btn btn-cart" name="add_cart" value='.$post_id.'>Add to Cart</button>
</form>';
} else {
return '<button type="submit" class="btn btn-cart"> <a href="log_in.php"> Add to Cart </a> </button>';
}
}
function component($post_id, $post_cover, $post_title, $post_category, $cat_name, $product_cost){
$str = "
<div class=\"swiper-slide\">
<!-- Projects Wrap Start -->
<div class=\"projects-wrap style-01 wow move-up\">
<div class=\"projects-image-box\">
<div class=\"projects-image\">
<a href=\"product.php?view_post=$post_id\">
<img class=\"img-fluid\" src=\"images/blog/$post_cover\" alt=\"$post_cover\">
</a>
</div>
<div class=\"content\">
<h6 class=\"heading\">
<a href=\"product.php?view_post=$post_id\">
$post_title;
</a>
</h6>
<span class=\"course_name\">
<div class=\"tag\">in
<a href=\"listing.php?view_all_cat=$post_category\">
$cat_name
</a>
</div>
</span>
<div class=\"course_struct\">
<div id=\"rating\">
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star\"></i>
<i class=\"fa fa-star-half-alt\"></i>
</div>"
.
showCart($post_id); // Here i want to execute that cart button depedning on user login
.
"</div>
<div class=\"box-projects-arrow\">
<div class=\"cost_text\">₹ $product_cost/-</div>
<span class=\"button-text\">
<a href=\"product.php?view_post=$post_id\">
Explore
</a>
</span>
</div>
</div>
</div>
</div>
</div>
";
echo $str;
}
推荐阅读
- r - dplyr/sparklyr "OR" 加入
- php - 如何搜索具有多个所需针的关联数组数组并返回索引?
- javascript - 光滑的滑块中心模式
- c# - 如何显示带有后台工作人员实际进度百分比的进度条?
- javascript - Angular如何通过传递属性名称来动态添加属性
- java - war文件如何在docker容器上的tomcat中创建目录
- vba - 如果值存在于数据验证列表中,则粘贴到数据验证中
- javascript - 警告:数组或迭代器中的每个孩子都应该有一个唯一的“key”道具。密钥已经存在
- ios - 客户端可以发布使用我的开发人员门户证书创建的 .ipa 二进制文件吗?
- vue.js - 使用 Vue 模板标签输出 html,但没有嵌套元素