PHP Basics 1-1
PHP Basics 1-1
Basics of PHP - 2
Basics of PHP - 2
1. HTML forms
The user can submit information using a standard HTML form. It is just a standard HTML feature, neither PHP nor JavaScript. 1.1 <FORM> tag The HTML form must begin with !"#M$ and must end with %!"#M$ !"#M$ ... %!"#M$ &ou can specif' the fo((owing attributes to form$ tag "ptiona( attributes method)*P"ST* % method)*+,T* This determines the method of transmission of form data to the -eb server. The defau(t method is +,T, and a(( the form va(ues wi(( be sent as the parameter of .#L. action)*destination_UR * This determines the destination .#L that the form wi(( be submitted. This .#L must be a page that uses server/side techno(og' such as PHP, 0SP or 1+I. If omitted, the same page as form page 2itse(f3 wi(( be the destination. 1.2 HTML form items 0(most a(( standard user interface e(ements can be creates b' I4P.T$ tag. The va(ue of an' 5ind of e(ement wi(( be a(wa's sent to -eb server in the same 6key)value7 format. &ou can get each value of these e(ements b' using $_GET !key!" or $_PO#T !key!" arra' depending on the method specified in !"#M$ tag as fo((ows (1) #i$g%e-%i$e te&t 'o& input t'pe)*te8t* name)*key*$ "ptiona( attributes va(ue)*value* If 'ou specif' 6va(ue7, it wi(( be the initia( te8t disp(a'ed. t'pe)*password* If 'ou specif' 6password7 for 6t'pe7, then characters wi(( not be shown direct(' in the te8t bo8. +ood for password fie(d.
t'pe)*hidden* If 'ou specif' 6hidden7 for 6t'pe7, then the te8t bo8 wi(( not be disp(a'ed on screen. &ou can use this t'pe of te8t bo8 to store an' va(ue temporari(' in HTML page, then send bac5 again to the -eb server. This is common(' used for PHP page that 1
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
needs to store variab(es within generated page. si9e)n Specif' width of te8t bo8 in number of characters. 4ote that this does not (imit the number of characters user wi(( enter. !or that purpose, use ma8(ength)n instead.
ma8(ength)n Specif' ma8imum number of characters a((owed to enter from user. This is usefu( for user/I: or password input. (2) M*%ti-%i$e te&t 'o& te8tarea rows)*n* co(s)*m* name)*key*$ ... Multi-line content comes here ... %te8tarea$ 0n' te8t between te8tarea$ and %te8tarea$ wi(( be treated as data to be shown in mu(ti/ (ine te8t bo8. (3) B*tto$s input t'pe)*button* name)*key* va(ue)*value*$ input t'pe)*submit* name)*key* va(ue)*value*$ input t'pe)*reset* name)*key* va(ue)*value*$ 0(( these ; tag wi(( show buttons on screen. value determines the caption disp(a'ed on the button. t'pe)*button* wi(( create norma( button with !" function assigned. &ou must write JavaScript or other script to assign the behavior manua(('. t'pe)*submit* wi(( create submit button for the form. 1(ic5ing on this button wi(( send a(( the information entered in the form t'pe)*reset* wi(( create reset button for the form. 1(ic5ing on this button wi(( c(ear a(( the fie(ds in the form
(4) List 'o& a$+ ,om'o 'o& se(ect si9e)*n* name)*key*$ option$item1 %option$ option$item2 %option$ option$item3 %option$ ... %se(ect$ This group of tags wi(( create a List/bo8 or 1ombo/bo8. si9e)*n* determines the number of rows disp(a'ed on the screen. If n ) <, then it wi(( be a 1ombo/=o8. If n $ <, then it wi(( be a List/bo8. ,ver' item in the List/bo8 or 1ombo/bo8 wi(( be specified b' option$ > %option$ pair of tags. 0nd the te8t inside this tag wi(( be sent to -eb server as value.
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
"ptiona( attributes va(ue)*value* If 'ou specif' 6va(ue7, it wi(( be the va(ue to be sent instead of item. se(ected If 'ou specif' 6se(ected7 for one of option$ tags, then that item wi(( be initia((' se(ected. "n(' < option$ tag is a((owed to be se(ected.
(-) O.tio$ '*tto$ input t'pe)*radio* name)*key* va(ue)*value_1*$ input t'pe)*radio* name)*key* va(ue)*value_2*$ ... input t'pe)*radio* name)*key* va(ue)*value_n*$ This tag wi(( create < option button. 0nd usua((' 'ou must create ? or more option buttons to be practica(. 0(( input$ tags for a set of option buttons must have the same 6 $ame7, and each option button must have different 6/a%*e7. So that the form wi(( send appropreate 6key)value7 pair to the -eb server. If 'ou want to create mu(tip(e sets of option buttons, then 'ou must use different 6$ame7 for different set of option buttons. "ptiona( attributes chec5ed If 'ou specif' 6chec5ed7 for one of option buttons, then that option button wi(( be initia((' se(ected. "n(' < option button is a((owed to be se(ected within a set of option buttons.
(0) ,1ec2 'o& input t'pe)*chec5bo8* name)*key* va(ue)*value*$ This tag wi(( create < chec5 bo8. Since chec5 bo8es are a(wa's independent, 'ou can insert on(' < chec5 bo8, or mu(tip(e chec5 bo8es with tota((' independent settings. "ptiona( attributes chec5ed If 'ou specif' 6chec5ed7, then the chec5 bo8 wi(( be initia((' chec5ed.
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
%B ,8ecute S@L command B% CsE( ) *S,L,1T B !#"M table_name*D Cresu(t ) m5s9%_9*er52CsE(3 or dieD %B "utput resu(ts as HTML tab(e B% echo * tab(e border$n*D %B "utput fie(d names as tab(e header B% echo * tr$n*D for 2Cfie(d)FD Cfie(d I echo * %tr$n*D %B "utput a(( records B%
m5s9%_$*m_fie%+s2Cresu(t3D Cfie(dGG3 H
whi(e 2C(ine ) m5s9%_fetc1_arra52Cresu(t, M&S@LJ0SS"133 H echo * tr$n*D foreach 2C(ine as Cce((3 H echo * td$Cce(( %td$n*D I echo * %tr$n*D I echo * %tab(e$n*D %B 1(osing procedures B% m5s9%_free_res*%t2Cresu(t3D m5s9%_c%ose2C(in53D A$ %=":&$ %HTML$ 2#eEuired modification for rea( use3 %% !ree resu(t %% 1(osing connection
#ost_name K 4ame or IP address of M'S@L server. &ou shou(d use 6%oca%1ost7 if M'S@L server is in the same P1 as -eb server. $ser_name K M'S@L user name. ,:;T<O7L This is not the (ogin name of the server. %ass&ord K M'S@L password. ,:;T<O7L This is not the (ogin password of the server.
database_name K M'S@L database name to use table_name KM'S@L tab(e name to use. =o* ca$ a%so *se a$5 ot1er #6L comma$+s 1ere.
Practice K Tr' database_name ) 6test7 and table_name ) 6sa%es7 2"utput of the practice shou(d be (i5e shown ne8t page3 4
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
7o .
< ? ; Q R <QN
>ate
?FF?/FM/F< ?FF?/FM/F< ?FF?/FM/F? ?FF?/FM/F; ?FF?/FM/FQ ?FF?/FO/;F
Bra$c1
Ja5arta Medan Suraba'a Ja5arta Ma5assar Ja5arta
<tem
Trousers Suites Mesh =ag Suites Trousers T/Shirt
,ategor5
1(othes 1(othes 0ccessor' 1(othes 1(othes 1(othes
;$it Price
MNFFF ;QRFFF ?ONFF ;QRFFF MNFFF ;ONFF
;$it Profit
??FFF <QMFFF <PNFF <QMFFF ??FFF <FNFF
6t5
<? Q <F N ;Q QR
#a%es
O;PFFF <;NFFFF ?ONFFF ?MPFFFF ?PR?FFF <MO<FFF
Profit
?PQFFF RNNFFF <PNFFF <<MPFFF MQNFFF QNPFFF
2.2 :+?*st t1e +is.%a5 of ce%% &ou notice that numeric va(ues in practice output is (eft/justified, which is not appropriate. There is a(so no commas 6,7 in numbers that it is difficu(t to read. -e wi(( tr' to change the ce(( format in genera( wa' 2so that this code can be used for any tab(e3 ... %B "utput a(( records B% whi(e 2C(ine ) m'sE(JfetchJarra'2Cresu(t, M&S@LJ0SS"133 H echo * tr$n*D foreach 2C(ine as Cce((3 H if (is_$*meric($ce%%)) @ ec1o !<t+ a%ig$ABrig1tB>!C ec1o $*m'er_format($ce%%)C ec1o !<Dt+>$!C E e%se @ ec1o !<t+>$ce%%<Dt+>$!C E I echo * %tr$n*D I echo * %tab(e$n*D ... 4ow the output wi(( be (i5e this.
7o .
< ? ; Q R <QN
>ate
?FF?/FM/F< ?FF?/FM/F< ?FF?/FM/F? ?FF?/FM/F; ?FF?/FM/FQ ?FF?/FO/;F
Bra$c1
Ja5arta Medan Suraba'a Ja5arta Ma5assar Ja5arta
<tem
Trousers Suites Mesh =ag Suites Trousers T/Shirt
,ategor5
1(othes 1(othes 0ccessor' 1(othes 1(othes 1(othes
;$it Price
MN,FFF ;QR,FFF ?O,NFF ;QR,FFF MN,FFF ;O,NFF
;$it Profit
??,FFF <QM,FFF <P,NFF <QM,FFF ??,FFF <F,NFF
6t5
<? Q <F N ;Q QR
#a%es
O;P,FFF <,;NF,FFF ?ON,FFF ?,MPF,FFF ?,PR?,FFF <,MO<,FFF
Profit
?PQ,FFF RNN,FFF <PN,FFF <,<MP,FFF MQN,FFF QNP,FFF
2.F E/e$ 'etter 4a5 to ge$era%iGe ta'%e +is.%a5 The code in ?.< is so genera( that 5o* ca$ .ac2 t1ese co+es i$to a *ser-+efi$e+ f*$ctio$. If 'ou do that, 'ou then can simp(' ca(( that function to disp(a' an' 5ind of tab(e in an' wa' 2incl$din' searc# res$lt or (oinin' of m$lti%le tables)3
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Here is the wa' to create user/defined function to output tab(e records as HTML tab(e. HTML$ =":&$ Aphp ($1ost_$ameH $*ser_$ameH $.ass4or+H $+ata'ase_$ameH $s9%) @ %B 1onnect to M'S@L database B% C(in5 ) m'sE(Jconnect2ChostJname, CuserJname, Cpassword3 or dieD m'sE(Jse(ectJdb2CdatabaseJname3 or dieD %B ,8ecute S@L command B% CsE( ) *S,L,1T B !#"M table_name*D Cresu(t ) m'sE(JEuer'2CsE(3D if (I$res*%t) @ ec1o !<B>(Error i$ #6L)<DB> ! . m5s9%_error()C +ieC E ... m'sE(Jc(ose2C(in53D E A$ )losing *race of the function Same code as !1" ! %% 1(osing connection (elete this line (is%la& error message for SQL (elete this line Parameters for the function the user $ill s%ecif& $hen the& call this function -%ening *race of the function (elete this line #ame of the function
f*$ctio$ s1o4_M5#6L_ta'%e
%=":&$ %HTML$ -hen 'ou finished modification, 6s1o4_M5#6L_ta'%e..1.7. then 'ou shou(d save the fi(e as
4ow, 'ou can use this user/defined function as fo((ows. 1reate new fi(e, then enter the fo((owing code. HTML$ +he filename that &ou ha,e =":&$ sa,ed &our function definition Aphp reEuireJonce2*showJM'S@LJtab(e.php*3D CsE( ) *#ELE,T J FROM sa%es*D showJM'S@LJtab(e2*%oca%1ost*,*5oic1i*,**,*test*,CsE(3D A$ %=":&$ 'ust s%ecif& %arameters %HTML$ Save the new fi(e as new name 6 with .php e8tension. .p(oad both
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
6s1o4_M5#6L_ta'%e..1.7 and this new fi(e to -eb server, then tr' browsing this new fi(e. 2.K >is.%a5 of ta'%e t1at 1as re%atio$ to ot1er ta'%es Suppose we normali*ed 2optimi9ed3 structure of tab(e for previous practice as fo((ows. 6<tem7, 6,ategor57, 6;$it Price7 and 6;$it Profit7 fie(ds can be separated as 6<tem7 tab(e, then just use 6<tem 7o.7 in main tab(e as a pointer to 6Item7 tab(e 6Bra$c17 fie(d and 6,ategor57 fie(d can a(so be separated #emove 6#a%es7 and 6Profit7 fie(d because the' can be ca(cu(ated from other va(ues
6'ra$c17 tab(e
Bra$c1 7o.
< ; ? < Q <
>ate
?FF?/FM/F< ?FF?/FM/F< ?FF?/FM/F? ?FF?/FM/F; ?FF?/FM/FQ ?FF?/FO/;F
<tem 7o.
? ; P ; ? <
6t5
<? Q <F N ;Q QR
Bra$c1 7o.
< ? ; Q
Bra$c1
Ja5arta Suraba'a Medan Ma5assar
<tem 7o.
< ? ; Q R P M
6item7 tab(e
<tem
T/Shirt Trousers Suites 4ec5tie ,ar #ing Mesh =ag =utton
,ategor5 7o.
< < < ? ? ? ?
;$it Price
;O,NFF MN,FFF ;QR,FFF RQ,FFF <O,NFF ?O,NFF M,FFF
,ost
?O,FFF RP,FFF <ON,FFF QM,FFF <<,FFF <;,FFF R,FFF
;$it Profit
<F,NFF ??,FFF <QM,FFF M,FFF N,NFF <P,NFF ?,FFF
,ategor5 7o. 1
6categor57 tab(e
)lothes Accessor&
,ategor5
7o4H 1o4 ca$ 4e +is.%a5 t1e same o*t.*t as 2.2 L -e((, 'ou can do it b' just giving appropriate #6L comma$+ to the same function !irst, (ets MO<7 ? tab(es into <, 6sa%es27 and 6'ra$c17 1. 2. F. K. #ELE,T S4o.S, :ate, =ranch, SItem 4o.S, Et' FROM sa(es?, branch NHERE sa(es?.S=ranch 4o.S ) branch.S=ranch 4o.S OR>ER B= S4o.S 1. 2. F. K. Specif' a(( fie(ds that 'ou want to disp(a'. .se bac5/Euote 6O7 2not sing(e Euote, but a bac5 EuoteTT3 to surround fie(d name that has space or s'mbo(. P I Q R Specif' two tab(es that 'ou want to join. O 1 2 F Specif' the (in5 between two tab(es Ta' 6 N E Specif' sorting order 7
,a.s Loc2 : # >
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
>ate
?FF?/FM/F< ?FF?/FM/F< ?FF?/FM/F? ?FF?/FM/F; ?FF?/FM/FQ ?FF?/FO/;F
Bra$c1
Ja5arta Medan Suraba'a Ja5arta Ma5assar Ja5arta
<tem 7o.
? ; P ; ? <
6t5
<? Q <F N ;Q QR
Then, the comp(ete S@L command to produce e8act(' the same output with ?.? wi(( be as fo((ows S,L,1T S4o.S, :ate, =ranch, Item, 1ategor', S.nit PriceS, @t', O;$it PriceO J 6t5 as #a%esH O;$it ProfitO J 6t5 as Profit !#"M sa(es?, branch, item, categor' -H,#, 2sa(es?.SItem 4o.S ) item.SItem 4o.S3 04: 2sa(es?.S=ranch 4o.S) branch.S=ranch 4o.S3 04: 2item.S1ategor' 4o.S) categor'.S1ategor' 4o.S3 "#:,# =& S4o.S
2.- Perform statistica% a$a%5sis of +ata =asica((', 'ou can a(so do statistica( ana('sis of data using the S,L,1T command. E&am.%e 18 Tota% sa%es of eac1 'ra$c1 S,L,1T =ranch, #;M2S.nit PriceS B @t'3 as STota( Sa(esS !#"M sa(es?, branch, item -H,#, 2sa(es?.SItem 4o.S ) item.SItem 4o.S3 04: 2sa(es?.S=ranch 4o.S) branch.S=ranch 4o.S3 GRO;P B= =ranch 2The output wi(( (oo5 (i5e this3
Bra$c1 Ja5arta Ma5assar Medan Suraba'a Tota% #a%es N?,<N;,?FF RO,?FN,FFF Q<,?Q?,?FF N;,PO?,QFF
E&am.%e 28 :/erage $*m'er of items so%+ i$ a +a5 S,L,1T item, :3G2@t'3 as S0verage @t'S !#"M sa(es?, item -H,#, 2sa(es?.SItem 4o.S ) item.SItem 4o.S3 GRO;P B= item
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
:/erage 6t5
<; P? <O QM M RN ?;
2.0 #earc1i$g t1ro*g1 +ata'ase =asica((', searching through database is the same as showing database b' S@L command using NHERE phrase. Much more critica( part is the /a%i+atio$ of +ata sent from user. Suppose we have the fo((owing tab(e of participants.
Name
Mic5e' Mouse Minnie Mouse :ona(d :uc5 =uff' :ogg' :umbo ,(ephant
Sex
M ! M M M
Birthday
<OP</FM/F< <ONM/FO/?; <ONO/<</?R ?FF?/FM/F; ?FF?/FM/FQ
Address
;R;R, -a(( street, 10 40!,:, Ja5arta #,TP1, Suraba'a JI10, To5'o M",, Somewhere
Telephone
F?/<?;Q/FONM F?/?;Q</ORM; F?/FQO?/OM<; F;/PM;</<?;< FQ/QN?M/;M<O
HTML$ =":&$ H<$Search in participants database %H<$ !"#M method)*P"ST*$ p$4ameL I4P.T t'pe)*te8t* name)*name*$ %p$ p$Se8L I4P.T t'pe)*radio* name)*se8* va(ue)*M*$Ma(e I4P.T t'pe)*radio* name)*se8* va(ue)*!*$!ema(e %p$ p$ I4P.T t'pe)*submit* name)*submit* va(ue)*"U*$ %p$ p$ I4P.T t'pe)*reset* name)*reset* va(ue)*1(ear*$ %p$ %!"#M$ H#$ Aphp .unction to re%ort error function inva(id2CerrorJmessage3 H echo * p$ font co(or)VredV$CerrorJmessage %font$ %p$*D echo * %=":&$ %HTML$*D dieD I reEuireJonce2*showJM'S@LJtab(e.inc*3D if 2isset2CJP"STW*submit*X33 H CsE( ) *S,L,1T B !#"M participants -H,#,*D Coption ) **D if 2isset2CJP"STW*name*X33 H
.orm
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
Cname ) CJP"STW*name*XD if 2str%e$2Cname3 $ RF3 inva(id2*4ame is too (ong*3D if 2Cname T) **3 Coption .) * 4ame LIU, VYCnameYV*D I if 2isset2CJP"STW*se8*X33 H if 2Coption T) **3 Coption .) * 04:*D Coption .) * Se8 ) V*.CJP"STW*se8*X.*V*D I if 2Coption T) **3 H CsE( .) CoptionD echo * p$*, 1tm%s.ecia%c1ars2CsE(3, * %p$*D showJM'S@LJtab(e2*(oca(host*,*'oichi*,**,*test*,CsE(3D I I A$ %=":&$ %HTML$ .sefu( data va(idation functions isset2Cvar3 #eturn T#., if the specified variab(e has been set 2e8ists3 %% This wi(( show nothing 2!0LS,3 %% 4ow this wi(( show 6<7 2T#.,3
Cvar ) *He((oT*D echo isJnumeric2Cvar3D %% This wi(( show nothing Cvar ) *<?;Q*D echo isJnumeric2Cvar3D %% 4ow this wi(( show 6<7 2T#.,3 chec5date2Cmont#,Cday,Cyear3 echo chec5date2?,;<,?FF;3D echo chec5date2?,?N,?FF;3D #eturn T#., if the specified combination of &ear, Month, :a' is va(idD Z chec5 (eap 'ears. %% This wi(( show nothing 2!0LS,3 %% 4ow this wi(( show 6<7 2T#.,3
str(en2Cvar3 #eturn number of characters contained in specified string variab(e Cvar ) *He((oT*D echo str(en2Cvar3D %% This wi(( show 6P7 26He((oT7 has P characters3
10
ADMTC-UCSC-University of Colombo
Basics of PHP - 2
.sefu( data conversion functions htm(specia(chars2Cvar3 1onvert an' specia( characters for HTML contained in specified string to a safe notation of HTML disp(a' Cvar ) * I$ L"[, &". %I$*D echo CvarD %% This wi(( show 6 "+, -"U7 in Ita(ic echo htm(specia(chars2Cvar3D %% This wi(( show 6 I$ L"[, &". %I$7 stripJtags2Cvar3 Strip a(( HTML and PHP tags from the specified string. .sefu( for message board s'stem to prevent abuse.
Cvar ) * a href)Vbad.htm(V$1(ic5 here %a$*D echo CvarD %% This wi(( show 61(ic5 here7 with a (in5 echo stripJtags2Cvar3D %% This wi(( show 61(ic5 here7 in norma( te8t substr2Cvar,Cstart,Clen't#3 #eturn part of specified string, starting from specified position and for the specified (ength. ,:;T<O7L Cstart must be counted from 6F7, not 6<7 %% This wi(( show who(e te8t %% This wi(( show 6just7 on('
11