CE Festival - PowerPoint PPT Presentation

About This Presentation
Title:

CE Festival

Description:

2 lessons (tonight, and next Friday) Tonight: Introduction to PHP ... One example is header and footer for a homepage, but it's not directly related to PHP ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 30
Provided by: Tab92
Category:
Tags: festival | footer

less

Transcript and Presenter's Notes

Title: CE Festival


1
CE Festival PHP Course
  • PHP Course
  • by Tabris
  • Organized by Computer Engineering Association

2
Schedule
  • Schedule
  • 2 lessons (tonight, and next Friday)
  • Tonight
  • Introduction to PHP
  • Basic programming skill in PHP
  • Magic you can do with PHP
  • QA

3
Schedule (Cont)
  • Next Friday
  • Write your PHP Applications
  • E.g., Discussion board, File uploader
  • Database access of PHP (optional?)
  • QA

4
Whats PHP??
  • Its a server-side language
  • Its compiled (parsed) before sending to
    end-users browser
  • Apart from PHP, CGI, JSP, ASP are also
  • Source code is hided
  • PHP can be code-mixing (i.e., mixing with HTML
    code)
  • So its very convenient

5
What can PHP done?
  • Provides Dynamic Content (interactive)
  • Normal HTML is static
  • Javascript is only client-side
  • Form Handling
  • Data Access (File, Database)

6
Yours 1st PHP Program
  • Very simple, just a hello world program
  • lt?php
  • echo Hello World!
  • ?gt
  • But how to run??

7
How to run a PHP Program
  • To run a PHP Program, you must have a web space
    with web server supporting PHP (usually by
    module)
  • Do we have??
  • YES! OUR CSIS webserver is using Apache and it
    supports PHP
  • The pity is that only CSIS server supports while
    EEE and HKUSUA server dont

8
How to run a PHP Program
  • Save your file as helloworld.php in
    W\public_html
  • telnet to virtue
  • goto /homepage/students/ce/xx/yr_login/public_html
  • Change Permission chmod 644 helloworld.php
  • Access from your browser http//www.csis.hku.hk/y
    r_login/helloworld.php

9
Structure of PHP Program
  • Normal Structure
  • lt?php
  • PHP Code here
  • ?gt
  • Mixed with HTML (save it as test.php)
  • lthtmlgt
  • ltheadgtlttitlegtPHP Courselt/titlegtlt/headgt
  • ltbodygt
  • Im from HTML! ltbrgt
  • lt?php // this is all PHP code
  • echo "ltbgtIm from PHP!lt/bgt"
  • ?gt
  • lt/bodygt lt/htmlgt

10
Output?
  • So whats the parsed output?
  • lthtmlgt
  • ltheadgtlttitlegtPHP Courselt/titlegtlt/headgt
  • ltbodygt
  • Im from HTML! ltbrgt
  • ltbgtIm from PHP!lt/bgt
  • lt/bodygt lt/htmlgt
  • the string ltbgtIm from PHP!lt/bgt is echoed to
    the output
  • echo likes printf() in C or cout in C
  • Thats why the source code can be hided

11
Syntax of PHP
  • You all must be familiar
  • Very similar like that of C/C/JAVA
  • But variable starts with
  • NO NEED TO DECLARE!!!!
  • lt?php //actually it can be lt?
  • var1Im a string!
  • var2123
  • echo Hello! var1 var2
  • ?gt

12
Syntax of PHP
  • Control structure
  • If-then-else, for loop, while loop, switch case
  • Boolean Operator , ,
  • Arithmetic Operator - /
  • PHP is not rigorous, frankly speaking

13
Syntax of PHP
  • See the following example (save as price.php)
  • lthtmlgt
  • lt?php
  • unit_price3
  • quantity5
  • costunit_pricequantity
  • ?gt
  • The price is lt? echo cost?gt.ltbrgt
  • lt/htmlgt

14
Syntax of PHP
  • From last example, PHP code can be cut into
    fragments with HTML code
  • Also, it can be included like C/C
  • Make more flexible and convenient
  • One example is header and footer for a homepage,
    but its not directly related to PHP

15
Magic you can do with PHP
  • The Magic is HTML Form Processing
  • Form is commonly used in the Internet
  • Get information from user
  • Let user to post data
  • Verification
  • A lot more!

16
Magic you can do with PHP
  • Form Let user enter information or data from
    browser
  • ltform actionform.php methodpostgt
  • Name ltinput typetext namenamegtltbrgt
  • Class ltinput typeradio nameclass
    value1gt CE1
  • ltinput typeradio nameclass value2gt CE2
  • ltinput typeradio nameclass value3gt
    CE3ltbrgt
  • ltinput typesubmit namesubmitgt
  • ltinput typeresetgt
  • lt/formgt
  • Save above as form.html
  • Must have a destination of data post to
    (form.php) this case

17
Form Processing
  • Browser form.html in your browser
  • http//www.csis.hku.hk/yr_login/form.html
  • Anyone browsing your page can enter data, but how
    we can get and process those data?
  • Recall that the data is posted to form.php
  • So of coz we can back the data in that php file,
    but how??

18
Form Processing
  • Actually PHP does all for us
  • In form.php file, if weve posted some data to
    it, it AUTOMATICALLY declare those variables
  • So now name and class store the name and class
    from the Form
  • The variable name depends on the name of the
    entry on the form
  • MAGIC?

19
What do we want to do?
  • Check whether theyve enter both field
  • check whether name and class
  • Printout what class they belong to
  • class1 -gt Part I
  • class2 -gt Part II
  • class3 -gt Part III

20
form.php
  • lthtmlgtltbodygt
  • lt?php
  • if (name"" class"")
  • echo "You have not entered enough..."
  • else
  • echo "Hi! name.ltbrgt"
  • switch(class)
  • case "1" echo "You're CE Part Iltbrgt" break
  • case "2" echo "You're CE Part IIltbrgt" break
  • case "3" echo "You're CE Part IIIltbrgt" break
  • default break
  • ?gt
  • lt/bodygtlt/htmlgt

21
Form processing
  • So If I enter Tabris as name and I choose
    CE2, then form.php is processed and output the
    following
  • lthtmlgt
  • ltbodygt
  • Hi! Tabris.ltbrgtYou're CE Part IIltbrgtlt/bodygt
  • lt/htmlgt

22
Form Processing
  • If an user miss an entry, the first IF will be
    true and then
  • You have not entered enough
  • will be printed
  • By this example, you may think some other
    application of Form using PHP
  • User verification
  • Application Form

23
String Processing
  • From the source code, notice that class is a
    string not a integer
  • Basically all values returned from Form should be
    string
  • PHP is very similar to C that support direct
    compare of string literal
  • But all the member function of String can also be
    used, for example
  • strlen(), substr()

24
Seems Clumsy?
  • Now have 2 files form.html and form.php
  • Actually they can be combined
  • Can you think how?
  • Try to think that the file can even post the form
    data to itself

25
Combine form2.php
  • The flow is that
  • First check whether user has input or not
  • If input, does what like form.php does
  • If not yet, does what like form.html does
  • Seems simple?

26
form2.php 1st part
  • lthtmlgt
  • lt?
  • if (!submit)
  • ?gt
  • ltform action"form2.php" method"post"gt
  • Name ltinput type"text" name"name"gtltbrgt
  • Class ltinput type"radio" name"class"
    value"1"gtCE1
  • ltinput type"radio" name"class" value"2"gtCE2
  • ltinput type"radio" name"class"
    value"3"gtCE3ltbrgtltbrgt
  • ltinput type"hidden" name"entered"gt
  • ltinput type"submit" namesubmit"gt ltinput
    type"reset"gt
  • lt/formgt
  • lt?

27
form2.php 2nd part
  • else
  • if (name"" class"")
  • echo "You have not entered enough..."
  • else
  • echo "Hi! name.ltbrgt"
  • switch(class)
  • case "1" echo "You're CE Part Iltbrgt" break
  • case "2" echo "You're CE Part IIltbrgt" break
  • case "3" echo "You're CE Part IIIltbrgt" break
  • default break
  • ?gt
  • lt/htmlgt

28
Form Processing
  • So now just need 1 file to do the MAGIC!
  • You can try more on your own

29
QA
  • Todays session is nearly finished
  • Any questions can ask me now
  • Thank you very much.
Write a Comment
User Comments (0)
About PowerShow.com