PHP Case Studies - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

PHP Case Studies

Description:

Shopping Cart. Product Selection Form form method='post' action='Shopping.php' ... Shopping Cart 2. Product Displayed. Product:Radio ... – PowerPoint PPT presentation

Number of Views:144
Avg rating:3.0/5.0
Slides: 20
Provided by: chme3
Category:
Tags: php | cart | case | studies

less

Transcript and Presenter's Notes

Title: PHP Case Studies


1
PHP Case Studies
2
Sending Mail
  • mail (to, subject, message, headers )
  • Can be used to send mail from a correctly
    configured php system
  • To confirm registration and email addresses
  • Send users information

3
Auto Increment Fields
  • Specify a field that is automatically incremented
  • orderNum int autoincrement primary key
  • Creates a unique key
  • Ensures simultaneous updates do not create
    duplicate values

4
Identifying Autoincrement Updated Value
  • After inserting values into table that has an
    auto-increment field
  • mysql_query("INSERT INTO trades
    (sellid,isbn,price,buysell) VALUES ('01001000',
    '1234567', '99.99', 'sell') ")
  • Use mysql_insert_id() to find what was the value
    automatically inserted
  • printf("Last inserted record has id d\n",
    mysql_insert_id())

5
Trading Example
  • Illustrating use of Php and Databases
  • Database definition
  • create table trades (
  • tradeid int auto_increment primary key,
  • buyid char(8),
  • sellid char(8),
  • isbn varchar(40),
  • price float,
  • buysell varchar(4)
  • )

6
Trading Example 2
  • Trading Submission Form
  • ltform method'post' action'index.php'gt
  • Student IDltinput type'text' name'studid'/gtltbrgt
  • Book ISBNltinput type'text' name'isbn'/gtltbrgt
  • Priceltinput type'text' name'price'/gtltbrgt
  • Buyltinput type'radio' name'buysell'
    value'buy' checked/gt
  • Sellltinput type'radio' name'buysell'
    value'sell'/gtltbrgt
  • ltinput type'submit' value'submit'/gt
  • lt/formgt

7
Trading Example 3
  • Connect to Database
  • db mysql_connect("localhost", "root")
  • mysql_select_db("ass1",db)
  • Create query and fetch first row
  • result mysql_query("SELECT tradeid,buyid,sellid
    ,isbn,price,buysell FROM trades",db)
  • myrow mysql_fetch_row(result)

8
Trading Example 4
  • Display Current Trades
  • if(myrowfalse)
  • echo "lth2gtNo trades in databaselt/h2gt"
  • else
  • echo "lttable border1gt\n"
  • echo "lttrgtlttdgtTrade IDlt/tdgtlttdgtBuy Student
    IDlt/tdgtlttdgtSell Student IDlt/tdgtlttdgtISBNlt/tdgtlttdgtPr
    icelt/tdgtlttdgtBuySelllt/tdgtlt/trgt\n"
  • do
  • printf("lttrgtlttdgtslt/tdgtlttdgtslt/tdgtlttdgtslt/tdgtlttdgt
    slt/tdgtlttdgtslt/tdgtlttdgtslt/tdgtlt/trgt\n",
  • myrow0,myrow1, myrow2, myrow3,
    myrow4, myrow5)
  • while (myrow mysql_fetch_row(result))
  • echo "lt/tablegt\n"

9
Trading Example 5
  • Extract posted values
  • studid_POST'studid'
  • isbn_POST'isbn'
  • price_POST'price'
  • buysell_POST'buysell'

10
Trading Example 6
  • SQL statements
  • Select possible matching buy bids for my sell bid
  • SELECT tradeid,buyid,sellid,isbn,price,buysell
    FROM trades where isbn'isbn' and buysell 'buy'
    and price gt price order by price DESC"

11
Trading Example 7
  • SQL statements
  • Insert current sell bid into database
  • "INSERT INTO trades (sellid,isbn,price,buysell)
    VALUES ('studid','isbn','price','buysell')

12
Trading Example 8
  • SQL statements
  • Modify the best matching buy bid with my sell bid
  • UPDATE trades set sellid'studid',
    buysell'sold' where tradeid'myrow0'

13
Shopping Cart
  • Product Selection Form
  • ltform method"post" action"Shopping.php"gt
  • ltinput type"hidden" name"addproducts"
    value"add"/gt
  • ltinput type"hidden" name"IDs"
    value"1000,2000,3000"/gt
  • ..PRODUCTS.
  • ltinput type"submit" value"add"/gt
  • lt/formgt

14
Shopping Cart 2
  • Product Displayed
  • ProductRadio
  • ltinput type"hidden" name"prodID1000name"
    value"Radio"/gt
  • Price49.99ltinput type"hidden"
    name"prodID1000price" value"49.99"/gt
  • Numberltinput type"text" name"prodID1000num"
    value"0"/gt

15
Shopping Cart 3
  • Cart Item class definition
  • class cartitem
  • var prodID
  • var name
  • var price
  • var number
  • function cartitem(pID, n, p, num)
  • this-gtprodIDpID
  • this-gtnamen
  • this-gtpricep
  • this-gtnumbernum

16
Shopping Cart 4
  • Processing Product List 1000,2000,3000
  • count0
  • IDlist array(10)
  • whole_POST'IDs'
  • do
  • morestrstr(whole,',')
  • IDlistcountsubstr(whole,0,strlen(whole)-
    strlen(more))
  • wholesubstr(more,1,strlen(more))
  • count
  • while (more!false)

17
Shopping Cart 5
  • Retrieving Submitted Values
  • for(c0cltcountc)
  • prodIDIDlistc
  • name_POST'prodID'.prodID.'name'
  • price_POST'prodID'.prodID.'price'
  • number_POST'prodID'.prodID.'num'
  • if(number1gt0)
  • newCartItem new cartitem(prodID, name,
    price, number)
  • _SESSION'prodID'.prodIDnewCartItem

18
Shopping Cart 5
  • Display Shopping Cart
  • echo "lth3gtShopping Cart Contentlt/h3gt"
  • foreach ( _SESSION as key gt val)
  • if(substr(key,0,6)'prodID')
  • echo "nameval-gtname
  • prodIDval-gtprodID
  • priceval-gtprice
  • numberval-gtnumberltbrgt"

19
Summary
Main points to remember Mail function Auto
increment Fields Trade Example Shopping Cart
Write a Comment
User Comments (0)
About PowerShow.com