简体   繁体   中英

add in how many items are in the cart

I can not get this part to work I have even tried to change the $key to just $k => $v and it is not outputting what i need to to do. Should I be changing this line $itemCount=X; to $itemCount=count() ??? At this point in time I have not a clue what else to do and its very fustating. Thanks for all whos look.

I need to add in how many items are in the cart

  if($_SERVER['PHP_SELF']!="CART FILE"){
            echo "<h1>Cart Contents</h1>";
            echo "<div class=\"p2\">";
            $itemCount=X;
            foreach($_SESSION['cart'] as $key=>$X)
            {
               for($i=0;$i<count($X);$i++){
                  $itemCount+= $X;
               }
            }

            echo "<a href=\"cart.php\">You have ".$itemCount." total items in your cart.</a>";
            echo "</div>\n";
 }
} // End of while loop.

Just use the PHP count() method on your $_SESSION['cart'] variable and remove all the other code like your foreach. This should be fine:

 if($_SERVER['PHP_SELF']!="CART FILE"){             
     echo "<h1>Cart Contents</h1>";             
     echo "<div class=\"p2\">";             

     echo "<a href=\"cart.php\">You have ".count($_SESSION['cart'])." total items in your cart.</a>";
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM