Connecting Tech Pros Worldwide Help | Site Map

number of items in a cart

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 10:47 AM
WindAndWaves
Guest
 
Posts: n/a
Default 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



  #2  
Old July 17th, 2005, 10:47 AM
craze3
Guest
 
Posts: n/a
Default Re: number of items in a cart

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
xdeathgrinderx@yahoo.com
aim: punkcraze3

  #3  
Old July 17th, 2005, 10:47 AM
Aidan
Guest
 
Posts: n/a
Default Re: number of items in a cart

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" <access@ngaru.com> wrote in message
news:mk%Gd.8904$mo2.676049@news.xtra.co.nz...[color=blue]
> 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
>
>[/color]


  #4  
Old July 17th, 2005, 10:47 AM
WindAndWaves
Guest
 
Posts: n/a
Default Re: number of items in a cart


"craze3" <craze3@gmail.com> wrote in message news:1106020260.760285.234130@f14g2000cwb.googlegr oups.com...[color=blue]
> 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[/color]


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?


  #5  
Old July 17th, 2005, 10:47 AM
Geoff Berrow
Guest
 
Posts: n/a
Default Re: number of items in a cart

I noticed that Message-ID: <4W0Hd.8971$mo2.676934@news.xtra.co.nz> from
WindAndWaves contained the following:
[color=blue]
>Would your code still work if I used something else rather then a cookie?[/color]

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/
  #6  
Old July 17th, 2005, 10:47 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: number of items in a cart

.oO(craze3)
[color=blue]
>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.";
>?>[/color]

A session cookie only holds the session ID, nothing more.
[color=blue]
>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.[/color]

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

Micha
  #7  
Old July 17th, 2005, 10:47 AM
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
Default Re: number of items in a cart

Michael Fesser wrote:
<snip>[color=blue]
> Actually you don't want to store shopping cart data in a cookie, it's
> unreliable and insecure. Use sessions instead.[/color]

Ummm???

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

  #8  
Old July 17th, 2005, 10:48 AM
Michael Fesser
Guest
 
Posts: n/a
Default Re: number of items in a cart

.oO(R. Rajesh Jeba Anbiah)
[color=blue]
>Michael Fesser wrote:
><snip>[color=green]
>> Actually you don't want to store shopping cart data in a cookie, it's
>> unreliable and insecure. Use sessions instead.[/color]
>
>Ummm???[/color]

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
  #9  
Old July 17th, 2005, 10:48 AM
WindAndWaves
Guest
 
Posts: n/a
Default Re: number of items in a cart


"Geoff Berrow" <bl@ckdog.co.uk> wrote in message news:6idpu099vor6v0m51g7a9s3u51b20iauf4@4ax.com...[color=blue]
> I noticed that Message-ID: <4W0Hd.8971$mo2.676934@news.xtra.co.nz> from
> WindAndWaves contained the following:
>[color=green]
> >Would your code still work if I used something else rather then a cookie?[/color]
>
> Care to tell us what or is it a secret? We could guess all day.[/color]

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>



  #10  
Old July 17th, 2005, 10:48 AM
craze3
Guest
 
Posts: n/a
Default Re: number of items in a cart

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?

  #11  
Old July 17th, 2005, 10:48 AM
WindAndWaves
Guest
 
Posts: n/a
Default Re: number of items in a cart


"craze3" <craze3@gmail.com> wrote in message news:1106110482.764755.233860@z14g2000cwz.googlegr oups.com...[color=blue]
> 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?
>[/color]

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).


  #12  
Old July 17th, 2005, 10:48 AM
Geoff Berrow
Guest
 
Posts: n/a
Default Re: number of items in a cart

I noticed that Message-ID: <eriHd.9206$mo2.697155@news.xtra.co.nz> from
WindAndWaves contained the following:
[color=blue][color=green]
>> Care to tell us what or is it a secret? We could guess all day.[/color]
>
>here is the code I use... I did not write it, but it seems to work....[/color]

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/
  #13  
Old July 17th, 2005, 10:48 AM
WindAndWaves
Guest
 
Posts: n/a
Default Re: number of items in a cart


"Geoff Berrow" <bl@ckdog.co.uk> wrote in message news:ks3su01eeciuc0d82edfis7f694n78mob5@4ax.com...[color=blue]
> I noticed that Message-ID: <eriHd.9206$mo2.697155@news.xtra.co.nz> from
> WindAndWaves contained the following:
>[color=green][color=darkred]
> >> Care to tell us what or is it a secret? We could guess all day.[/color]
> >
> >here is the code I use... I did not write it, but it seems to work....[/color]
>
> Have you tried :
>
> $count =count($_SESSION['cart']);
> print "There are $count item(s) in the cart";
>[/color]


Hi Geoff

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

Thanks


  #14  
Old July 17th, 2005, 10:48 AM
Geoff Berrow
Guest
 
Posts: n/a
Default Re: number of items in a cart

I noticed that Message-ID: <iIrHd.9419$mo2.708350@news.xtra.co.nz> from
WindAndWaves contained the following:
[color=blue]
>
>Hi Geoff
>
>It seems to be working fine now! Great - awesome in fact.[/color]

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/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.