Shopping cart

Shopping cart

am 16.05.2009 11:54:46 von Vernon St Croix

------=_NextPart_000_005D_01C9D614.B9C18FD0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hi,

I am pretty new to PHP and I am trying to create a shopping cart.=20

I keep on getting the below error when trying to show the shopping list. =


Any guidance that can be provided will be very much appreciated

Fatal error: Call to a member function query() on a non-object in =
C:\wamp\www\draft\basket.php on line 36

mysql_connect.php
$con =3D mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("rum", $con);
?>

basket.php
include("mysql.class.php");
include ("header.php");
include ("mysql_connect.php");
include ("functions.php");
?>


Rum Basket


echo writeCart();
?>


Rum on Offer


=20
$sql=3D 'SELECT * FROM spirits BY id';
$result =3D $con->query($sql);
$output[]=3D '';
echo join ('', $output);
?>



include("footer.html");

?>


cart.php


include ("header.php");
=20
include ("mysql_connect.php");
=20
include ("functions.php");
=20


$cart =3D $_SESSION['cart'];


if(isset($_GET["action"]))
{ $action =3D $_GET["action"]; }
else
{ $action =3D ""; }


switch ($action) {
case 'add':
if ($cart) {
$cart .=3D ','.$_GET['id'];
} else {
$cart =3D $_GET['id'];
}
break;
case 'delete':
if ($cart) {
$items =3D explode(',',$cart);
$newcart =3D '';
foreach ($items as $item) {
if ($_GET['id'] !=3D $item) {
if ($newcart !=3D '') {
$newcart .=3D ','.$item;
} else {
$newcart =3D $item;
}
}
}
$cart =3D $newcart;
}
break;
case 'update':
if ($cart) {
$newcart =3D '';
foreach ($_POST as $key=3D>$value) {
if (stristr($key,'qty')) {
$id =3D str_replace('qty','',$key);
$items =3D ($newcart !=3D '') ? explode(',',$newcart) : =
explode(',',$cart);
$newcart =3D '';
foreach ($items as $item) {
if ($id !=3D $item) {
if ($newcart !=3D '') {
$newcart .=3D ','.$item;
} else {
$newcart =3D $item;
}
}
}
for ($i=3D1;$i<=3D$value;$i++) {
if ($newcart !=3D '') {
$newcart .=3D ','.$id;
} else {
$newcart =3D $id;
}
}
}
}
}
$cart =3D $newcart;
break;
}
$_SESSION['cart'] =3D $cart;

?>



Rum Basket



echo writeCart();
?>





Please Check Quantities...



echo showCart();
?>








include("footer.html");

?>

functions.php

function writeCart() {
$cart =3D $_SESSION['cart'];
if (!$cart) {
return '

There is no alcohol in your Rum Basket

';
} else {
// Parse the cart session variable
$items =3D explode(',',$cart);
$s =3D (count($items) > 1) ? 's':'';
return '

There are'.count($items).' item'.$s.' =
in your rum basket

';
}
}
=20
function showCart() {
$cart =3D $_SESSION['cart'];
if ($cart) {
$items =3D explode(',',$cart);
$contents =3D array();
foreach ($items as $item) {
$contents[$item] =3D (isset($contents[$item])) ? $contents[$item] + 1 =
: 1;
}
$output[] =3D '
method=3D"post" id=3D"cart">';
$output[] =3D '';
foreach ($contents as $id=3D>$qty) {
$sql =3D 'SELECT * FROM spirits WHERE id =3D '.$id;
$result =3D $con->query($sql);
$row =3D $result->fetch();
extract($row);
$output[] =3D '';
$output[] =3D '';
$output[] =3D '';
$output[] =3D '';
$output[] =3D '';
$output[] =3D '';
$total +=3D $price * $qty;
$output[] =3D '';
}
$output[] =3D '
class=3D"r">Remove'.$name.'£'.$price.' value=3D"'.$qty.'" size=3D"3" maxlength=3D"3" >£'.($price * $qty).'
';
$output[] =3D '

Grand total: £'.$total.'

';
$output[] =3D '
';
$output[] =3D '
';
} else {
$output[] =3D '

You shopping cart is empty.

';
}
return join('',$output);
}

?>




Many Thanks

Vee
------=_NextPart_000_005D_01C9D614.B9C18FD0--

Re: Shopping cart

am 16.05.2009 15:02:23 von Max

On Sat, 2009-05-16 at 10:54 +0100, Vernon St Croix wrote:
> Hi,
>
> I am pretty new to PHP and I am trying to create a shopping cart.
>
> I keep on getting the below error when trying to show the shopping list.
>
> Any guidance that can be provided will be very much appreciated
>
> Fatal error: Call to a member function query() on a non-object in C:\wamp\www\draft\basket.php on line 36
>
> mysql_connect.php
> > $con = mysql_connect("localhost","root","");
> if (!$con)
> {
> die('Could not connect: ' . mysql_error());
> }
> mysql_select_db("rum", $con);
> ?>
>
> basket.php
> > include("mysql.class.php");
> include ("header.php");
> include ("mysql_connect.php");
> include ("functions.php");
> ?>
>


>

Rum Basket


> > echo writeCart();
> ?>
>

>

>

Rum on Offer


> >
> $sql= 'SELECT * FROM spirits BY id';
> $result = $con->query($sql);
> $output[]= '
    ';
    > while ($row = $result->fetch()) {
    > $output[] = '
  • '.$row['name'].': £'.$row['price'].'
    Add to Cart
  • ';
    > }
    > $output[] = '
';
> echo join ('', $output);
> ?>
>

>
>
> > include("footer.html");
>
> ?>
>
>
> cart.php
>
> >
> include ("header.php");
>
> include ("mysql_connect.php");
>
> include ("functions.php");
>
>
>
> $cart = $_SESSION['cart'];
>
>
> if(isset($_GET["action"]))
> { $action = $_GET["action"]; }
> else
> { $action = ""; }
>
>
> switch ($action) {
> case 'add':
> if ($cart) {
> $cart .= ','.$_GET['id'];
> } else {
> $cart = $_GET['id'];
> }
> break;
> case 'delete':
> if ($cart) {
> $items = explode(',',$cart);
> $newcart = '';
> foreach ($items as $item) {
> if ($_GET['id'] != $item) {
> if ($newcart != '') {
> $newcart .= ','.$item;
> } else {
> $newcart = $item;
> }
> }
> }
> $cart = $newcart;
> }
> break;
> case 'update':
> if ($cart) {
> $newcart = '';
> foreach ($_POST as $key=>$value) {
> if (stristr($key,'qty')) {
> $id = str_replace('qty','',$key);
> $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
> $newcart = '';
> foreach ($items as $item) {
> if ($id != $item) {
> if ($newcart != '') {
> $newcart .= ','.$item;
> } else {
> $newcart = $item;
> }
> }
> }
> for ($i=1;$i<=$value;$i++) {
> if ($newcart != '') {
> $newcart .= ','.$id;
> } else {
> $newcart = $id;
> }
> }
> }
> }
> }
> $cart = $newcart;
> break;
> }
> $_SESSION['cart'] = $cart;
>
> ?>
>
>

>
>

Rum Basket


>
> > echo writeCart();
> ?>
>
>

>
>

>
>

Please Check Quantities...


>
> > echo showCart();
> ?>
>
>


>
>

>
>
>
>
> > include("footer.html");
>
> ?>
>
> functions.php
>
> > function writeCart() {
> $cart = $_SESSION['cart'];
> if (!$cart) {
> return '

There is no alcohol in your Rum Basket

';
> } else {
> // Parse the cart session variable
> $items = explode(',',$cart);
> $s = (count($items) > 1) ? 's':'';
> return '

There are

';
> }
> }
>
> function showCart() {
> $cart = $_SESSION['cart'];
> if ($cart) {
> $items = explode(',',$cart);
> $contents = array();
> foreach ($items as $item) {
> $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
> }
> $output[] = '
';
> $output[] = '';
> foreach ($contents as $id=>$qty) {
> $sql = 'SELECT * FROM spirits WHERE id = '.$id;
> $result = $con->query($sql);
> $row = $result->fetch();
> extract($row);
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $total += $price * $qty;
> $output[] = '';
> }
> $output[] = '
'.$name.'£'.$price.'£'.($price * $qty).'
';
> $output[] = '

Grand total: £'.$total.'

';
> $output[] = '
';
> $output[] = '
';
> } else {
> $output[] = '

You shopping cart is empty.

';
> }
> return join('',$output);
> }
>
> ?>
>
>
>
>
> Many Thanks
>
> Vee

Hi,

if i look at the code of basket.php its only 33 lines long, hence it
must be counting the includes as well.

Send me zip file of you project, i can help debug.

regards,

Max.
------------------------------------
Max E. Kimambo
Franz-Stenzer-Str, 51
12679 Berlin

Office: +493053080013
Mobile: +4917649520175

------------------------------------
Fortune message of the moment
You will triumph over your enemy.
------------------------------------



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: Shopping cart

am 16.05.2009 17:06:32 von danaketh

--------------050502080204040409090806
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi Vee,

You have it all in that error :) You're calling function on
non-object. Look into the basket.php. On the lines where you call the
MySQL query you have $con->query($sql) but there is no decalaration of
$con object anywhere before.

I think you have an MySQL layer included (mysql.class.php) but you
haven't it declared. You just made a new relation in $con. Instead of
calling $con->query() you have to call mysql_query(). You also have some
mistakes in query ;)

$sql = 'SELECT * FROM spirits ORDER BY id';
$query = mysql_query($sql);
....

Look at the PHP documentation how to work with mysql_ functions. Or
provide us with link to the MySQL layer you're using - that
mysql.class.php - so we can help you with using that. I think there will
be something like

$con = new MySQL('host', 'user', 'pass', 'db');

Then your syntax will work. However without knowing what layer it
is, I can't tell you the right syntax for it ;)


Vernon St Croix napsal(a):
> Hi,
>
> I am pretty new to PHP and I am trying to create a shopping cart.
>
> I keep on getting the below error when trying to show the shopping list.
>
> Any guidance that can be provided will be very much appreciated
>
> Fatal error: Call to a member function query() on a non-object in C:\wamp\www\draft\basket.php on line 36
>
> mysql_connect.php
> > $con = mysql_connect("localhost","root","");
> if (!$con)
> {
> die('Could not connect: ' . mysql_error());
> }
> mysql_select_db("rum", $con);
> ?>
>
> basket.php
> > include("mysql.class.php");
> include ("header.php");
> include ("mysql_connect.php");
> include ("functions.php");
> ?>
>


>

Rum Basket


> > echo writeCart();
> ?>
>

>

>

Rum on Offer


> >
> $sql= 'SELECT * FROM spirits BY id';
> $result = $con->query($sql);
> $output[]= '
    ';
    > while ($row = $result->fetch()) {
    > $output[] = '
  • '.$row['name'].': £'.$row['price'].'
    Add to Cart
  • ';
    > }
    > $output[] = '
';
> echo join ('', $output);
> ?>
>

>
>
> > include("footer.html");
>
> ?>
>
>
> cart.php
>
> >
> include ("header.php");
>
> include ("mysql_connect.php");
>
> include ("functions.php");
>
>
>
> $cart = $_SESSION['cart'];
>
>
> if(isset($_GET["action"]))
> { $action = $_GET["action"]; }
> else
> { $action = ""; }
>
>
> switch ($action) {
> case 'add':
> if ($cart) {
> $cart .= ','.$_GET['id'];
> } else {
> $cart = $_GET['id'];
> }
> break;
> case 'delete':
> if ($cart) {
> $items = explode(',',$cart);
> $newcart = '';
> foreach ($items as $item) {
> if ($_GET['id'] != $item) {
> if ($newcart != '') {
> $newcart .= ','.$item;
> } else {
> $newcart = $item;
> }
> }
> }
> $cart = $newcart;
> }
> break;
> case 'update':
> if ($cart) {
> $newcart = '';
> foreach ($_POST as $key=>$value) {
> if (stristr($key,'qty')) {
> $id = str_replace('qty','',$key);
> $items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
> $newcart = '';
> foreach ($items as $item) {
> if ($id != $item) {
> if ($newcart != '') {
> $newcart .= ','.$item;
> } else {
> $newcart = $item;
> }
> }
> }
> for ($i=1;$i<=$value;$i++) {
> if ($newcart != '') {
> $newcart .= ','.$id;
> } else {
> $newcart = $id;
> }
> }
> }
> }
> }
> $cart = $newcart;
> break;
> }
> $_SESSION['cart'] = $cart;
>
> ?>
>
>

>
>

Rum Basket


>
> > echo writeCart();
> ?>
>
>

>
>

>
>

Please Check Quantities...


>
> > echo showCart();
> ?>
>
>


>
>

>
>
>
>
> > include("footer.html");
>
> ?>
>
> functions.php
>
> > function writeCart() {
> $cart = $_SESSION['cart'];
> if (!$cart) {
> return '

There is no alcohol in your Rum Basket

';
> } else {
> // Parse the cart session variable
> $items = explode(',',$cart);
> $s = (count($items) > 1) ? 's':'';
> return '

There are

';
> }
> }
>
> function showCart() {
> $cart = $_SESSION['cart'];
> if ($cart) {
> $items = explode(',',$cart);
> $contents = array();
> foreach ($items as $item) {
> $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
> }
> $output[] = '
';
> $output[] = '';
> foreach ($contents as $id=>$qty) {
> $sql = 'SELECT * FROM spirits WHERE id = '.$id;
> $result = $con->query($sql);
> $row = $result->fetch();
> extract($row);
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $output[] = '';
> $total += $price * $qty;
> $output[] = '';
> }
> $output[] = '
'.$name.'£'.$price.'£'.($price * $qty).'
';
> $output[] = '

Grand total: £'.$total.'

';
> $output[] = '
';
> $output[] = '
';
> } else {
> $output[] = '

You shopping cart is empty.

';
> }
> return join('',$output);
> }
>
> ?>
>
>
>
>
> Many Thanks
>
> Vee
>

--

S pozdravem

Daniel Tlach
Freelance webdeveloper

Email: mail@danaketh.com
ICQ: 160914875
MSN: danaketh@hotmail.com
Jabber: danaketh@jabbim.cz


--------------050502080204040409090806--