Jump to content

Need Help on shopping cart ~

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
phyllo

phyllo

    Newbie

  • Members
  • Pip
  • 1 posts
Hi guys,

I'm having this project for my school on doing a website for ordering of food and i'm stuck on doing the shopping cart code.
Firstly, i have manage to store my ordering into the session and now i'm stuck on doing the checking out of shopping cart.

can some one teach me how to store the session into the database ? so i can do a confirmation of the order i make ?



<?php

$cart = $_SESSION['cart'];

$action = $_GET['action'];

switch ($action) {

	case 'add':

		if ($cart) {

			

			$cart .= ','.$_GET['bento_id'];

		} else {

			

			$cart = $_GET['bento_id'];

			$_SESSION['cart'] = $cart;

		}

		

		break;

	case 'delete':

		if ($cart) {

			$items = explode(',',$cart);

			$newcart = '';

			foreach ($items as $item) {

				if ($_GET['bento_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')) {

				$bento_id = str_replace('qty','',$key);

				$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);

				$newcart = '';

				foreach ($items as $item) {

					if ($bento_id != $item) {

						if ($newcart != '') {

							$newcart .= ','.$item;

						} else {

							$newcart = $item;

						}

					}

				}

				for ($i=1;$i<=$value;$i++) {

					if ($newcart != '') {

						$newcart .= ','.$bento_id;

					} else {

						$newcart = $bento_id;

					}

				}

			}

		}

	}

	$cart = $newcart;

	break;

}

$_SESSION['cart'] = $cart;




?>


<?php

echo writeShoppingCart();

?>




<div id="contents">


<h1>Please check quantities...</h1>



<?php

echo showCart();

?>


<p><a href="viewmenu.php">Back to bento menu...</a></p>

<?php

		}

		else

       {	

	      echo "<p> </p>";

	      echo "<h4>Error: The page you tried to view can only be used when you're logged in.</h4>";

	      echo "          ";

		  echo "<p> </p>";

          echo "<a href='index.php'>Back to home</a>";

		  echo "<p> </p>";

	   }

?>	   


i hope some one can guide me. thanks :crying:

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Why do you store your cart as a string into your session? You can use arrays in sessions as well, just store like
$_SESSION['cart'][artnumber] = quantity

and then it's very easy to recall items and change them etc

storage in the database is almost as simple, if you're familiar to databases.
you create one order table and one order item table, then store one order in the order table, retrieve the last insert id, and use that as a foreign key to store order items


does this give you any good hints, or any more questions?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall