st1984
st1984

Reputation: 67

how to add a cart total

I have been working on this for a few days. I never messed with anything to do with a cart so I am trying to understand this. Here is my web page and if you look off to the side it is not doing what I need it to do and also showing other codes: http://www.elinkswap.com/snorris/header.html. What I am trying to get done is when you are on the header.html page and you look at the cart content it should say You have 0 items. Then say you go over to the items and you want 1 and click update it should say you have 1 item in your cart. But for some reason I can not get it to work right. Here is a little bit of the code I am working with:

<?php # Script 5.2 - header.html

/*
*   This page begins the HTML header for the site.
*   The header also creates the right-hand column.
*   This page calls session_start().
*/

// Need sessions!
session_start();

// Check for a $page_title value:
if (!isset($page_title)) $page_title = 'WoW::World of Widgets!';
?><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><?php echo $page_title; ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="./includes/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="all">

    <div class="box">
        <div class="menu"><a href="#">home</a><a href="#">about</a><a href="#">products</a><a href="#">contact</a></div>
        <div class="header"><img alt="" style="float:right; " src="./images/www.jpg" width="225" height="95" />
        <h1>[<span class="style1">WoW</span>] World of Widgets</h1>
        <div class="clearfix"></div>
    </div>

    <div class="newsbar">
        <h1>Browse Widget Categories</h1>
        <div class="p2"><ul>
<li><a href="category.php?cid=5">Fuzzy Widgets</a></li>
<li><a href="category.php?cid=4">Non-widget Widgets</a></li>
<li><a href="category.php?cid=6">Razor-sharp Widgets</a></li>
<li><a href="category.php?cid=2">Widgets That Bounce</a></li>
<li><a href="category.php?cid=3">Widgets That Sit There</a></li>
<li><a href="category.php?cid=1">Widgets That Wiggle</a></li>
<?php
// Get all the categories and
// link them to category.php.

// Define and execute the query:
$q = 'SELECT category_id, category FROM categories ORDER BY category';
$r = mysqli_query($dbc, $q);

// Fetch the results:
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) {

    // Print as a list item.
    echo "<li><a href=\"category.php?cid=$fcid\"></a></li>\n";

    if($_SERVER['PHP_SELF']!="CART FILE"){
                echo "<h1>Cart Contents</h1>";
                echo "<div class=\"p2\">";
                $itemCount=$k;
                foreach($_SESSION['cart'] as $k=>$v){

                    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.

        ?></ul></div>


        <h1>Specials?</h1>
        <div class="p2">
            <p>Maybe place specials or new items or related items here.</p>
        </div>

    </div>

    <div class="content">

I am still learning some of these things and some of this just doesnt make sense and I cant get it to work. Thanks for looking

Upvotes: 1

Views: 281

Answers (2)

sags
sags

Reputation: 11

Try to base the echo of the if query on cart is empty or number of products. The number of products could be reached by the value you are refering to in the $k=>$V , with this there is also either one value or more to each of these values, that is the result of the count + the amount of times each item was ordered more than once.

something like this :

      $aantal += $value  ;

      if ( $value < 1  ) { 
         echo"Cart is empty";
      } else {
         echo'<h3>YOURE CART('. $aantal .')</h3>';
      }

Upvotes: 0

Layke
Layke

Reputation: 53206

You can count an array object, by just using count();

There is no need to loop over the array incrementing a $itemCount variable.

Just have:

$itemCount = count( $myShoppingCart );

Upvotes: 2

Related Questions