使用 php 变量作为 HTML 图像中的 src 属性

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

我正在编写一个购物车页面,检查购物车中是否有东西,然后,如果条件为真,它会与带有产品图片、数量、价格和产品名称的 div 相呼应。这些变量位于会话数组内,因为我从其他页面(登录和登录)获取数据,在其中声明以下数组:

$myProdArray=[
  array("Superbiomin", 0.016, 3, "/img/superbiomin.jpg"), //name, units, price, image
  array("Prop P Jaleia Reial", 0.010, 2, "/img/proppjalea.jpg"),
  array("Vitamina C 500mg Mastegable Solaray", 0.0097, 1, "/img/vitc.jpg"),
];
$_SESSION['prods'] = $myProdArray;

一切正常,但我不知道如何引用存储在会话数组中的图像名称字符串。这是购物车页面的正文代码:

<body class="cartbody">
<?php
  $nothing = true;
  for($i = 0; $i<3;$i++){
    if($_SESSION['prods'][$i][2] !== 0) {
      $nothing = false;
      if($nothing == false) {   
    ?>
      <div class="cartbodyfalse">
        <div class="productSummary">
        <?php
          for($j=$i; $j<3;$j++){
            if($_SESSION['prods'][$j][2]>0)echo "
              <div class='indivProdRow'><img src=".
               $_SESSION['prods'][$j][2]." class='prodcartimg'><h5>x" . 
               $_SESSION['prods'][$j][2] . " " . 
               $_SESSION['prods'][$j][0] ." " . 
               $_SESSION['prods'][$j][1] ." ETH</h5></div>";
             }
           }
           echo "</div>";
           $i = $j;       
          }   
        }
        if($nothing == true){
            echo "<div class='cartbodytrue'><div class='cartmsg'><h4 class='nothing'>Sembla que encara no tens res al carret</h4><a href='botiga.php'><button class='btn kbuyin'><i class='fa-solid fa-shop'></i> Seguir comprant</button></a></div></div>";
        } else { ?>
          <div class="ticketactions">
          <?php ?>
          </div>
        <?php } ?> 
        </div>     
    </body>

我期望代码输出图像,但是,由于 src 属性值不正确,它输出了迷你 img 徽标。最后,除了前面显示的代码之外,我尝试使用“.{'session array'}.”和“.'session array'”。

php html web src
1个回答
0
投票

我在你的代码中看到的方式,图像路径的索引键是

3

尝试:

echo "<div class='indivProdRow'><img src=".$_SESSION['prods'][$j][3]." ...

顺便说一句,不相关的一点:多次关闭并重新打开 PHP 标签并不是一个好主意,它可能会导致更多错误,甚至暴露后端代码。创建一个 HTML 内容缓冲区并在最后刷新它。

© www.soinside.com 2019 - 2024. All rights reserved.