Version: 0.1.2-real
Type: Full Script
Category: Shopping Carts
License: BSD License
Description: Entire shopping cart which does not rely on cookies. It uses a MySQL database for storing product data, as well as shopping cart data. Right now it’s a rough and dirty, but functional shopping cart.
<?php /******************************************************************************* lazy shopping cart ******************************************************************************** copyright (c) 2001 lazy <[email protected]>. all rights reserved. http://bsdbox.org/ redistribution and/or usage of this source with or without modifications is permitted given that the above copyright notice and/or a link to the website is given. THIS SOFTWARE/SOURCE CODE IS PROVIDED BY LAZY ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LAZY NOR ANY PERSONS/ENTITIES AFFILIATED WITH LAZY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR SORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ /******************************************************************************* THE PHP SCRIPT BELLOW IS TO INITALIZE THE DATABASE (added in version 0.1.2) ******************************************************************************** <?php $sql_box="localhost"; $sql_user="root"; $sql_pass="password";$sql_db="shop"; $link=mysql_connect($sql_box,$sql_user,$sql_pass); mysql_create_db($sql_db,$link); $ret=mysql_select_db($sql_db,$link) or die(mysql_error()); //mysql_query("drop table stuff"); mysql_query("create table stuff(item text,product text,price text,descr text, type text)"); mysql_query("insert into stuff(item,product,price,descr,type) values(1,'jessica','1.94','shes yummy','misc')"); //mysql_query("drop table carts"); mysql_query("create table carts(client text,gofigure text,items text)"); mysql_close($link); ?> ******************************************************************************** THE SCRIPT FOR DATABASE INITALIZATION IS ABOVE (added in version 0.1.2) *******************************************************************************/ ## open database connection $sql_box="localhost"; $sql_user="root"; $sql_pass=""; $sql_db="shop"; $link=mysql_connect($sql_box,$sql_user,$sql_pass); $ret=mysql_select_db($sql_db,$link) or die("db: getrows <hr>".mysql_error()); ## define [constant] global variables $n=" "; $version="0.1.2"; $updated=date("D j M Y H:i:s", filemtime("$PHP_SELF")); ## define client variables $client=$REMOTE_ADDR; ## voidify vodifiable variables $row=array();$cartitem=array(); $content="";$itemnum="";$itemold="";$itemnew="";$query="";$retval=""; $subtotal=0;$total=0;$cost=0;$stcost=0;$shipping=0; ## add item to shopping cart if($additem && $addqty) { $query="SELECT * FROM carts WHERE client='$client'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); $addqty=intval($addqty); if($row[0]) { $cartitem = $row[2]."&cartitem[]=".$additem."-".$addqty; $query="UPDATE carts SET items="$cartitem" WHERE client='$client'"; $query_result_handle = mysql_query ($query); } else { $itemstr="cartitem[]=".$additem."-".$addqty; $query="INSERT INTO carts (client,items) VALUES ("$client","$itemstr")"; $query_result_handle = mysql_query ($query); } } ## handle site earch if($searchquery) { $query="SELECT * FROM stuff WHERE (item='$searchquery') OR (product LIKE '%$searchquery%') OR (descr LIKE '%$searchquery%')"; $query_result_handle = mysql_query ($query); for ($count = 1; $row = mysql_fetch_row ($query_result_handle); ++$count) { $retval.= "<b>></b> <a href="./?item=$row[0]">$row[1]</a> ($$row[2])<br>"; } if($retval) { $content=$retval; } else { $content="Nothing was found."; } } ## handle type of stuff request if($type) { $query="SELECT * FROM stuff WHERE type='$type'"; $query_result_handle = mysql_query ($query); for ($count = 1; $row = mysql_fetch_row ($query_result_handle); ++$count) { $cost=sprintf ("%01.2f", $row[2]); $retval.= "<b>></b> <a href="./?item=$row[0]">$row[1]</a> ($$cost)<br>"; } if($retval) { $content=$retval; } else { $content="Nothing was found."; } } ## handle item info request if ($item > 0) { $query="SELECT * FROM stuff WHERE item='$item'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); if(!$row[1]) { $content="Invalid item number."; } else { $cost=sprintf ("%01.2f", $row[2]); $content=" <b>$row[1]</b><br> Price: $$cost<br> $n<br> $row[3]<br> <form action="$PHP_SELF" method="post"> Quantity: <input type="text" name="addqty" size="3" value="1"> <input type="hidden" name="additem" value="$item"> <input type="submit" value=" add to cart "> </form> "; } } ## handle cart jobs if($cartjob=="empty") { $query="DELETE FROM carts WHERE client='$client'"; $query_result_handle = mysql_query($query); $jobreturn="Your shopping cart is empty.<br>$n<br>"; $view="cart"; } elseif($cartjob=="update") { $query="SELECT * FROM carts WHERE client='$client'"; $query_result_handle = mysql_query($query); $row = mysql_fetch_row($query_result_handle); $cartdata=$row[2]; $cartdata=str_replace("&&","&",$cartdata); $count=0; while ($remove[$count]): if($remove[$count]) { $cartdata=str_replace("cartitem[]=$remove[$count]","",$cartdata); } $count++; endwhile; $query="UPDATE carts SET items="$cartdata" WHERE client='$client'"; $query_result_handle = mysql_query($query); if($count==0) { ## get total number of items $query="SELECT MAX(item) FROM stuff"; $query_result_handle = mysql_query($query); $row=mysql_fetch_row($query_result_handle); $totalitems=$row[0]; ## get cart data $query="SELECT * FROM carts WHERE client='$client'"; $query_result_handle = mysql_query($query); $row = mysql_fetch_row($query_result_handle); $cartdata=$row[2]; ## so will she go out with me i wonder once again while (list ($key, $val) = @each ($qty)) { $itemnum=$key; while (list ($key, $val) = @each ($val)) { $itemold=$key; $itemnew=intval($val); $exp1="cartitem[]=$itemnum-$itemold"; $exp2="cartitem[]=$itemnum-$itemnew"; $cartdata=str_replace($exp1,$exp2,$cartdata); } } $exp1="cartitem[]=$itemnum-$itemold"; $exp2="cartitem[]=$itemnum-$itemnew"; $cartdata=str_replace($exp1,$exp2,$cartdata); ## update table $query="UPDATE carts SET items="$cartdata" WHERE client='$client'"; $query_result_handle = mysql_query($query); } $view="cart"; } elseif($cartjob=="checkout") { $content=" <form action="" method="post"> <table border=0 cellpadding=2 cellspacing=2> <tr valign="top"> <td><b>Contact Information</b><br></td> <td>$n<br></td> </tr> <tr valign="top"> <td> E-mail Address: </td> <td> <input type="text" name="contact[]"> </td> </tr> <tr valign="top"> <td> Telephone #:<br> <small>with area code</small> </td> <td> <input type="text" name="contact[]"> </td> </tr> <tr valign="top"> <td>$n<br></td> <td>$n<br></td> </tr> <tr valign="top"> <td><b>Shipping Address</b><br></td> <td><small>We cannot ship to PO Boxes</small></td> </tr> <tr valign="top"> <td> Name: </td> <td> <input type="text" name="shipto[]"> </td> </tr> <tr valign="top"> <td> Address: </td> <td> <input type="text" name="shipto[]"><br> <input type="text" name="shipto[]"> </td> </tr> <tr valign="top"> <td> City: </td> <td> <input type="text" name="shipto[]"> </td> </tr> <tr valign="top"> <td> State/Providence: </td> <td> <select name="shipto[]"> <option value="al">alabama <option value="ak">alaska <option value="az">arizona <option value="ar">arkansas <option value="ca">california <option value="co">colorado <option value="ct">connecticut <option value="dc">d.c. <option value="de">delaware <option value="fl">florida <option value="ga">georgia <option value="gu">guam <option value="hi">hawaii <option value="id">idaho <option value="il">illinois <option value="in">indiana <option value="ia">iowa <option value="ks">kansas <option value="ky">kentucky <option value="la">louisiana <option value="me">maine <option value="md">maryland <option value="ma">massachusetts <option value="mi">michigan <option value="mn">minnesota <option value="ms">mississippi <option value="mo">missouri <option value="mt">montana <option value="ne">nebraska <option value="nv">nevada <option value="nh">new hampshire <option value="nj">new jersey <option value="nm">new mexico <option value="ny">new york <option value="nc">north carolina <option value="nd">north dakota <option value="oh">ohio <option value="ok">oklahoma <option value="or">oregon <option value="pa">pennsylvania <option value="pr">puerto rico <option value="ri">rhode island <option value="sc">south carolina <option value="sd">south dakota <option value="tn">tennessee <option value="tx">texas <option value="ut">utah <option value="vt">vermont <option value="va">virginia <option value="wa">washington <option value="wv">west virginia <option value="wi">wisconsin <option value="wy">wyoming </select> </td> </tr> <tr valign="top"> <td> Zip/Postal Code: </td> <td> <input type="text" name="shipto[]"> </td> </tr> <tr valign="top"> <td>$n<br></td> <td>$n<br></td> </tr> <tr valign="top"> <td>$n<br></td> <td> <input type="checkbox" name="billisship"> Billing address is same as shipping address.<br></td> </tr> <tr valign="top"> <td>$n<br></td> <td>$n<br></td> </tr> <tr valign="top"> <td><b>Billing Address</b><br></td> <td><small>This is the address your credit card bill is sent to</small></td> </tr> <tr valign="top"> <td> Name: </td> <td> <input type="text" name="billto[]"> </td> </tr> <tr valign="top"> <td> Address: </td> <td> <input type="text" name="billto[]"><br> <input type="text" name="billto[]"> </td> </tr> <tr valign="top"> <td> City: </td> <td> <input type="text" name="billto[]"> </td> </tr> <tr valign="top"> <td> State/Providence: </td> <td> <select name="billto[]"> <option value="al">alabama <option value="ak">alaska <option value="az">arizona <option value="ar">arkansas <option value="ca">california <option value="co">colorado <option value="ct">connecticut <option value="dc">d.c. <option value="de">delaware <option value="fl">florida <option value="ga">georgia <option value="gu">guam <option value="hi">hawaii <option value="id">idaho <option value="il">illinois <option value="in">indiana <option value="ia">iowa <option value="ks">kansas <option value="ky">kentucky <option value="la">louisiana <option value="me">maine <option value="md">maryland <option value="ma">massachusetts <option value="mi">michigan <option value="mn">minnesota <option value="ms">mississippi <option value="mo">missouri <option value="mt">montana <option value="ne">nebraska <option value="nv">nevada <option value="nh">new hampshire <option value="nj">new jersey <option value="nm">new mexico <option value="ny">new york <option value="nc">north carolina <option value="nd">north dakota <option value="oh">ohio <option value="ok">oklahoma <option value="or">oregon <option value="pa">pennsylvania <option value="pr">puerto rico <option value="ri">rhode island <option value="sc">south carolina <option value="sd">south dakota <option value="tn">tennessee <option value="tx">texas <option value="ut">utah <option value="vt">vermont <option value="va">virginia <option value="wa">washington <option value="wv">west virginia <option value="wi">wisconsin <option value="wy">wyoming </select> </td> </tr> <tr valign="top"> <td> Zip/Postal Code: </td> <td> <input type="text" name="billto[]"> </td> </tr> <tr valign="top"> <td>$n<br></td> <td>$n<br></td> </tr> <tr valign="top"> <td>$n<br></td> <td><input type="submit" name="cartjob" value="continue"> <br><small>(you can still cancel the order)</small></td> </tr> </table> </form> "; } elseif ($cartjob=="continue") { if($billisship) { $billto=$shipto; } if($shipto[2]) { $shiptoaddy=" $shipto[0]<br> $shipto[1]<br> $shipto[2]<br> $shipto[3], $shipto[4] $shipto[5] "; } else { $shiptoaddy=" $shipto[0]<br> $shipto[1]<br> $shipto[3], $shipto[4] $shipto[5] "; } if($billto[2]) { $billtoaddy=" $billto[0]<br> $billto[1]<br> $billto[2]<br> $billto[3], $billto[4] $billto[5] "; } else { $billtoaddy=" $billto[0]<br> $billto[1]<br> $billto[3], $billto[4] $billto[5] "; } $query="SELECT * FROM carts WHERE client='$client'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); parse_str($row[2]); $count=0; while ($cartitem[$count]): $pos=strrpos($cartitem[$count],"-"); $itemnum=substr($cartitem[$count],0,$pos); $itemqty=substr($cartitem[$count],$pos+1); $query="SELECT * FROM stuff WHERE item='$itemnum'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); $prod = $row[1]; $cost = sprintf ("%01.2f", $row[2]); $stcost=$cost * $itemqty; $stcost=sprintf ("%01.2f", $stcost); $subtotal=$stcost+$subtotal; $subtotal=sprintf ("%01.2f", $subtotal); if($billto[4]=="ny") { $salestax=$subtotal * .0825; } else { $salestax=0; } $salestax=sprintf ("%01.2f", $salestax); $shippingG=5+($subtotal * .0468); $shippingG=sprintf ("%01.2f", $shippingG); $shipping=$shippingG; $total= $subtotal + $salestax + $shipping; $total=sprintf ("%01.2f", $total); $remval=$itemnum."-".$itemqty; $cartstuff.=" <i>$prod</i><br> unit price: $$cost - subtotal: $$stcost<br> quantity: $itemqty<br>$n<br>"; $count++; endwhile; $cartstuff.=" <b>Costs:</b><br> Subtotal: $$subtotal<br> Sales tax: $$salestax<br> Shipping: $$shipping<br> Total: $$total<br> $n<br>"; $contactinfo=" E-mail: $contact[0]<br> Phone: $contact[1]<br> "; $content=" <table border=0 cellpadding=2 cellpadding=2> <tr valign="top"> <td> <b>Contact:</b><br> $contactinfo </td> <td width="150"> $n<br> </td> <td> $n<br> </td> </tr> <tr><td>$n<br></td><td>$n<br></td><td>$n<br></td></tr> <tr valign="top"> <td> <b>Ship To:</b><br> $shiptoaddy </td> <td width="100"> $n<br> </td> <td> <b>Bill To:</b><br> $billtoaddy </td> </tr> <tr><td>$n<br></td><td>$n<br></td><td>$n<br></td></tr> <tr valign="top"> <td> <form action="$PHP_SELF" method="post"> <b>Shipping Method:</b><br> <select name="shipping"> <option value="$shippingG">UPS Ground ($shippingG) </select><br> $n<br> <b>Payment Information:</b><br> Name as it appears on card:<br> <input type="text" size="21" name="payment[]"><br> Credit card number:<br> <input type="text" size="21" name="payment[]">$n <select name="payment[]"> <option value="AmEx">AmEx <option value="Discover">Discover <option value="MasterCard">MasterCard <option value="Visa">Visa </select><br> Exp. date:<br> <select name="payment[]"> <option value="01">01 <option value="02">02 <option value="03" selected>03 <option value="04">04 <option value="05">05 <option value="06">06 <option value="07">07 <option value="08">08 <option value="09">09 <option value="10">10 <option value="11">11 <option value="12">12 </select>$n <select name="payment[]"> <option value="2001">2001 <option value="2002">2002 <option value="2003">2003 <option value="2004">2004 <option value="2005">2005 <option value="2006">2006 <option value="2007">2007 <option value="2008">2008 </select><br> $n<br> <input type="hidden" name="billaddy" value="$shiptoaddy"> <input type="hidden" name="shipaddy" value="$billtoaddy"> <input type="hidden" name="cartstuff" value="$cartstuff"> <input type="hidden" name="contact" value="$contactinfo"> <input type="hidden" name="myemail" value="$contact[0]"> <input type="submit" name="cartjob" value="confirm"><br> <small>(you can still cancel the order)</small> </form> </td> <td width="100"> $n<br> </td> <td> <b>Order:</b><br> $cartstuff </td> </tr> </table> "; } elseif($cartjob=="confirm") { $cardnum=str_replace(" ","",$payment[2]); $cardnum=str_replace("-","",$cardnum); $showcard=substr($payment[1],-4); $cardinfo=" Card name: $payment[0]n Card type: $payment[2]n Card exp.: $payment[3]/$payment[4]n Card num.: $cardnumn"; $customercard=" <b>Payment:</b><br> Name on credit card:<br> <i>$payment[0]</i><br> Card type/exp. date:<br> <i>$payment[2]</i> / <i>$payment[3]/$payment[4]</i><br> Last 4 digits of card:<br> $showcard<br> "; $content=" Please make sure all these changes are correct.<br> If they are not, use your browser's "back" button and change them. <br> $n<br> <form action="$PHP_SELF" method="post"> <table border=0 cellpadding=2 cellspacing=2> <tr valign="top"> <td> <b>Shipping Address:</b><br> $shipaddy </td> <td> <b>Billing Address:</b><br> $billaddy </td> </tr> <tr><td>$n<br></td><td>$n<br></td></tr> <tr valign="top"> <td> <b>Orders:</b><br> $cartstuff </td> <td> $customercard </td> <tr valign="top"> <td> <input type="hidden" name="cartstuff" value="$cartstuff"> <input type="hidden" name="cardinfo" value="$cardinfo"> <input type="hidden" name="customercard" value="$customercard"> <input type="hidden" name="contact" value="$contact"> <input type="hidden" name="billaddy" value="$billaddy"> <input type="hidden" name="shipaddy" value="$shipaddy"> <input type="hidden" name="myemail" value="$myemail"> <input type="submit" name="cartjob" value="finish"><br> (click only once!) </td> <td> <input type="submit" name="cartjob" value="cancel!"><br> (in case you just remembered you're broke!) </td> </tr> </table> </form> "; } elseif($cartjob=="finish") { $mailbody=" ORDER DETAILSn --------------n $cartstuffnn PAYMENT INFOn -------------n $cardinfonn BILLING ADDRESSn ----------------n $billaddynn SHIPPING ADDRESSn -----------------n $shipaddynn CUSTOMER CONTACTn -----------------n $contactn "; $mailbody=strip_tags($mailbody); mail("[email protected]","ORDER",$mailbody,"From: [email protected]"); $mailbody=" ORDER DETAILSn --------------n $cartstuffnn PAYMENT INFOn -------------n $customercardnn BILLING ADDRESSn ----------------n $billaddynn SHIPPING ADDRESSn -----------------n $shipaddynn CUSTOMER CONTACTn -----------------n $contactn "; $mailbody=strip_tags($mailbody); mail($myemail,"Order Recieved",$mailbody,"From: [email protected]"); $query="DELETE FROM cart WHERE client='$client'"; mysql_query($query); $content=" Your order has been sent to be processed, and you should recieve a copy of the order shortly in your email. "; } ## display cart contents if ($view=="cart") { $query="SELECT * FROM carts WHERE client='$client'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); parse_str($row[2]); $cartstuff="<form action="$PHP_SELF" method="post">"; $count=0; while ($cartitem[$count]): /*$query="SELECT * FROM stuff WHERE item='$cartitem[$count]'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); $prod = $row[1];*/ $pos=strrpos($cartitem[$count],"-"); $itemnum=substr($cartitem[$count],0,$pos); $itemqty=substr($cartitem[$count],$pos+1); $query="SELECT * FROM stuff WHERE item='$itemnum'"; $query_result_handle = mysql_query ($query); $row = mysql_fetch_row ($query_result_handle); $prod = $row[1]; $cost = sprintf ("%01.2f", $row[2]); $stcost=$cost * $itemqty; $stcost=sprintf ("%01.2f", $stcost); $subtotal=$stcost+$subtotal; $subtotal=sprintf ("%01.2f", $subtotal); $remval=$itemnum."-".$itemqty; $cartstuff.=" <i>$prod</i><br> unit price: $$cost - subtotal: $$stcost<br> quantity: <input name="qty[$itemnum][$itemqty]" type="text" size="3" value="$itemqty"> <input type="checkbox" name="remove[]" value="$remval">remove<br>$n<br>"; $count++; endwhile; $cartstuff.=" Subtotal: $subtotal<br> $n<br> <input type="submit" name="cartjob" value="update"> <input type="submit" name="cartjob" value="empty"> <input type="submit" name="cartjob" value="checkout"></form>"; if(!$itemnum){$jobreturn="Your shopping cart is empty.<br>$n<br>";} $content="$jobreturn $cartstuff"; } ## if no content use default if (!$content) { $content=" <h3>about</h3> this is just a little project i've taken up. writing my own shopping cart type stuff.<br> version 0.1.2 source code is now available. <a href="/src/cart.phps">more...</a>]<br> $n<br> after so long since releasing 0.1.1 on phpbuilder, i decided to resume the project.<br> questions/comments/suggestions: [email protected]<br> <h3>update log</h3> version 0.1.2 - fixed a couple bugs, including one where it didn't calculate costs at checkout.<br> version 0.1.1 - deletes shopping cart stuff once checkout is completed. seemed logical.<br> version 0.1.0 - resolved 2 minor bugs w/ checkout regarding order confirmation emails.<br> version 0.0.9 - checkout process completed (long and annoying, but not at all dificult)<br> version 0.0.8 - sales tax for NY residents (very simple, wasnt even worth the version change)<br> version 0.0.7 - UPS ground shipping (quite inaccurate, due to how it's done)<br> version 0.0.6 - update quantity of product in cart [w/o cookies] (that was pretty tough)<br> version 0.0.5 - remove specific/all cart contents<br> version 0.0.4 - change inital quantity to add to cart<br> version 0.0.3 - shopping cart set up; view cart contents<br> version 0.0.2 - search for products<br> version 0.0.1 - browse products<br> <h3>devel plans</h3> the following features are planned to be added:<br> <b>></b> better structured code <br> <b>></b> backend <br> <b>></b> inventory checking <br> <b>></b> improved search<br> <b>></b> better display of products<br> "; } ## display webpage echo(" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>-== lazy shopping cart ==-</title> </head> <body bgcolor="#ffffff" text="#000000"> <table border="0" cellpadding="2" cellspacing="2"> <tr valign="top"> <td width="100"> <b>project</b><br> <a href="./">home</a><br> <a href="/">bsdbox</a><br> $n<br> <b>products</b><br> <a href="./$PHP_SELF?type=tshirts">t-shirts</a><br> <a href="./$PHP_SELF?type=cds">cds</a><br> <a href="./$PHP_SELF?type=misc">misc</a><br> $n<br> <b>your cart</b><br> <a href="./$PHP_SELF?view=cart">view cart</a><br> $n<br> <b>search</b><br> <form action="$PHP_SELF" method="post"> <input type="text" name="searchquery" size="8"> </form> </td> <td> $content </td> </tr> </table> $n<br> <small> current version: $version<br> file last modified: $updated<br> </small> </body> </html> "); ## close the database connection mysql_close($link); ?>