Click here to Skip to main content
15,923,789 members
Home / Discussions / Web Development
   

Web Development

 
GeneralRe: ASPX question Pin
alex.barylski31-Oct-04 13:11
alex.barylski31-Oct-04 13:11 
GeneralUK Postcode Checker Code Pin
Anonymous31-Oct-04 10:58
Anonymous31-Oct-04 10:58 
Generalfrom ASP to PHP Pin
aguest30-Oct-04 13:29
aguest30-Oct-04 13:29 
GeneralRe: from ASP to PHP Pin
Jack Puppy30-Oct-04 17:22
Jack Puppy30-Oct-04 17:22 
GeneralRe: from ASP to PHP Pin
aguest31-Oct-04 12:15
aguest31-Oct-04 12:15 
GeneralRe: from ASP to PHP Pin
Jack Puppy31-Oct-04 23:14
Jack Puppy31-Oct-04 23:14 
GeneralRe: from ASP to PHP Pin
aguest2-Nov-04 8:13
aguest2-Nov-04 8:13 
GeneralRe: from ASP to PHP Pin
Jack Puppy2-Nov-04 13:52
Jack Puppy2-Nov-04 13:52 
You can add/update/delete any type of variable at anytime when you use php sessions.

Here's a general take at what I'd do for an e-cart app. Firstly, I'd place all e-cart code
in a separate file, that way you can include it in any file you want. (even better, you can make
a class for it)

include_ecart.php:
(not checked for errors)

<br />
<?php<br />
  /* e-cart related functions */<br />
	  function does_ecart_exist()<br />
	  {<br />
		    // if 'ecart' session variable is set, it exists<br />
		    return (isset($_SESSION['ecart']));<br />
	  }<br />
<br />
  function is_item_in_ecart($product_id)<br />
  {<br />
		    // make sure the e-cart exists!<br />
		    if (!does_ecart_exist())<br />
			      return (false);<br />
<br />
		    return (isset($_SESSION['ecart'][$product_id]));<br />
  }<br />
<br />
  function add_item_to_ecart($product_id, $product_name, $unit_price, $quantity)<br />
  {<br />
    // make sure the e-cart exists!<br />
    if (!does_ecart_exist())<br />
      return (false);<br />
<br />
    // todo: determine what to do if item already in cart...<br />
    // if (is_item_in_ecart($product_id))<br />
    // update or return error?<br />
<br />
    // create an array to hold the product data<br />
    $product_data = array('name' => $product_name, 'price' => $unit_price, 'quantity' => $quantity);<br />
<br />
    // add the product to the e-cart array, using it's id as the key<br />
    $_SESSION['ecart'][$product_id] = $product_data;<br />
<br />
    return (true);<br />
  }<br />
<br />
  function view_ecart_contents()<br />
  {<br />
    // make sure the e-cart exists!<br />
    if (!does_ecart_exist())<br />
      return (false);<br />
<br />
    // grab each item in the cart, and print out data<br />
    $product_id = 0;<br />
    $product_data = array();<br />
		<br />
    while (list($product_id, $product_data) = each($_SESSION['ecart']))<br />
    {<br />
      printf('Product Id: %1$s, Name: %2$s, Price: %3$s, Quantity: %4$s', <br />
        $product_id,<br />
        $product_data['name'],<br />
        $product_data['price'],<br />
        $product_data['quantity']);<br />
    }<br />
<br />
    return (true);<br />
  }<br />
<br />
  function remove_item_from_ecart($product_id)<br />
  {<br />
    // make sure the e-cart exists!<br />
    if (!does_ecart_exist())<br />
      return (false);<br />
<br />
    // item must be in ecart<br />
    if (!is_item_in_ecart($product_id))<br />
      return (false);<br />
<br />
    // remove the product from the ecart<br />
    // this code may need fixing, but I think it should work<br />
    // all you're doing is removing an item from the ecart array<br />
    $_SESSION['ecart'][$product_id] = array();<br />
		    unset($_SESSION['ecart'][$product_id]);<br />
	<br />
    return (true);<br />
  }<br />
<br />
  function check_out_ecart()<br />
  {<br />
    // here take the e-cart session data, perform any validation, then add <br />
    // a record to the order table<br />
  }<br />
<br />
  /* The following code will be executed by each .php file that includes this file */<br />
  // must call session_start before you can access any session data<br />
  session_start();<br />
<br />
  // if cart hasn't been created for user yet, create one now (an empty array)<br />
  // this code will trigger when the user 1st visits the site<br />
  if (!is_ecart_exists())<br />
    $_SESSION['ecart'] = array();<br />
?><br />
<br />


Now, all you have to do is add:

include_once("include_ecart.php");

to the top of each file that will use the e-cart, and you've got access to all the e-cart functions and session data. For adding an item, all you'd have to do is call add_item_to_cart after the user post/button click occurs:

add_product_to_ecart(1, "Product No1", "5.00", "1");

Removing an item, just call:

remove_product_from_ecart(1);

You should check and see if there's an existing e-cart php class somewhere on the net. I know PEAR has lots of good classes in it, but I can't say for sure if it has any e-cart functionality.


Pssst. You see that little light on your monitor? That's actually a spy camera, and I'm tracking your every move...
Generalstrange problem Pin
sianatia30-Oct-04 7:03
sianatia30-Oct-04 7:03 
GeneralRe: strange problem Pin
lanying_wzw1-Nov-04 0:52
lanying_wzw1-Nov-04 0:52 
GeneralRe: strange problem Pin
sianatia1-Nov-04 1:22
sianatia1-Nov-04 1:22 
GeneralDyanamic menus Java Applet Pin
rgoyal30-Oct-04 2:38
rgoyal30-Oct-04 2:38 
GeneralPaging the data Pin
hakanaktan29-Oct-04 21:27
hakanaktan29-Oct-04 21:27 
GeneralRe: Paging the data Pin
Paul Watson29-Oct-04 23:48
sitebuilderPaul Watson29-Oct-04 23:48 
GeneralRe: thanx Pin
hakanaktan30-Oct-04 0:17
hakanaktan30-Oct-04 0:17 
Generallink Pin
cmarmr29-Oct-04 5:20
cmarmr29-Oct-04 5:20 
GeneralRe: link Pin
R. Thomas29-Oct-04 17:42
R. Thomas29-Oct-04 17:42 
GeneralRe: link Pin
lanying_wzw1-Nov-04 0:49
lanying_wzw1-Nov-04 0:49 
GeneralNeed Guidance Pin
rgoyal29-Oct-04 0:28
rgoyal29-Oct-04 0:28 
GeneralRe: Need Guidance Pin
R. Thomas29-Oct-04 17:31
R. Thomas29-Oct-04 17:31 
GeneralRe: Need Guidance Pin
rgoyal29-Oct-04 23:51
rgoyal29-Oct-04 23:51 
GeneralRe: Need Guidance Pin
R. Thomas30-Oct-04 13:11
R. Thomas30-Oct-04 13:11 
GeneralPage re-submision Pin
TPN29-Oct-04 0:03
TPN29-Oct-04 0:03 
GeneralRe: Page re-submision Pin
Paul Watson29-Oct-04 23:52
sitebuilderPaul Watson29-Oct-04 23:52 
Generalmessagebox Pin
cmarmr28-Oct-04 4:56
cmarmr28-Oct-04 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.