By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,078 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,078 IT Pros & Developers. It's quick & easy.

number of items in a cart

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
.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

P: n/a
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

P: n/a
.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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.