【问题标题】:How do i display total item in cart just behind cart icon如何在购物车图标后面显示购物车中的总商品
【发布时间】:2017-12-04 15:56:23
【问题描述】:

我是 css 新手,我在研究期间得到的结果很少,但我希望购物车中的全部商品显示在我的购物车图标后面,如下所示:

我的代码:

<?php
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
echo "<style type='text/css'>
    .cart {
        background-image: url('cart.png');
    }

    </style>";
echo "<div class='cart'></div>";
} else {
  $totalquantity = 0;
foreach($_SESSION['cart_array'] as $each_item) {
             $totalquantity += $each_item[quantity];
            }
echo "<div class='cart'>$totalquantity</div>";
}
?>

提前致谢

【问题讨论】:

  • 寻求代码帮助的问题必须包含重现它所需的最短代码在问题本身中最好在Stack Snippet 中。见How to create a Minimal, Complete, and Verifiable example
  • 查看“位置:相对”。然后有一个带有两个图像的 div。第二张图片的内容类似于“top: 20px; left: -1;”
  • 请在真正的团队中,我没有关于我的问题的代码,如果真的有可能实现的话
  • 如果问题仅与所需的 html 和 css 有关,您可以通过 only 将呈现的 html 放置在帖子中而不是 php 代码在此帖子之后生成它来获得更好的结果未被标记为PHP
  • @happymacarts 我真的需要解决方案 php is tag

标签: php css icons cart


【解决方案1】:

这与 Bootstrap 使用的类似

使用css伪选择器:empty可以在没有内容时自动折叠

这样在你的 php 中你总是可以渲染 ""

.container {
  background-color: #000;
  padding: 5px;
  width: 200px;
  position: relative;
}

.cart {
  background-image: url('http://www.pvhc.net/img180/xewlwqfroahrisfwokkc.png');
  background-size: contain;
  background-repeat: no-repeat;
  background-color: darkgreen;
  width: 50px;
  height: 50px;
  border: 1px solid #fff;
  outline: 3px solid #8174f7;
  position: absolute ;
  right:8px;
  top:8px;
}

.badge {
  position: absolute;
  left: -15px;
  bottom: -15px;
  font-weight: 700;
  font-family: sans-serif;
  font-size: 20px;
  color: #fff;
  background-color: green;
  border: 2px solid #fff;
  padding: 3px;
  width: 20px;
  height: 20px;
  text-align: center;
}

.badge:empty {
  display: none;
}

.content {
  background-color: #fff;
  color: #000;
  padding:8px;
  text-align:center;
  margin-top:62px;
}

.red {
  color: red;
}
<div class="container">
  <div class='cart'>
    <span class="badge">4</span>
  </div>
  <div class="content">
    Tot. odd: <span class="red">40.04</span>
  </div>
</div>

您的 PHP 代码可能如下所示

<?php 

echo "<style>... the styles above</style>" ;
/*
load your styles before content is rendered
they should be loaded in a a separate css file but that's up to you

*/


$totalquantity = 0;
if(!isset($_SESSION["cart_array"]) ) {
      foreach($_SESSION['cart_array'] as $each_item) {
         $totalquantity += $each_item[quantity];
        }
?>
<div class='cart'><?php echo $totalquantity;?></div>
<?php }?>

【讨论】:

  • 是的,一开始谢谢,但我真的不知道怎么说这不是预期的那样称之为 echo "
    $totalquantity
    ";就在我的标题之后,但我在页面顶部输出了我的期望输出应该是如果它被称为例如 ________________________________________________________________________ 在这里显示 ____________________________ 但我得到 ___________________ 显示在这里 _____________________________________________
  • 不确定这条评论是什么意思?我想我理解这个问题。您没有输出我创建的“容器” div,它是初始化相对位置的原因。没有它,您将获得一个绝对定位的元素
猜你喜欢
  • 1970-01-01
  • 2015-06-01
  • 2019-12-09
  • 2018-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2020-01-14
相关资源
最近更新 更多