Tải bản đầy đủ - 0 (trang)
Chapter 21. Creating a Shopping Cart Mechanism

Chapter 21. Creating a Shopping Cart Mechanism

Tải bản đầy đủ - 0trang

PlanningandCreatingtheDatabaseTables

Becausethegoalofthischapteristoprovidetheuserwitha

waytoselectandorderitems,youcanimaginewhatthetables

willbefirstandforemost,youneedacarttable!Inadditionto

thecarttable,you'llneedatabletostoreorders,alongwith

onetostoretheitemspurchasedaspartofeachorder.

ThefollowingSQLstatementswereusedtocreatethethree

newtables,startingwiththestore_shoppertracktable.Thisis

thetableusedtoholditemsasusersaddthemtotheir

shoppingcart.

Thefieldlengthsusedtodefinethesetableswerechosen

arbitrarilytoaccommodateseveralpossibleinputs.Pleasefeel

freetomodifythelengthstomeetyourspecificneeds.



mysql>createtablestore_shoppertrack(

->idintnotnullprimarykeyauto_increment,

->session_idvarchar(32),

->sel_item_idint,

->sel_item_qtysmallint,



->sel_item_sizevarchar(25),

->sel_item_colorvarchar(25),

->date_addeddatetime

->);

QueryOK,0rowsaffected(0.01sec)

Inthistable,theonlykeyistheidfieldfortherecord.The

session_idcannotbeunique;otherwise,userscouldorder

onlyoneitemfromyourstore,whichisnotagoodbusiness

practice.Thesession_ididentifiestheuser.Thesel_*fields

aretheselectionsbytheuser:theselecteditem,theselected

quantityoftheitem,andtheselectedcolorandsizeofthe

item.Finally,there'sadate_addedfield.Manytimes,users

placeitemsintheircartandnevergothroughthecheckout

process.Thispracticeleavesstragglingitemsinyourtracking

table,whichyoumaywanttoclearoutperiodically.For

example,youmightwanttodeleteallcartitemsmorethana

weekoldthisiswherethedate_addedfieldishelpful.

Thenexttableholdstheorderinformation:



mysql>createtablestore_orders(

->idintnotnullprimarykeyauto_increment,

->order_datedatetime,



->order_namevarchar(100),

->order_addressvarchar(255),

->order_cityvarchar(50),

->order_statechar(2),

->order_zipvarchar(10),

->order_telvarchar(25),

->order_emailvarchar(100),

->item_totalfloat(6,2),

->shipping_totalfloat(6,2),

->authorizationvarchar(50),

->statusenum('processed','pending')

->);

QueryOK,0rowsaffected(0.00sec)

Theonlykeyfieldinthestore_orderstableistheid.Forthe

sakeofbrevityinthislesson,anassumptionismadethatthe

billingandshippingaddressesoftheuserarethesame,and

thatthisstoresellsonlytoUnitedStatesaddresses.It'ssimple

enoughtoaddanotherblockoffieldsforshippingaddress

information,ifyouwanttodoso.Also,thistableassumesthat

youarenotstoringcredit-cardinformation,whichyoushouldn't

dounlessyouhavesuper-encryptedtheinformationandare

positiveyourfirewallissecure.Thistableisbasedontheidea



ofreal-time,credit-cardprocessing.You'lllearnafew

transactionoptionsattheendofthislesson.

Thefinaltableisthetabletoholdthelineitemsineachorder,

store_orders_items:



mysql>createtablestore_orders_items(

->idintnotnullprimarykeyauto_increment,

->order_idint,

->sel_item_idint,

->sel_item_qtysmallint,

->sel_item_sizevarchar(25),

->sel_item_colorvarchar(25),

->sel_item_pricefloat(6,2)

->);

QueryOK,0rowsaffected(0.00sec)

Thesel_*fieldsshouldlookfamiliarwiththeexceptionof

sel_item_price,theyarethesamefieldsthatappearinthe

store_shoppertracktable!Theprimarykeyistheidfield,and

theorder_idfieldisusedtotieeachlineitemtothe

appropriaterecordinstore_orders.Thesel_item_pricefield



isincludedhere,asopposedtosimplyrelatingtotheitem

recordbecauseyoumighthaveoccasiontochangethepricing

inyouritemrecord.Ifyouchangethepriceintheitemrecord,

andyourelatethesoldlineitemstothecurrentcatalogprice,

yourlineitempriceswon'treflectwhattheuseractuallypaid.

Withyourtablesallsquaredaway,wecanmoveontoadding

anitemtotheuser'sshoppingcart.



IntegratingtheCartwithYourStorefront

Inthissection,you'llmakemodificationstotheshowitem.php

scriptfromChapter20,"CreatinganOnlineStorefront."The

goalistotransformtheiteminformationpageintoanitem

informationpagewithaformforselectingcolors,sizes,and

quantities.

Intheoriginalscript,insertthefollowingbeforeline2:



session_start();

Becausetheshoppingcartelementsareattachedtotheuser

throughasessionID,thesessionmustbestarted.Thenext

changesdon'toccuruntilwhatwasline37oftheoriginalscript,

sothat'swherewestartListing21.1.



Listing21.1NewLinesinshowitem.php



37:

Price:\$$item_price



38:";

39://getcolors



40:$get_colors="selectitem_colorfromstore_item_color

41:item_id=$item_idorderbyitem_color";



42:$get_colors_res=mysql_query($get_colors)ordie(mysql

43:

44:if(mysql_num_rows($get_colors_res)>0){

45:



46:$display_block.="

AvailableColors:
47:";

48:



49:while($colors=mysql_fetch_array($get_colors_res)

50:$item_color=$colors['item_color'];

51:$display_block.=



52:"$item_color
53:}

54:

55:$display_block.="";

56:}

57:

58://getsizes



59:$get_sizes="selectitem_sizefromstore_item_sizewhe



60:item_id=$item_idorderbyitem_size";



61:$get_sizes_res=mysql_query($get_sizes)ordie(mysql_e

62:

63:if(mysql_num_rows($get_sizes_res)>0){

64:



65:$display_block.="

AvailableSizes:
66:";

67:



68:while($sizes=mysql_fetch_array($get_sizes_res))

69:$item_size=$sizes['item_size'];

70:$display_block.="



71:$item_size
72:}

73:

74:$display_block.="";

75:}

76:

77:$display_block.="

78:

SelectQuantity:



79:";

80:

81:for($i=1;$i<11;$i++){



82:$display_block.="$i

83:}

84:

85:$display_block.="

86:



87:


88:


89:

90:

91:

92:";

93:}

94:?>

95:

96:



97:MyStore

98:

99:

100:

101:

102:

Thefirstchangeisatwhatwasline37,wherethe

$display_blockstringiscontinuedtoincludethebeginning

element.Theactionoftheformisascriptcalled

addtocart.php,whichyouwillcreateinthenextsection.

Thenextchangeoccursatline47,wherethe$display_block

stringiscontinuedtoincludetheopeningtagofa

element.

Thesametypesofchangesaremadeforitemsizes.Lines6667

reflectthecontinuationofthe$display_blockstringtoinclude

theelement.

Lines7783areadditionstothescript.Theselinescreatea



element,andline87addsahiddenfieldfortheitem_id.Line

88addsthesubmitbuttonandline89closestheform.The

remaininglinesareunchangedfromtheoriginalscript.



Whenviewingthecowboyhatitemusingthenewversionof

showitem.php,youwouldseeFigure21.1,reflectingthe

additionoftheformelements.



Figure21.1.Thenewcowboyhatitempage.



Thenextstepistocreatetheaddtocart.phpscript.



AddingItemstoYourCart

Theaddtocart.phpscriptwillsimplywriteinformationtothe

store_shoppertracktable,andredirecttheusertotheviewof

theshoppingcart.We'llcreatetheaddtocart.phpscriptin

Listing21.2,andthentackletheshowcart.phpscriptnext.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 21. Creating a Shopping Cart Mechanism

Tải bản đầy đủ ngay(0 tr)

×