Domanda

I want to display a list of products, but in the output browser this code is displaying only the last product and ignoring the previous items:

<?php 
  $cartOutput="";
  if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
$cartOutput="<h2 align='center'>Your shopping cart is empty</h2>";
  }else{
$i=0;
foreach($_SESSION["cart_array"] as $each_item){
    $i++;
    $cartOutput="<h2>Cart Item $i</h2>";
        $cartOutput="Item ID:" .$each_item['item_id']."<br>";
    $cartOutput="Item Quantity:" .$each_item['item_id']."<br>";
    //while(list($key,$value)=each($each_item)){
      //$cartOutput="item_id: $pid <br> $key: $value<br/>";
    //}
       }
      }
      ?>

and here is html

  <body>
  <div align="center" id="mainWrapper">
  <?php include_once("template_header.php");?>
   <div id="pageContent">
   <div style="margin:24px; text-align:left;">
  <?php echo $cartOutput ?>
  <br /><br />
    <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a>
    </div>
    <br />
    </div>
    <?php include_once("template_footer.php");?>
   </div>
   </body>
  </html>
È stato utile?

Soluzione

That is because you are overwriting the variable $cartOutput each time your foreach loop runs. So that means that only the last element of your cart is visible. You can remedy this by either appending to $cartOutput or by making it an array, and imploding it afterwards.

Like this (using implode):

<?php 
$cartOutput=array();
if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align='center'>Your shopping cart is empty</h2>";
}else{
    $i=0;
    foreach($_SESSION["cart_array"] as $each_item){
        $i++;
        $cartOutput[] = "<h2>Cart Item $i</h2>";
        $cartOutput[] = "Item ID:" .$each_item['item_id']."<br>";
        $cartOutput[] ="Item Quantity:" .$each_item['item_id']."<br>";
    }
    $cartOutput = implode('<br />', $cartOutput);
}
?>

Alternatively you can also just append all output to the already existant output in $cartOutput like so:

<?php 
$cartOutput="";
if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
    $cartOutput="<h2 align='center'>Your shopping cart is empty</h2>";
}else{
    $i=0;
    foreach($_SESSION["cart_array"] as $each_item){
        $i++;
        // note the use of .= which is a combined assignment and concatenation operators (it concatenates all content after the .= to the string preceding it)
        $cartOutput .= "<h2>Cart Item $i</h2>";
        $cartOutput .= "Item ID:" .$each_item['item_id']."<br>";
        $cartOutput .="Item Quantity:" .$each_item['item_id']."<br>";
    }
}
?>

Altri suggerimenti

You're resetting the cartOutput value each time the while statement goes through an iteration. So inside the iteration change $cartOutput = "...." to $cartOutput .= "...."

<?php 
$cartOutput="";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput="<h2 align='center'>Your shopping cart is empty</h2>";
} else {
    $i=0;
    foreach($_SESSION["cart_array"] as $each_item) {
        $i++;
        $cartOutput .= "<h2>Cart Item $i</h2>";
        $cartOutput .= "Item ID:" .$each_item['item_id']."<br>";
        $cartOutput .= "Item Quantity:" .$each_item['item_id']."<br>";
     }
}
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top