skip to Main Content

I’m coding a cart page that checks if there’s something in the cart and then, if the condition is true,, it echoes a div with a picture of the product, the amount, the price and the name of the product. The variables are inside a session array because I take the data from other pages (login and sign in) where I declare the following array:

$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;

Everything works but I don’t know how to refer to the image name string stored in the session array. Here’s the body code of the cart page:

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

I was expecting the code to output the image, however, it outputs the mini img logo as the src attribute value is incorrect. Finally, apart from the code previously showed, I tried using ".{‘session array’}.", and ".’session array’".

2

Answers


  1. The way I see in your code, the index key for image path is 3.

    Try:

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

    UPDATE: You shoud use qoutes/double-qoutes for src as mentioned in comments by @user3783243.

    UPDATE2: There are too many things wrong with this code, yet another thing is that "/img/superbiomin.jpg"is an absolute path not relative, so this will not show you the image as well, You must use a dot at first of paths to achieve relative path and make it work, use it like: "./img/proppjalea.jpg".

    BTW, irrelevant point : having PHP tags closed and reopened several times is not a good idea, It may cause more mistakes or even exposing back-end code. Make a buffer of HTML content and flush it at the end.

    Login or Signup to reply.
  2. It is better to code like this :‌

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

    Then:

    <body class="cartbody">
    <div class="cartbodyfalse">
        <div class="productSummary">
            <?php
            
            if (count($_SESSION["prods"]) > 0) {
                foreach ($_SESSION['prods'] as $prod) {
                    echo "
                  <div class='indivProdRow'><img src='" .
                        $prod["image"] . "' class='prodcartimg'><h5>x" .
                        $prod["price"] . " " .
                        $prod["name"] . " " .
                        $prod["units"] . " ETH</h5></div>";
                }
            }
            else{
                    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>";
            }
            ?>
        </div>
    </body>
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search