Jump to content

Recommended Posts

I have a page called item.php

<?php
include "basket.inc";
//The object need to be here, first of any output to the browser
// Refer to cookie reasons
$b=new basket();
switch($action){
case "A":
$b->additem($id,1);
break;
case "R":
$b->removeitem($id);
break;
}

?>

<body text="#000080" link="#0000FF" vlink="#800080" alink="#FF0000" bgcolor="#FFFFFF">
<pre>
ID	ITEM		Price
23	Notebook 	3500	<a href="<?php echo $PHP_SELF; ?>?action=A&id=23&q=1">Add</a>
10	Pad		10	<a href="<?php echo $PHP_SELF; ?>?action=A&id=10&q=1">Add</a>
</pre>
<pre>
<?php

if(!$b->empty) {
echo "Items in your shopping cart:<br>ID	ITEM	Q<br>";
while(list($x,$y)=each($b->items))
{ echo "$x	NN	$y	<a href=\"$PHP_SELF?action=R&id=$x\">Remove</a><br>";
}
}
?></pre>

 

and then basket.inc

<?php
/*
Basket class for e-commerce purpose

Version : 0.5
Type:Class
Category:Shopping Carts
License: GNU General Public License

Description: This class provides methods for add, remove, update and remove all items
from a basket. The basket works with a session cookie, saving the product ID
and quantity in an array for simple accesing it.
There's an example in this category... called "Basket example using basket class"
*/

class basket {
var $items;
var $empty;

function basket()
{
	global $cart;
	if(isset($cart))
	{
		$this->items=unserialize(stripslashes($cart));
		if ( gettype($this->items) == "array" )
		{
			$this->empty=false;
		}
		else
			$this->empty=true;
	}
	else
		$this->empty=true;
}

function additem($id, $name, $count, $prix)
{
if ($this->items[$id][1] > 0 )
{
	$this->items[$id][1]+=$count;
}
else
{
	$this->items[$id][0]=$name;
	$this->items[$id][1]=$count;
	$this->items[$id][2]=$prix;
	$this->items[$id][3]=stripslashes($name);
}
setcookie("cart",serialize($this->items),0,"/");
$this->empty=false;
}

function removeitem($id) {
$tmp="iwphoto";	// Don't forget to initialize !
if(!$this->empty)  {
	while(list($x,$y)=each($this->items)){
		if($x!=$id) $tmp[$x]=$y;
	};
};
$this->items=$tmp;	// Or this will have no effect !
setcookie("cart",serialize($this->items),0,"/");
if(count($this->items)==0) $this->empty=true;
}

function resetArray($basket)
{
reset($basket->items);
}

function countItems($basket)
{
if(!$basket->empty)
{
	while ( list($x,$y,) = each($basket->items) )
	{
		$ant++;
	}
}
return $ant;
}
function sumItems($basket)
{
if(!$basket->empty)
{
	while ( list($x,$y,) = each($basket->items) )
	{
		$ant = $ant + $y[1];
	}
}
return $ant;
}
function printItems($b)
{
if(!$b->empty)
{
	while ( list($x,$y,) = each($b->items) )
	{
		echo "$x     ".$y[0]."   ".$y[1]."  <a href=\"$PHP_SELF?action=R&id=$x\">Remove</a><br>";
	}
}
}
function updateitem($id,$count){
$this->items[$id][1]=$count;
setcookie("cart",serialize($this->items),0,"/");
}

function removeall(){
setcookie("cart",NULL,0,"/");
$this->empty=true;
}

}
?>

 

can anyone see why this wouldnt work?

it doesnt seem able to add anything to the basket?

Link to comment
https://forums.phpfreaks.com/topic/65723-very-very-simple-basket-script-error/
Share on other sites

<?php
include "basket.inc";
//The object need to be here, first of any output to the browser
// Refer to cookie reasons
$b=new basket();
$action = $_GET['action'];  //ADD THIS LINE
$id= $_GET['id'];  //ADD THIS LINE
switch($action){
case "A":
$b->additem($id,1);
break;
case "R":
$b->removeitem($id);
break;
}

ah ha,

 

this has sort of worked, i click on the first item and it shows:

 

ID ITEM Q

10 NN Array

 

- the only thing is, it will only allow one item in the basket, and it doesnt knoe the item number nor the cost.. how can i sort that?

 

Thanks for you help :)

All i can suggect is read the info that came with the class or create your own.. i'll admit i have seen better!

 

add item syntax

additem($id, $name, $count, $prix)

 

update quantity(q=) & price(p=)

<?php
include "basket.inc";
//The object need to be here, first of any output to the browser
// Refer to cookie reasons
$b=new basket();
$action = $_GET['action'];  //ADD THIS LINE
$id= $_GET['id'];  //ADD THIS LINE
$q= $_GET['q'];  //ADD THIS LINE
$p= $_GET['p'];  //ADD THIS LINE
switch($action){
case "A":
$b->additem($id,1, $q, $p); //EDIT
break;
case "R":
$b->removeitem($id);
break;
}

All i can suggect is read the info that came with the class or create your own.. i'll admit i have seen better!

 

add item syntax

additem($id, $name, $count, $prix)

 

update quantity(q=) & price(p=)

 

hmm, ok, i didnt get any info with it, and i dont think i know enough to create one, basically this is to hold about 3 or 4 bits of info, for up to 500 items per sale...

 

Possible this wont handle it then, i might have to see what else would do the job.

 

I already have loads of system in place, ie, payment methods accepted are in a database and the products are in a database, each item has 4 or 5 options, all pulled out of a database and shown on a page, so i was hoping to pass the same variables through in to a basket, allowing people to order the item...

 

the system is working, in a very basic form, its just the basket im struggling with.

erm.. well it uses setcookie that can only be set from the header..

 

what about an mySQL basket  ?

i haven't looked at this in detail but the description looks ok

This package implements a shopping basket for e-commerce sites that stores the products picked by the user in a MySQL database.

 

The class can keep track of the session associated with the current user shopping basket, add and remove individual products, empty the basket, change the quantity of each product, calculate the cost of all products in the basket, and dump a report of all products in the basket in HTML.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.