473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

number of items in a cart

Hi Folks

I am working with a session and a 'cart' (note that I do not really know what I am doing though).

I was wondering if there is some easy way (function) to work out how many items I have in my 'basket' / 'cart', because then I could
call this function from anywhere to show the user just how many items are on order

TIA

- Newbie Nicolaas
Jul 17 '05 #1
13 2254
you need to create an array of the session cookie that is set. You need
to do something along the lines of...
--
<?
$cookie = $_COOKIE["cookie_name"];
$explode = explode("<br>", $cookie);
$count = count($explode);
echo "You currently have <b>$count</b> item(s) in your cart.";
?>
--
What that will do is get the information from the cookie (supply
cookie_name) and divide at every line break, then count how many
individual pieces there are. Then it echoes the ammount of pieces. You
might have to replace "<br>" with whatever you use to split the items
in the cart/cookie.
--
craze3
xd************@yahoo.com
aim: punkcraze3

Jul 17 '05 #2
If the items are stored in an array structure, then you could simply use the
sizeof() function on said array to determine the number of items in the
array...

eg:

<?
$cart[0] = "item1";
$cart[1] = "item2";
$cart[2] = "item3";

$numItems = sizeof($cart);
?>

$numItems should contain the number of items in your cart.

HTH
"WindAndWaves" <ac****@ngaru.com> wrote in message
news:mk*******************@news.xtra.co.nz...
Hi Folks

I am working with a session and a 'cart' (note that I do not really know
what I am doing though).

I was wondering if there is some easy way (function) to work out how many
items I have in my 'basket' / 'cart', because then I could
call this function from anywhere to show the user just how many items are
on order

TIA

- Newbie Nicolaas

Jul 17 '05 #3

"craze3" <cr****@gmail.com> wrote in message news:11**********************@f14g2000cwb.googlegr oups.com...
you need to create an array of the session cookie that is set. You need
to do something along the lines of...
--
<?
$cookie = $_COOKIE["cookie_name"];
$explode = explode("<br>", $cookie);
$count = count($explode);
echo "You currently have <b>$count</b> item(s) in your cart.";
?>
--
What that will do is get the information from the cookie (supply
cookie_name) and divide at every line break, then count how many
individual pieces there are. Then it echoes the ammount of pieces. You
might have to replace "<br>" with whatever you use to split the items
in the cart/cookie.
--
craze3

Hi Craze # 3

Thank you very much- believe me or not - I don't actually have a cookie set ... all I do is pass the session ID on from link to
link - I think. It seems to work so far anyway....

Would your code still work if I used something else rather then a cookie?
Jul 17 '05 #4
I noticed that Message-ID: <4W*******************@news.xtra.co.nz> from
WindAndWaves contained the following:
Would your code still work if I used something else rather then a cookie?


Care to tell us what or is it a secret? We could guess all day.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
.oO(craze3)
you need to create an array of the session cookie that is set. You need
to do something along the lines of...
--
<?
$cookie = $_COOKIE["cookie_name"];
$explode = explode("<br>", $cookie);
$count = count($explode);
echo "You currently have <b>$count</b> item(s) in your cart.";
?>
A session cookie only holds the session ID, nothing more.
What that will do is get the information from the cookie (supply
cookie_name) and divide at every line break, then count how many
individual pieces there are. Then it echoes the ammount of pieces. You
might have to replace "<br>" with whatever you use to split the items
in the cart/cookie.


Actually you don't want to store shopping cart data in a cookie, it's
unreliable and insecure. Use sessions instead.

Micha
Jul 17 '05 #6
Michael Fesser wrote:
<snip>
Actually you don't want to store shopping cart data in a cookie, it's
unreliable and insecure. Use sessions instead.


Ummm???

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #7
.oO(R. Rajesh Jeba Anbiah)
Michael Fesser wrote:
<snip>
Actually you don't want to store shopping cart data in a cookie, it's
unreliable and insecure. Use sessions instead.


Ummm???


What if cookies are not available? And do you really want to revalidate
all cookie data over and over again on every page request before using
it? Remember: Everything coming in from client-side can be manipulated.
It's quite easy to send "customized" cookie content back to the server.

Micha
Jul 17 '05 #8

"Geoff Berrow" <bl@ckdog.co.uk> wrote in message news:6i********************************@4ax.com...
I noticed that Message-ID: <4W*******************@news.xtra.co.nz> from
WindAndWaves contained the following:
Would your code still work if I used something else rather then a cookie?


Care to tell us what or is it a secret? We could guess all day.


here is the code I use... I did not write it, but it seems to work.... Note that each page passes on the session ID to the next....

<?php
// Establish a session
session_start();
include_once("connectDB.php");

// Initialize variables
$errormessage = "";
$sqlStart = "SELECT a.id, a.FN, a.name, a.description, a.photolink, a.regionId FROM friars a, regions b";
$sqlWhere = " WHERE (a.regionId = b.id) AND ";
$sqlEnd = " GROUP BY a.regionId, a.name ";
$humanSQL = "<UL>";
$maxrows = 50;
$snum = 1;
$sql = "";
$name = "";
$basketcount = 0;

// If no sortorder is set, initialize it to 1
if(!$sortorder){
$_SESSION["sortorder"] = 1;
}

// ADD TO TRIP
if($add){

// Add accomodation to the session with an incremented sortorder
$_SESSION["sortorder"] = $sortorder + 1;

$id = $add;

// Select DB data for accommodation to ADD and Append to Session
$sqladd = "SELECT * FROM friars WHERE id = $id";
$resultAdd = mysql_query($sqladd);
$myrowAdd = mysql_fetch_array($resultAdd);

// Get the Region Name
$regionId = $myrowAdd["regionId"];
$sqladd = "SELECT description FROM regions WHERE id = '$regionId'";
$resultAdd2 = mysql_query($sqladd);
$myrowAdd2 = mysql_fetch_array($resultAdd2);

//get name
$name = $myrowAdd["name"];

// Add name and id to the session
$_SESSION["cart"][$sortorder]["id"] = $id;
$_SESSION["cart"][$sortorder]["name"] = $name;
$_SESSION["cart"][$sortorder]["regionId"] = $myrowAdd["regionId"];
$_SESSION["cart"][$sortorder]["regiondescr"] = $myrowAdd2["description"];
$_SESSION["cart"][$sortorder]["description"] = $myrowAdd["description"];
$_SESSION["cart"][$sortorder]["photolink"] = $myrowAdd["photolink"];
$_SESSION["cart"][$sortorder]["FN"] = $myrowAdd["FN"];

// GET THE SQL back
$sql = $_SESSION["sqlr"][$snum]["sqler"];

// sql = stripslashes($HTTP_POST_VARS[sqlS]);

$result = mysql_query($sql);
$numrows = mysql_num_rows($result);
//$locationer = 'results.php?PHPSESSID='. session_id().'';
//header("Location: $locationer");
}
elseif ($HTTP_POST_VARS[regSubmit] || $HTTP_POST_VARS[advSubmit]){
// Select properties from regions for normal and advanced search...
include_once("connectDB.php");
$sql = $sqlStart;
$sql .= $sqlWhere;
if(count($search) < 1){
.......................................etc
}
if($HTTP_POST_VARS[advSubmit]) { // ADVANCED SEARCH
.......................................etc
}
<html.................><A HREF="...?add=[propertycode]&amp;.....>......................./html>

Jul 17 '05 #9
correct, you do not want to store any important information in a cookie
but this guy is so i was just helpn him out. I dont really use cookies
alot so yea...

--
"Thank you very much- believe me or not - I don't actually have a
cookie set ... all I do is pass the session ID on from link to
link - I think. It seems to work so far anyway...."
-WindandWaves
--

Im kind of confused but do you mean you do something like this:
http://www.site.com/shop.php?item1=s...rk?item3=knife
is that what your doing? Passing the variables through the URL and then
just GET-ing them?

Jul 17 '05 #10

"craze3" <cr****@gmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
correct, you do not want to store any important information in a cookie
but this guy is so i was just helpn him out. I dont really use cookies
alot so yea...

--
"Thank you very much- believe me or not - I don't actually have a
cookie set ... all I do is pass the session ID on from link to
link - I think. It seems to work so far anyway...."
-WindandWaves
--

Im kind of confused but do you mean you do something like this:
http://www.site.com/shop.php?item1=s...rk?item3=knife
is that what your doing? Passing the variables through the URL and then
just GET-ing them?


I dont think I am using cookies - sorry if I have given you that impression.... I am just such a novice that I am not 100% sure.
All I do is the session start command and not much more (see code in another post).
Jul 17 '05 #11
I noticed that Message-ID: <er*******************@news.xtra.co.nz> from
WindAndWaves contained the following:
Care to tell us what or is it a secret? We could guess all day.


here is the code I use... I did not write it, but it seems to work....


Have you tried :

$count =count($_SESSION['cart']);
print "There are $count item(s) in the cart";

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #12

"Geoff Berrow" <bl@ckdog.co.uk> wrote in message news:ks********************************@4ax.com...
I noticed that Message-ID: <er*******************@news.xtra.co.nz> from
WindAndWaves contained the following:
Care to tell us what or is it a secret? We could guess all day.


here is the code I use... I did not write it, but it seems to work....


Have you tried :

$count =count($_SESSION['cart']);
print "There are $count item(s) in the cart";

Hi Geoff

It seems to be working fine now! Great - awesome in fact.

Thanks
Jul 17 '05 #13
I noticed that Message-ID: <iI*******************@news.xtra.co.nz> from
WindAndWaves contained the following:

Hi Geoff

It seems to be working fine now! Great - awesome in fact.


Just goes to show, it's not the answers that are difficult, it's the
questions... ;-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil Powell | last post by:
You have ordered one item and placed it into your cart. Let's say you ordered one small black t-shirt. Your cart will have the product_id, color_id and size_id for the small black t-shirt. The...
2
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
12
by: Josef Blösl | last post by:
hi folks! i have a cart link in all of my listed products on the site. when i press this link i will generate a url like this ...
3
by: nike | last post by:
please i need help. I am doing a wed site online shopping system for my school project, which requires some asp coding using vb script. I am new to asp and have no knowledge whatsoever on how to...
3
by: help | last post by:
please i need help. I am doing a wed site online shopping system for my school project, which requires some asp coding using vb script. I am new to asp and have no knowledge whatsoever on how to...
7
by: David Lozzi | last post by:
Howdy, I have a shopping cart in my arraylist. Works great for adding items but I'm displaying the shopping cart in a gridview and the user can update the qty of the items. If they set it to 0,...
1
by: Vahehoo | last post by:
Hi, I have an ASP .Net e-business site that is built using DNN 2.0. I am having troubles passing my shopping cart items to PayPal. I implemented a total paynow button, but it was not good enough...
15
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
8
by: mtgriffiths86 | last post by:
Hi All, I am trying to remove all items from my shopping cart but am having problems. I can remove individual items by using clicking a link which runs this piece of code: <a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.