【问题标题】:how to call function with in function string PHP如何在函数字符串PHP中调用函数
【发布时间】:2020-06-11 05:16:13
【问题描述】:

我做了功能来知道用户是否登录..取决于添加到购物车将可用于添加到购物车或注册页面。

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】:

    更改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;
    }
    

    【讨论】:

    • 先生,非常感谢这位先生,它工作得很好,再一次比你好多了......
    猜你喜欢
    • 2019-04-07
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    相关资源
    最近更新 更多