Date post: | 07-Apr-2018 |
Category: |
Documents |
Upload: | vin-raon-yu |
View: | 223 times |
Download: | 0 times |
of 110
8/6/2019 Building an Online Bidding Application Using PHP
1/110
Building an online bidding
application using PHP/MySQL
8/6/2019 Building an Online Bidding Application Using PHP
2/110
Introduction
Purpose of this lecture is to show how tobuild an online application using PHP/
MySQL platform This lecture is intended for developerswho wish to develop web-basedapplication but not familiar with PHP
syntaxes
8/6/2019 Building an Online Bidding Application Using PHP
3/110
Online bidding application
Application is an online auction systemwhere items for bid are displayed to
browsers Interested browsers create/ log-in to theiraccount
Once logged-in, bidders can submit bidsfor items that are auctioned
8/6/2019 Building an Online Bidding Application Using PHP
4/110
Outline
PHP basics MySQL connection Creation of accounts
Managing logins Adding bid items Deleting bid items Logging out
Viewing bid items Accepting bids Listing bids for each bid item
8/6/2019 Building an Online Bidding Application Using PHP
5/110
Outline Topic #1 PHP Basics
8/6/2019 Building an Online Bidding Application Using PHP
6/110
PHP basics
Background
Web application architecture
Basic syntax tags
Variables
O
perators Flow Control and Looping
8/6/2019 Building an Online Bidding Application Using PHP
7/110
PHP basics-background
Server-side scripting language for the web
PHP codes are embedded in HTML todynamically generate web pages
Originally stood for Personal Home Page
Changed to PHP Hypertext Processor
Database integration
MySQL, PostgreSQL, etc Portability
Run on Linux, UNIX, Windows, MacOS platforms
8/6/2019 Building an Online Bidding Application Using PHP
8/110
PHP basics-web application
architecture
To open a web page in browser: Type in a Universal Resource Locator (URL)
Click an existing link A request is sent to the web server
Web server locates the page and sends it back to the browser
Browser displays the page
8/6/2019 Building an Online Bidding Application Using PHP
9/110
PHP basics-web application
architecture model Three Tiered Model Presentation Layer
Web browser- displays and submits the data
Application Layer Some programs or scripts which processes the data
PHP, JSP, Servlets, ASP,CGI, etc.
Database Layer
Provides the data that the application layer needs May be an RDBMS, a flat file, XML documents, etc.
8/6/2019 Building an Online Bidding Application Using PHP
10/110
PHP basics- tags
Tells web server where PHP code ends
Code in between are processed
Ex:
8/6/2019 Building an Online Bidding Application Using PHP
11/110
PHP basics- tags, an example
The code below outputs 10 + 15 = 25
8/6/2019 Building an Online Bidding Application Using PHP
12/110
PHP basics- variables
No need to declare variables
Must start with dollar sign ($)
Values are assigned to variables by theassignment operator =
$name=Jun Dolor;
$qty=100;
8/6/2019 Building an Online Bidding Application Using PHP
13/110
PHP basics- operators
String
Arithmetic
Assignment Comparison
Logical
8/6/2019 Building an Online Bidding Application Using PHP
14/110
PHP basics- operators, string
Concatenation
. appends strings
8/6/2019 Building an Online Bidding Application Using PHP
15/110
PHP basics- operators, arithmetic
+ addition
- subtraction
* multiplication / division
% modulus
8/6/2019 Building an Online Bidding Application Using PHP
16/110
PHP basics- operators, arithmetic
Operator Use Equivalent to
+= $a += $b; $a= $a + $b;
-= $a -= $b; $a = $a - $b;
*= $a *= $b; $a = $a * $b;
/= $a /= $b; $a = $a / $b;
%= $a %= $b; $a = $a % $b;
.= $a .= $b; $a = $a . $b;
8/6/2019 Building an Online Bidding Application Using PHP
17/110
PHP basics- operators, comparison
Operator Name Example
== Equals $a == $b
=== Identical $a === $b
!= Not equal $a != $b
Not equal $a $b
< Less than $a < $b
> Greater than $a > $b
= $b
8/6/2019 Building an Online Bidding Application Using PHP
18/110
PHP basics- operators, logical
Operator Name Use Result
! NOT !$b Returns true if $b is falseand vice-versa
&& AND $a && $b Returns true if both $a and$b are true, otherwise false
|| OR $a || $b Returns true if either $a or $b is true, otherwise false
and AND $a and $b Same as &&, but with lower precedence
or OR $a or $b Same as ||, but with lower precedence
8/6/2019 Building an Online Bidding Application Using PHP
19/110
PHP basics-arrays
Arrays are special type of variables forstoring a list of data and manipulatingthem efficiently
An array stores multiple data items,divided into a number of slots
8/6/2019 Building an Online Bidding Application Using PHP
20/110
PHP basics-initializing numericallyindexed arrays
Initialization by direct assignment
Initialization by array construct
Array elements can be of different types
8/6/2019 Building an Online Bidding Application Using PHP
21/110
PHP basics-initializing numericallyindexed arrays 2
Arrays are usually assigned sequentially
In PHP, arrays can be assigned non-sequentially
8/6/2019 Building an Online Bidding Application Using PHP
22/110
PHP basics-using numericallyindexed arrays
To access an array element, or part of an array,use a number called index or subscript
Index number or subscript
Assigned to each member of the array to allow theprogram to access an individual member of the array
Usually begins with zero and progress sequentially bywhole numbers to the end of the array
NOTE: Elements inside arrays are from 0 tp(sizeOfArray 1)
8/6/2019 Building an Online Bidding Application Using PHP
23/110
PHP basics-using numericallyindexed arrays 2
Accessing a single element
Accessing elements of an array using a for
loop
Accessing elements of an array using
foreach loop
8/6/2019 Building an Online Bidding Application Using PHP
24/110
PHP basics-initializing string-indexed arrays
Initialization by direct assignment
Initialization by array construct
Accessing a single element
8/6/2019 Building an Online Bidding Application Using PHP
25/110
PHP basics-initializing string-indexed arrays 2
Accessing elements of a string-indexedarray using list()/ each()
8/6/2019 Building an Online Bidding Application Using PHP
26/110
PHP basics-each()
Receives an array as a parameter
Returns the current key and value pair as anarray with four elements
If the internal pointer for the array past the endof the array contents, each() returns FALSE
Moves the array pointer to the next element
Use reset() to move the array back at the firstelement
8/6/2019 Building an Online Bidding Application Using PHP
27/110
PHP basics-each() 2
8/6/2019 Building an Online Bidding Application Using PHP
28/110
PHP basics-list()
Used to assign multiple variables in oneoperation
Usually used to capture values returned byeach()
8/6/2019 Building an Online Bidding Application Using PHP
29/110
PHP basics-foreach()
Gives an easy way to iterate over arrays
On sample code below:
For each loop, the value of the current element is
assigned to $value Internal array pointer is advanced by one
On the next loop, the next element is referenced
8/6/2019 Building an Online Bidding Application Using PHP
30/110
PHP basics-foreach() 2
On the sample code below:
The current elements key will be assigned to thevariable $k on each loop
8/6/2019 Building an Online Bidding Application Using PHP
31/110
PHP basics- multi-dimensionalarrays
PHP supports multi-dimensional arraysthrough array of arrays
8/6/2019 Building an Online Bidding Application Using PHP
32/110
PHP basics- flow control andlooping
if
if-else
switch
for
while
do-while
break
exit or die
8/6/2019 Building an Online Bidding Application Using PHP
33/110
PHP basics- if/ if-else statement
Statement- if:
A statement (or block of code) will executeonly if a certain boolean is true
Statement if-else
A statement (or block of code) will executeif a condition is true and a different
statement if the condition is false
8/6/2019 Building an Online Bidding Application Using PHP
34/110
PHP basics- switch statement
Allows branching on multiple outcomes
When a switch is encountered: PHP evaluates expression and jumps to case whose
selector matches the value of the expression Program executes the statements in order from that
point on until a break statement is reached
This skips to the statement after the first statement atthe end of the switch
Should none of the case is satisfied, the default blockis executed
Default blocks are optional
8/6/2019 Building an Online Bidding Application Using PHP
35/110
PHP basics- for loop
Executes the same code a number oftimes Syntax: for
(expression1;condition;expression2){statement/s} Where:
Expression1 is executed at the start; the counter isinitialized here
Condition is the test before each iteration Expression2 is evaluated at the end of each
iteration
Statement/s are executed once for each iteration
8/6/2019 Building an Online Bidding Application Using PHP
36/110
PHP basics- while loop
Contains statements or block ofstatements to be repeated as long as thecondition is true
Syntax: while(condition){statement/s}
Statements inside the loop are executedfor as long as the condition take the true
value
8/6/2019 Building an Online Bidding Application Using PHP
37/110
PHP basics- do while loop
Contains statement or block of statementsthat repeats as long as the condition istrue
Main difference between while and do-while loop:
Statements within a do-while loops executes
at least once
Syntax: do{statement/s}while(condition)
8/6/2019 Building an Online Bidding Application Using PHP
38/110
PHP basics- break
Terminates the enclosed switch statementand flow of control transfers to the nextstatement after switch
This is also used to terminate for, whileand do-while loops
8/6/2019 Building an Online Bidding Application Using PHP
39/110
PHP basics- exit or die
Stops the execution of the script with anoptional error message
8/6/2019 Building an Online Bidding Application Using PHP
40/110
Outline Topic #2 MySQL
Connection
8/6/2019 Building an Online Bidding Application Using PHP
41/110
MySQL Database Connection
Overview of database structure
Connecting to MySQL database
Selecting the database to use Using the require_once statement
8/6/2019 Building an Online Bidding Application Using PHP
42/110
Overview of Database connection
Database: auction
Tables
tblaccount tblbiditems
tblbidhistory
8/6/2019 Building an Online Bidding Application Using PHP
43/110
Table tblaccount
This will hold the account info of bidders/auctioneers
Table structure Column accountid: integer, primary key, auto-increment
Column username: string 50 chars
Columnpassword: string 50 chars
8/6/2019 Building an Online Bidding Application Using PHP
44/110
Table tblbiditems
This will hold the items auctioned forbidding
Table structure Column biditemid: integer , primary key, auto-
increment
Column accountid: string 50 chars This identifies the auctioneer
Column biditem: string 50 chars
Column biddesc: tiny text
8/6/2019 Building an Online Bidding Application Using PHP
45/110
Table tblbidhistory
This will hold the bid info for each itembeing auctioned
Table structure Column bidhistoryid: integer , primary key,
auto-increment
Column accountid: integer
Column biditemid: integer
Column bidprice: double
Column dtesubmitted: datetime
8/6/2019 Building an Online Bidding Application Using PHP
46/110
Connecting to databases:
Function mysql_connect: Creates a connection to MySQL Syntax: mysql_connect($hostname,
$username,$password)
Ex: $conn=mysql_connect(localhost,root,password)
Function mysql_select_db Specifies the database in MySQL for use Syntax: mysql_select_db($database, $connection) Ex: mysql_select_db(auction, $conn)
Function die Terminates execution of PHP script
8/6/2019 Building an Online Bidding Application Using PHP
47/110
Connecting to MySQL andselecting auction database
Create file dbconnect.inc
For code reuse, a separate file can be createdto connect to the database
PHP pages can call dbconnect.inc to connectyo the auction database
8/6/2019 Building an Online Bidding Application Using PHP
48/110
Reusing the database connection
Function require_once()
Loads a file into a PHP script
8/6/2019 Building an Online Bidding Application Using PHP
49/110
Outline Topic #3 Creation of
Accounts
8/6/2019 Building an Online Bidding Application Using PHP
50/110
Creation of accounts
HTML form handling
MySQL commands
Function mysql_query() Function mysql_error()
Adding records
SQL insert statement
8/6/2019 Building an Online Bidding Application Using PHP
51/110
HTML form handling
Create:
File index.html
File addaccount.html
File addaccountprocess.php
$_POST array
8/6/2019 Building an Online Bidding Application Using PHP
52/110
File index.html
First page that displays
Provide the user with the option to create
accounts
8/6/2019 Building an Online Bidding Application Using PHP
53/110
File addaccount.html
Displays a form for accepting new accountinfo
8/6/2019 Building an Online Bidding Application Using PHP
54/110
File addaccountprocess.php
$_POST array Special arrays that hold all form variables
Function mysql_query() Executes an SQL statement on the database
Function mysql_error() Displays error encountered when executing
an SQL statement SQL Insert
Adds a record on a database table
8/6/2019 Building an Online Bidding Application Using PHP
55/110
File addaccountprocess.php script
8/6/2019 Building an Online Bidding Application Using PHP
56/110
Create accounts:
Username: auctioneer1
This account will place items for bidding
Usernames: bidder1, bidder2 These account will bid for item auctioned off
8/6/2019 Building an Online Bidding Application Using PHP
57/110
Outline Topic #4 Managing Logins
8/6/2019 Building an Online Bidding Application Using PHP
58/110
Managing logins
SQL select statement
Function mysql_num_rows
Function isset()
Session
URL rewriting Querystring
$_GET array
Create:
File login.php File loginverify.php
File checkstatus.inc
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
59/110
SQL select statement
Example 1: select * from tblaccount Selects all columns/ rows from table tblaccount
Example 2: select username, password from tblaccount Selects columns username and password for all rows in table
tblaccount Example 3: select * from tblaccount where
username=jundolor Selects all columns from table tblaccount for all rows whose
column username contains jundolor
Example 4: select accountid from tblaccount whereusername=media Selects column accountid from tblaccount for all rows whose
column username contains media
8/6/2019 Building an Online Bidding Application Using PHP
60/110
Function mysql_num_rows
Retrieves the number of rows from a resultset
Can only be used for SQL selectstatements
8/6/2019 Building an Online Bidding Application Using PHP
61/110
Function isset()
Checks if a variable exist
Example: isset($name)
This check if the variable $name exist
8/6/2019 Building an Online Bidding Application Using PHP
62/110
Sessions
Special variables stored in web servers
Allows passing of information betweenweb pages
Call the function session_start() at thestart of scripts that will use sessions
8/6/2019 Building an Online Bidding Application Using PHP
63/110
URL Rewriting
Querystring
Information can be passed on by appendingvariable/value to the URL
$_GET array
Special array that holds all querystring values
8/6/2019 Building an Online Bidding Application Using PHP
64/110
File login.php code
8/6/2019 Building an Online Bidding Application Using PHP
65/110
File login.php browser shot
8/6/2019 Building an Online Bidding Application Using PHP
66/110
File loginverify.php code
8/6/2019 Building an Online Bidding Application Using PHP
67/110
File checkstatus.inc code
8/6/2019 Building an Online Bidding Application Using PHP
68/110
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
69/110
Outline Topic #5 Adding Items
to Auction
8/6/2019 Building an Online Bidding Application Using PHP
70/110
Adding items to auction
File menu.php
Create:
File addauctionitem.php
File addauctionitemprocess.php
8/6/2019 Building an Online Bidding Application Using PHP
71/110
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
72/110
File addauctionitem.php code
Fil dd ti it h
8/6/2019 Building an Online Bidding Application Using PHP
73/110
File addauctionitem.php screenshot
8/6/2019 Building an Online Bidding Application Using PHP
74/110
File addauctionprocess.php
8/6/2019 Building an Online Bidding Application Using PHP
75/110
Outline Topic #6 Deleting Bid
Items
8/6/2019 Building an Online Bidding Application Using PHP
76/110
Deleting Bid Items
Function mysql_fetch_array()
Writing querystring URL to identify recordsto delete
SQL delete statement
Create:
File listauctionitems.php
File: deletebiditem.php
8/6/2019 Building an Online Bidding Application Using PHP
77/110
Function mysql_fetch_array()
Fetches a row as an associative from aselect query result set
8/6/2019 Building an Online Bidding Application Using PHP
78/110
Sample mysql_fetch_array() code
W iti t i URLt id tif
8/6/2019 Building an Online Bidding Application Using PHP
79/110
Writing querystring URLto identifyrecords to delete
Auction items belonging to current accountwill be selected
A loop will be created to go through eachrow
Each row will hyperlink to a PHP basedpage for deletion
To identify the row, a querystring variablewill be appended to the URL
W iti t i URLt id tif
8/6/2019 Building an Online Bidding Application Using PHP
80/110
Writing querystring URLto identifyrecords to delete- code
8/6/2019 Building an Online Bidding Application Using PHP
81/110
SQL delete statement
Example 1: delete from tblaccount
Deletes all rows on table tblaccount
Example 2: delete from tblaccount whereaccountid=1
Deletes only rows matching the condition
8/6/2019 Building an Online Bidding Application Using PHP
82/110
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
83/110
File listauctionitems.php
8/6/2019 Building an Online Bidding Application Using PHP
84/110
File deletebiditem.php
8/6/2019 Building an Online Bidding Application Using PHP
85/110
Outline Topic #7 Logging Out
8/6/2019 Building an Online Bidding Application Using PHP
86/110
Loggin out
Function session_destroy()
Create:
File logout.php
8/6/2019 Building an Online Bidding Application Using PHP
87/110
Function session_destroy()
Terminates all session variables stored inserver memory
8/6/2019 Building an Online Bidding Application Using PHP
88/110
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
89/110
File logout.php
Once logout.php is called, all sessionvariable will be dropped from servermemory
Browser will not be able to access anypage calling checkverify.php (ex:menu.php)
8/6/2019 Building an Online Bidding Application Using PHP
90/110
Outline Topic #8 Viewing Bid
Items
8/6/2019 Building an Online Bidding Application Using PHP
91/110
Viewing bid items
Establishing relations between tables
SQL natural join clause
Create: File listbiditems.php
8/6/2019 Building an Online Bidding Application Using PHP
92/110
Establishing relations
Table tblbiditem Holds the items being
auctioned off
Column accountid identifiesthe owner if the auctioned
item
Table tblaccount Holds account information
of the owner of the itembeing auctioned
Column accountid Links the owner of the
account to the auction item
8/6/2019 Building an Online Bidding Application Using PHP
93/110
SQL natural join clause
Used with SQL select statement
Connects rows between different tablesvia their common column
8/6/2019 Building an Online Bidding Application Using PHP
94/110
File menu.php
8/6/2019 Building an Online Bidding Application Using PHP
95/110
File listbiditems.php
All items with their respective ownersbeing auction are listed
Each item will hyperlink to a PHP page for
accepting bids Accepting bids will be covered in the next
topic section
Each hyperlink will append a querystringvariable to identify it in the PHP page foraccepting bids
8/6/2019 Building an Online Bidding Application Using PHP
96/110
File listbiditems.php code
8/6/2019 Building an Online Bidding Application Using PHP
97/110
File listbiditems.php screen shot
8/6/2019 Building an Online Bidding Application Using PHP
98/110
Outline Topic #9 Accepting
Bids
8/6/2019 Building an Online Bidding Application Using PHP
99/110
Accepting bids
Using hidden fields to store ID numbers
MySQL now() function
Create:
File acceptbid.php
File acceptbidprocess.php
8/6/2019 Building an Online Bidding Application Using PHP
100/110
Hidden fields
Not displayed to the browser
Used to pass constant values
8/6/2019 Building an Online Bidding Application Using PHP
101/110
File acceptbid.php
Place the id of the auction item in a hidden field
Fil bid h h
8/6/2019 Building an Online Bidding Application Using PHP
102/110
File acceptbid.php screen shot
File acceptbid php HTML
8/6/2019 Building an Online Bidding Application Using PHP
103/110
File acceptbid.php HTMLgenerated code
M SQL () f ti
8/6/2019 Building an Online Bidding Application Using PHP
104/110
MySQL now() function
Returns the current date and time as avalue in 'YYYY-MM-DD HH:MM:SS' orYYYYMMDDHHMMSS.uuuuuu format
depending on whether the function is used ina string or numeric context
The value is expressed in the current time
zone.
Fil tbid h
8/6/2019 Building an Online Bidding Application Using PHP
105/110
File acceptbidprocess.php
R lti d
8/6/2019 Building an Online Bidding Application Using PHP
106/110
Resulting records
8/6/2019 Building an Online Bidding Application Using PHP
107/110
Outline Topic #10 Listing Bids
For Each Bid Item
Li ti bid f h bid it
8/6/2019 Building an Online Bidding Application Using PHP
108/110
Listing bids for each bid item
MySQL date_format() function
Relating information from two or moretables
SQL order by clause
M SQL d t f t() f ti
8/6/2019 Building an Online Bidding Application Using PHP
109/110
MySQL date_format() function
Formats a string based on a specifiedformat
The following are some of the specifies of
the format string: %D: Day of month with English suffix
%d: Numeric day of month (0131)
%M: Month name (JanuaryDecember) %m: Month numeric (0112)
%Y: Year (4 digits)
%y: Year (2 digits)
M SQL d t f t() l
8/6/2019 Building an Online Bidding Application Using PHP
110/110
MySQL date_format() sample