Shopping cart

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("rum", $con);
?>

basket.php
<?php 
 include("mysql.class.php");
 include ("header.php");
 include ("mysql_connect.php");
  include ("functions.php");
 ?>
 <div id="shopping">
<h2>Rum Basket</h2>
<?php
    echo writeCart();
    ?>
 </div>
<div id="rumlist">
  <h2>Rum on Offer</h2>
  <?php
 
 $sql= 'SELECT * FROM spirits BY id';
  $result = $con->query($sql);
 $output[]= '<ul>';
 while ($row = $result->fetch()) {
 $output[] = '<li>'.$row['name'].': &pound;'.$row['price'].'<br/><a href="cart.php?action=add&id=
  '.$row['id'].'">Add to Cart</a></li>';
    }
    $output[] = '</ul>';
  echo join ('', $output);
   ?>
  </div>

</div>
<?php
 include("footer.html");

?>


cart.php

<?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;

?>

<div id="shopping">

<h2>Rum Basket</h2>

<?php
echo writeCart();
?>

</div>

<div id="contents">

<h2>Please Check Quantities...</h2>

<?php
echo showCart();
?>

<p><a href="basket.php">Back to Rum List</a></p>

</div>

</div>


<?php
 include("footer.html");

?>

functions.php

<?php
function writeCart() {
 $cart = $_SESSION['cart'];
 if (!$cart) {
  return '<p>There is no alcohol in your Rum Basket</p>';
 } else {
  // Parse the cart session variable
  $items = explode(',',$cart);
  $s = (count($items) > 1) ? 's':'';
  return '<p>There are<a href="cart.php">'.count($items).' item'.$s.' in your rum basket</a></p>';
 }
}
  
     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[] = '<form action="cart.php?action=update" method="post" id="cart">';
  $output[] = '<table>';
  foreach ($contents as $id=>$qty) {
   $sql = 'SELECT * FROM spirits WHERE id = '.$id;
   $result = $con->query($sql);
   $row = $result->fetch();
   extract($row);
   $output[] = '<tr>';
   $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
   $output[] = '<td>'.$name.'</td>';
   $output[] = '<td>&pound;'.$price.'</td>';
   $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" ></td>';
   $output[] = '<td>&pound;'.($price * $qty).'</td>';
   $total += $price * $qty;
   $output[] = '</tr>';
  }
  $output[] = '</table>';
  $output[] = '<p>Grand total: &pound;'.$total.'</p>';
  $output[] = '<div><button type="submit">Update cart</button></div>';
  $output[] = '</form>';
 } else {
  $output[] = '<p>You shopping cart is empty.</p>';
 }
 return join('',$output);
}

?>




Many Thanks

Vee

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux