Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sessions and arrays in a shopping cart

0 views
Skip to first unread message

Hades

unread,
Jan 13, 2003, 12:07:54 PM1/13/03
to
I've been creating a new website for a friend and am currently stuck on the
shopping cart part of the site.

I have a products table in mySQL database and the idea of this particular
script is that it recieves a product_id via POST, adds it to the list of
product id's and then displays all the products in the list (as a shopping
cart). It must then reset the session variable with the new list.

I'm also using the PHP-LIB template class for code seperation.

The problem I'm having is that the "foreach" loop isn't recognising the
array as an array (and even if I test it with is_array() it tells me it's
not an array) even though I have at the very least initialised it as an
empty array.

Can anyone help me with this problem as it is the only sticking point I've
come against on this site. Thanks in advance.

Lee Conlin


##### Begin Code #####

session_start();

$products_in_basket = array();

//
// Get current basket
//
if(isset($_SESSION['products_in_basket'])) {
$products_in_basket = explode(',', $_SESSION['products_in_basket']);
}

function show_page() {
GLOBAL $tpl;

$tpl->set_block('MAIN', 'basket_row', 'BASKET_ROW');
foreach($products_in_basket as $product_id) {
//
// Get item data to display
//
$sql = "SELECT product_id, product_name, product_price
FROM products
WHERE product_id = $product_id";
$list = DB($sql);

while($row = mysql_fetch_array($list, MYSQL_ASSOC)) {
$tpl->set_var(array( 'PRODUCT_ID' => $row['product_id'],
'PRODUCT_NAME' => $row['product_name'],
'PRODUCT_PRICE' => $row['product_price']));
$tpl->parse('BASKET_ROW', 'basket_row', TRUE);
}
}
}

switch($_GET['mode']){
case 'add':
$products_in_basket[] = $_POST['product_id'];
show_page();
break;
default:
show_page();
break;
}
//
// Reset session var with new list of product_id's
//
$_SESSION['products_in_basket'] = implode(',', $products_in_basket);

##### End Code #####


ofor

unread,
Jan 13, 2003, 2:24:50 PM1/13/03
to

"Hades" <newg...@hades.me.uk> skrev i meddelandet
news:JnCU9.33$G6X4....@news2.randori.com...
Hi
You might want to look at

http://www.pgmarket.com

ofor


Sundial Services

unread,
Jan 13, 2003, 2:29:02 PM1/13/03
to
ofor wrote:

>
> "Hades" <newg...@hades.me.uk> skrev i meddelandet
> news:JnCU9.33$G6X4....@news2.randori.com...
>> I've been creating a new website for a friend and am currently stuck on
> the
>> shopping cart part of the site.

>> [...]


>>
>>
> Hi
> You might want to look at
>
> http://www.pgmarket.com
>

Yes indeed... "a shopping cart" is something that has been done to death,
so why do it again? Grab something that you can use, then adapt it to your
purposes.

----------------------------------
Fast automatic Paradox/Delphi table repair at a click of a mouse!
http://www.sundialservices.com/products/chimneysweep

Hades

unread,
Jan 13, 2003, 6:40:37 PM1/13/03
to
Sundial Services <info...@sundialservices.com> wrote in message news:<v264rua...@corp.supernews.com>...

> ofor wrote:
>
> >
> > "Hades" <newg...@hades.me.uk> skrev i meddelandet
> > news:JnCU9.33$G6X4....@news2.randori.com...
> >> I've been creating a new website for a friend and am currently stuck on
> the
> >> shopping cart part of the site.
> >> [...]
> >>
> >>
> > Hi
> > You might want to look at
> >
> > http://www.pgmarket.com

This URL takes me to a site about an indian market. I can't find
anything there even php related, let alone a shopping cart.

The main reason I asked about my problem is that I have a particular
structure for both the database and site and having searched
hotscripts.com not found anything that was even close to what I
wanted. I've already got an items database with different categories
and the admin side. The only bit I need now is the bit for the cart
it's self.

Any one know of anything that would help, or a cart class that would
do the job?

Thanks

Woll

unread,
Jan 13, 2003, 10:48:24 PM1/13/03
to
If you are/anyone is intersted that URL should have been:

http://www.pgmarket.net


Hades

unread,
Jan 14, 2003, 8:50:02 AM1/14/03
to

"Woll" <pw[spamtrap]@studentdigs.org> wrote in message
news:cMLU9.2622$G_7.22...@news-text.cableinet.net...

> If you are/anyone is intersted that URL should have been:
>
> http://www.pgmarket.net
>

Thanks.

I had a look at it and it seems pretty good. But I already have most of the
site (including catalogue) done. I was just looking for a class that would
deal with item_id's and quantities.

I found one on HotScripts.com called MyMarket that was based on a devshed
article about creating shopping carts. Handy really that I'd already read
that article :o)

Again, thankyou for your help.

Lee Conlin


Pete H

unread,
Jan 18, 2003, 4:40:31 PM1/18/03
to
get the basket_lib class from www.phpclasses.org

"Hades" <newg...@hades.me.uk> wrote in message
news:JnCU9.33$G6X4....@news2.randori.com...

0 new messages