Jump to content

[SOLVED] How to display total quantity of items in a shopping cart on other page on a web


Recommended Posts

Hi Everyone,

 

I have a problem, I've created a shopping cart in php, this is what happens:

 

-When a user presses the add button on a gallery page it adds the details of the item to the shopping cart.

 

-This works perfectly fine.

 

-What I really need help with is, how I can let the user see how many items they have in their shopping cart on other pages while they are still browsering the site.

 

-So once they pressed the add button it will show the total quantity of cart.

 

I hope this is clear enough.

 

Thank you

???

Use the magic() function.

 

hehe seriously, there are too many ways to do this. What you need is how to do it in your particular scenario, which requires more detail than you are providing.

 

If your cart is an array, you can use

 

echo count($_SESSION['cart-array']);

 

to count the items.

To Helmet,

Heres copy of my shopping cart script, you see Im very new new to php, so could you break down your explaination around my code please:

 

<?php

session_start();      //at the top of all pages. sessions it stores the data about the user in the web server.

                      //the session stores session variables for each user,when a session is started the client

    //is given a session identifier

 

if (isset($_SESSION['cart'])) {

?>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title></title>

<link rel="stylesheet" type="text/css" href="style.css" />

<style type="text/css">

<!--

.style1 {color: #000000}

.style7 {

font-size: 12px;

color: #000000;

}

.style8 {

font-size: 12px;

font-weight: bold;

color: #000000;

}

.style12 {

font-size: 14px;

color: #000000;

}

.style19 {font-size: 14px; color: #000000; font-weight: bold; }

-->

</style>

</head>

<body>

<div id="top_menu"></div>

<div id="main_content">

<div id="top_banner">

    <a href="index.html"><img src="images/roar.jpg" width="391" height="130" alt="home" title="logo" border="0" class="logo" /></a>    </div>

   

 

 

   

  <div >

    <div class="title style1">

      <p><strong>Shopping Cart</strong></p>

    </div>

<div class="content_text">

  <table width="40%" border="1" bordercolor="black" bgcolor="">

            <tr>

              <th width="5%"> <span class="style7">STYLE NUMBER</span></th>

              <th width="6%"> <span class="style7">DETAILS</span></th>

              <th width="6%"> <span class="style8">QUANTITY</span></th>

              <th width="7%"> <span class="style8">SIZE </span></th>

              <th width="7%"> <span class="style8">PRICE (per cartoon) </span></th>

              <th width="8%"> -</th>

              <?php

      foreach ($_SESSION['cart'] as $key => $general_cartoon) {

    ?>

            </tr>

            <tr>

              <td><span class="style19"><?php echo $_SESSION['cart'][$key] ['cartoon_id']; ?></span></td>

              <td><span class="style19"><?php echo $_SESSION['cart'][$key] ['title']; ?></span></td>

              <td><span class="style19"><?php echo $_SESSION['cart'][$key] ['quantity'];?></span></td>

              <td><span class="style19"><?php echo $_SESSION['cart'][$key] ['size'];?></span></td>

              <td><span class="style19"><?php echo $_SESSION['cart'][$key] ['price'];?></span></td>

              <?php $quantity =$_SESSION['cart'][$key]['quantity'];?>

              <td><a href="removefromcart.php?cartoon_id=<?php echo $_SESSION['cart'][$key]['cartoon_id'];?>&location=<?php echo $_SERVER['PHP_SELF'];?>">[REMOVE]</a></td>

    </tr>

 

            <?php }?>

          </table>

  <p>

            <?php }

    else { ?>

No cartoon chosen yet

<?php }

  ?>

</p>

I would REALLY recommend not making your own shopping cart with your knowledge of PHP, if you are going to be processing transactions you will need to be secure. I would recommend getting OSCommerce/Zen Cart and then building it into your template.

try throwing

 

echo count($_SESSION['cart']);

 

at the top of the page after the check if cart is set. That should print out how many items are there.

 

thatsgreat2345: shopping carts do not need to be secure.

 

To Helmet,

 

Could I use this statement on very page on the site

-So each time the user adds a item to the cart, the  total amount would increase and it would be visible for the user to see wherever they are.

 

Thank you for your understanding of my level in php.

Test it to answer your question, and read up about session variables in php.

 

I misread thatsgreat's comment before.. he's right that if you are planning to take credit card payments on your site you should probably integrate some prefab software

To Helmet again,

 

You are a true life saver, it worked!

 

Thank yoooooooooooooou so much, for not commenting on my knowledge and understanding in php, unlike some people on this site

 

Im just building sample website,(its a project) it not going to be hosted, so security would not be needed.

 

I will always have you in mind for next time.

have a great day!

Why are you talking about security related to shopping cart and shopping cart related to credit card payments?

 

In the end, unless callisto11 is planing on running his own payment gateway, he will just ask the customer to verify that he is about to pay a certain amount of money before passing that number along with card details to an already established payment gateway.

 

In my opinion all which needs to be secure (sent via SSL) is the transfer of card details to the payment gateway. Of course the shopping cart shouldn't let people take control of the site or harm the site in any other way, but should someone decide to tamper with the content of the cart then the user would see the new amount of money and disagree, right?

 

Just my 25 øre (around 5 cents)

This is irrelevant, but what I was saying is this:

 

1)shopping carts do not have to be secure

2)if he plans on having people enter their card number on his site, it looks like he should use solid software

 

For a newbie to php the recommended do-it-yourself method of doing ecommerce would be passing the array to paypal or equivalent and letting them take the credit card details.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.