为什么我没有自己写就得到<a>标签

时间:2019-08-19 17:55:18

标签: php html

我在一个电子商务网站上工作,一切正常,直到我开始无处获取此标签,为什么我要得到它,请帮忙

这是我的代码

function cart_items(){
global $connection;
$ip_addr = getUserIpAddr();
$select_price = "select * from cart where ip_add = '$ip_addr'";
$run_price = mysqli_query($connection, $select_price);
while ($products_prices = mysqli_fetch_array($run_price)){
    $product_id = $products_prices['p_id'];
    $product_price = "select * from products where product_id = '$product_id'";
    $run_product_price = mysqli_query($connection,$product_price);
    while ($p_price = mysqli_fetch_array($run_product_price)){
        $pro_name = $p_price['product_title'];
        $pro_price = $p_price['product_price'];
        $pro_category = $p_price['product_cat'];
        $pro_image = $p_price['product_img'];
        $get_cat = "select * from categories where cat_id = $pro_category";
        $run_cat = mysqli_query($connection,$get_cat);
        $row_cat = mysqli_fetch_array($run_cat);
        $prod_cat = $row_cat['cat_title'];
        echo "
        <div class=\"row\">
             <div class=\"border-0 col-md-6\">
                  <div class=\"p-2\">
                       <img src=\"admin_area/product_images/$pro_image\" width=\"70\" class=\"img-fluid rounded shadow-sm\">
                       <div class=\"ml-3 d-inline-block align-middle\">
                            <h5 class=\"mb-0\"> <a href=\"#\" class=\"text-dark d-inline-block align-middle\">$pro_name</a></h5><span class=\"text-muted font-weight-normal font-italic d-block\">Category: $prod_cat</span>
                       </div>
                  </div>
             </div>
             <div class=\"border-0 align-middle align-center col-md-2\"><br><strong>$$pro_price</strong></div>
             <div class=\"border-0 align-middle align-center col-md-2\"><br><strong>3</strong></div>
             <div class=\"border-0 align-middle align-center col-md-2\"><br><a href=\"#\" class=\"text-dark\"><button class=\"btn btn-outline-danger fas fas-close\"><i class=\"fa fa-trash\"></i></button></div>
        </div>
        ";
    }

这就是我在浏览器中得到的东西

<div class="p-2">
  <a href="#" class="text-dark">
    <img src="admin_area/product_images/2.png" width="70" class="img-fluid rounded shadow-sm">
  </a>
  <div class="ml-3 d-inline-block align-middle">
    <a href="#" class="text-dark"></a>
  <h5 class="mb-0">
    <a href="#" class="text-dark"></a>
    <a href="#" class="text-dark d-inline-block align-middle">Second Product</a> 
  </h5>
  <span class="text-muted font-weight-normal font-italic d-block">Category: Bikes</span>
</div>

您会看到额外的<a href="#" class="text-dark"> 它是哪里来的??

1 个答案:

答案 0 :(得分:1)

@Barmar发现,存在一个未封闭的<a>标签,该标签会导致浏览器解析错误和意外行为。

一种更好的代码格式样式可以帮助您避免此类错误,尤其是在php中使用字符串生成html时。

示例:

echo "
  <div class=\"row\">
    <div class=\"border-0 col-md-6\">
      <div class=\"p-2\">
        <img src=\"admin_area/product_images/$pro_image\" width=\"70\" class=\"img-fluid rounded shadow-sm\" />
        <div class=\"ml-3 d-inline-block align-middle\">
          <h5 class=\"mb-0\">
            <a href=\"#\" class=\"text-dark d-inline-block align-middle\">
              $pro_name
            </a>
          </h5>
          <span class=\"text-muted font-weight-normal font-italic d-block\">
            Category: $prod_cat
          </span>
        </div>
      </div>
    </div>
    <div class=\"border-0 align-middle align-center col-md-2\">
      <br>
      <strong>$$pro_price</strong>
    </div>
    <div class=\"border-0 align-middle align-center col-md-2\">
      <br>
      <strong>3</strong>
    </div>
    <div class=\"border-0 align-middle align-center col-md-2\">
    <br>
    <a href=\"#\" class=\"text-dark\">
      <button class=\"btn btn-outline-danger fas fas-close\">
        <i class=\"fa fa-trash\"></i>
      </button>
    <a/>
 </div>
</div>";