如何在函数字符串PHP中调用函数

问题描述 投票:0回答:1

i的功能是知道用户是否登录。.根据添加到购物车的信息,可以添加到购物车或注册页面。

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\">&#8377; $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;
}

如果我将函数放在函数字符串中,它就像文本一样呈现,是否在这里遗漏了任何错误...

php session shopping-cart
1个回答
0
投票

更改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\">&#8377; $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;
}
© www.soinside.com 2019 - 2024. All rights reserved.