473,799 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2286
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_nam e"];
$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
"WindAndWav es" <ac****@ngaru.c om> wrote in message
news:mk******** ***********@new s.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.c om> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.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_nam e"];
$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.xt ra.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_nam e"];
$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.c om...
I noticed that Message-ID: <4W************ *******@news.xt ra.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("c onnectDB.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($sq ladd);
$myrowAdd = mysql_fetch_arr ay($resultAdd);

// Get the Region Name
$regionId = $myrowAdd["regionId"];
$sqladd = "SELECT description FROM regions WHERE id = '$regionId'";
$resultAdd2 = mysql_query($sq ladd);
$myrowAdd2 = mysql_fetch_arr ay($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]["regiondesc r"] = $myrowAdd2["descriptio n"];
$_SESSION["cart"][$sortorder]["descriptio n"] = $myrowAdd["descriptio n"];
$_SESSION["cart"][$sortorder]["photolink"] = $myrowAdd["photolink"];
$_SESSION["cart"][$sortorder]["FN"] = $myrowAdd["FN"];

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

// sql = stripslashes($H TTP_POST_VARS[sqlS]);

$result = mysql_query($sq l);
$numrows = mysql_num_rows( $result);
//$locationer = 'results.php?PH PSESSID='. session_id().'' ;
//header("Locatio n: $locationer");
}
elseif ($HTTP_POST_VAR S[regSubmit] || $HTTP_POST_VARS[advSubmit]){
// Select properties from regions for normal and advanced search...
include_once("c onnectDB.php");
$sql = $sqlStart;
$sql .= $sqlWhere;
if(count($searc h) < 1){
............... ............... .........etc
}
if($HTTP_POST_V ARS[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

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

Similar topics

7
1951
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 quantity of this item is, let's say 30. You have one in your cart, so how many are available: 1) 29 2) 30 Now let's say that while I ordered one small black t-shirt, Sven ordered 1 small black t-shirt, Anders ordered one black t-shirt, and...
2
2869
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 im going. Just a couple of hints will do, Im familiar with vb script but not java based code , and im wondering how to store what they select so I can move around different product ranges. Don
12
3810
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 http://www.mysite.com/index.htm?location=shop&view=cart&action=addtocart&productid=3 after i press this link the site cart.php will be included and add the wanted product to the cart, and show the cart.
3
1420
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 code what i need. Please please please could someone please provide me with help, or coding on how to add item details into a basket record, once the user clicks the add to basket link.
3
2927
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 code what i need. Please please please could someone please provide me with help, or coding on how to add item details into a basket record, once the user clicks the add to basket link. *** Sent via Developersdex http://www.developersdex.com...
7
2644
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, then I want to remove it. I'm doing the following script: For Each ele In bag Dim qty As Integer = CType(gvBag.Rows.Item(cnt).FindControl("txtQty"),
1
2108
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 for my customer. I found some PayPal ASP.Net controls to be used with .net studio. Basically, I want the user to be able to shop on my website, add items to my shopping cart and at the checkout to be taken to the PayPal website. I would like...
15
4301
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 functionality on. However, first I want to make sure it works if javascript is not available. So when your on the shopping cart page, you can update the quantities then click Update Cart, which would update the totals, and refresh the shopping cart page,...
8
2608
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 href="ShoppingCart.php?action=remove_item&id=<?php echo $row; ?>">Remove</a> My problem is that i want to include a link that when clicked will clear all items and not just specific ones that are chosen by the car_id
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.