website series 6 - php

93
PHP 5/14 - Website Series 6 [email protected]

Upload: eugene-yang

Post on 23-Feb-2017

264 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Website Series 6 - PHP

PHP5/14 - Website Series 6

[email protected]

Page 2: Website Series 6 - PHP

Finally, it’s the last one!

Page 3: Website Series 6 - PHP

Concept Passively receive request from user and give result back to

user. Data! Security! Sometimes we use back-end to hard-loading computation.

Page 4: Website Series 6 - PHP

HTML

CSS

JSSQL

PHP

Page 5: Website Series 6 - PHP

HTML

CSS

JSSQL

PHP

Page 6: Website Series 6 - PHP

Synchronous Request(同步呼叫 )

Page 7: Website Series 6 - PHP

Request

Synchronous Request(同步呼叫 )

Page 8: Website Series 6 - PHP

Request Receive

Synchronous Request(同步呼叫 )

Page 9: Website Series 6 - PHP

Request Receive

Select data

Synchronous Request(同步呼叫 )

Page 10: Website Series 6 - PHP

Request Receive

Select data Receive data

Synchronous Request(同步呼叫 )

Page 11: Website Series 6 - PHP

Request Receive

Select data Receive data

Put data into original HTML

Synchronous Request(同步呼叫 )

Page 12: Website Series 6 - PHP

Request Receive

Select data Receive data

Put data into original HTML

HTML + Data

Synchronous Request(同步呼叫 )

Page 13: Website Series 6 - PHP

Request Receive

Select data Receive data

Put data into original HTML

HTML + Data

Synchronous Request(同步呼叫 )

Page 14: Website Series 6 - PHP

Asynchronous Request(非同步呼叫 )

Page 15: Website Series 6 - PHP

Request

Asynchronous Request(非同步呼叫 )

Page 16: Website Series 6 - PHP

Request Receive

Asynchronous Request(非同步呼叫 )

Page 17: Website Series 6 - PHP

Request Receive

HTML +JS

Asynchronous Request(非同步呼叫 )

Page 18: Website Series 6 - PHP

Request Receive

HTML +JS

Asynchronous Request(非同步呼叫 )

Page 19: Website Series 6 - PHP

Request Receive

HTML +JS

JS: Request for data

Asynchronous Request(非同步呼叫 )

Page 20: Website Series 6 - PHP

Request Receive

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

Page 21: Website Series 6 - PHP

Request Receive

Select data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

Page 22: Website Series 6 - PHP

Request Receive

Select data Receive data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

Page 23: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

Page 24: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Data

Asynchronous Request(非同步呼叫 )

Page 25: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Data

Asynchronous Request(非同步呼叫 )

Page 26: Website Series 6 - PHP

We need to make your PC become a server

Page 27: Website Series 6 - PHP

Basic Syntax

Page 28: Website Series 6 - PHP

Basic SyntaxWrap all the code

Page 29: Website Series 6 - PHP

Basic SyntaxWrap all the code

Page 30: Website Series 6 - PHP

Basic SyntaxWrap all the code

Variables need to have prefix ‘$’

Page 31: Website Series 6 - PHP

Basic SyntaxWrap all the code

Different from JS

Variables need to have prefix ‘$’

Page 32: Website Series 6 - PHP

Basic SyntaxWrap all the code

Different from JS

Variables need to have prefix ‘$’

Don’t need to declare

Page 33: Website Series 6 - PHP

Basic SyntaxWrap all the code

Different from JS

Variables need to have prefix ‘$’

Don’t need to declare

Like JS, we don’t need to specified variable type, but we still have types!

Page 34: Website Series 6 - PHP

Basic Syntax (conti.) Print out things

echo ‘hello world!’;echo $myVar;

Variable Handleisset($var)unset($var)

Array operationarray()array_pop($arr)array_push($arr,’’)print_r($arr)array_slice($arr,2,4)

Page 35: Website Series 6 - PHP

Array

Page 36: Website Series 6 - PHP

Array

Page 37: Website Series 6 - PHP

Array

Ordinary array

Page 38: Website Series 6 - PHP

Array

Ordinary array

Page 39: Website Series 6 - PHP

Array

Ordinary arrayAssociate array

Page 40: Website Series 6 - PHP

Variables

Page 41: Website Series 6 - PHP

Variables

Page 42: Website Series 6 - PHP

Variables We don’t have local or global variables. More like the black window when we write C, variables clear out

after program finish running.

We have some Predefined Variables.

Page 43: Website Series 6 - PHP

Predefined Variables Server system variables( check out phpinfo(); )

$_SERVER Variable that every user can have their own space

$_SESSION

Variables from get request$_GET

Variables from post request$_POST

Page 44: Website Series 6 - PHP

Predefined Variables Server system variables( check out phpinfo(); )

$_SERVER Variable that every user can have their own space

$_SESSION

Variables from get request$_GET

Variables from post request$_POST

Page 45: Website Series 6 - PHP

Example PHP embedded with HTML Not the kind we are going to do, just for demonstration.

@php_example_1.php

Page 46: Website Series 6 - PHP

SQL

PHP

Front-end

Page 47: Website Series 6 - PHP

Connection From Front-end We called it Http Request Two ordinary type: GET, POST This is a synchronous request example.

@php_example_2.php

Page 48: Website Series 6 - PHP

Connection to MySQL It is all the same as what we do on console. Still need to login and set the database we what to use.

There are many ways to connect database, but we use the most popular and secure way.

We use PDO (PHP Data Object).

Page 49: Website Series 6 - PHP

Initialize PDO Connection

Page 50: Website Series 6 - PHP

Initialize PDO Connection

MySQL server host, username, password and the database we are going to use

Page 51: Website Series 6 - PHP

Initialize PDO Connection

MySQL server host, username, password and the database we are going to use

Start MySQL connection

Page 52: Website Series 6 - PHP

Simple Query

Page 53: Website Series 6 - PHP

Simple Query

Set the array type of the result

Page 54: Website Series 6 - PHP

Simple Query

Set the array type of the result

Get all the record we selected

Page 55: Website Series 6 - PHP

Query with Parameters

Page 56: Website Series 6 - PHP

Query with Parameters

Use array to bind parameters with order

Page 57: Website Series 6 - PHP

Query with Parameters

Use array to bind parameters with order

Get the primary key of the record we just inserted

Page 58: Website Series 6 - PHP

Example@php_example_3.php

Page 59: Website Series 6 - PHP

OK!We are almost there!

Page 60: Website Series 6 - PHP

Let’s back to JS~lol

Page 61: Website Series 6 - PHP

jQuery

Utilities

DOM

Events

Ajax

Page 62: Website Series 6 - PHP

jQuery

Utilities

DOM

Events

Ajax

Page 63: Website Series 6 - PHP

Request Receive

HTML +JS

Asynchronous Request(非同步呼叫 )

Page 64: Website Series 6 - PHP

Request Receive

HTML +JS

JS: Request for data

Asynchronous Request(非同步呼叫 )

Page 65: Website Series 6 - PHP

Request Receive

HTML +JS

JS: Request for data

Asynchronous Request(非同步呼叫 )

API calling

Page 66: Website Series 6 - PHP

Request Receive

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

API calling

Page 67: Website Series 6 - PHP

Request Receive

Select data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

API calling

Page 68: Website Series 6 - PHP

Request Receive

Select data Receive data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

API calling

Page 69: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Asynchronous Request(非同步呼叫 )

API calling

Page 70: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Data

Asynchronous Request(非同步呼叫 )

API calling

Page 71: Website Series 6 - PHP

Request Receive

Select data Receive data

PHP: Collect and orignize data

HTML +JS

JS: Request for data Receive

Data

Asynchronous Request(非同步呼叫 )

API calling

Page 72: Website Series 6 - PHP

RESTful APIOperation SQL HTTP

Create INSERT POSTRead (Retrieve) SELECT GETUpdate (Modify) UPDATE PUTDelete (Destroy) DELETE DELETE

資源 GET PUT POST DELETEhttp://

example.com/resources/

取得整組資源 替換整組資源 增加一筆資源 刪除整組資源。http://

example.com/resources/142

取得特定一筆資源 替換特定一筆資源 在特定一筆資源中增加一個元素 (屬性 ) 刪除特定一筆資源。

Page 73: Website Series 6 - PHP

Data Type Full HTML String Number XML JSON

Page 74: Website Series 6 - PHP

Data Type (conti.)XML

<course><id>1</id><name>Web Programming</name><teacher>Turtle</teacher></course><course><id>2</id><name>QF Computer Programming</name><teacher>Han</teacher></course>

JSON[{id: 1,name: ‘Web Programming’,teacher: ‘Turtle’},{id: 2,name: ‘QF Computer Programming’,teacher: ‘Han’}]

Page 75: Website Series 6 - PHP

Data Type (conti.)XML

<course><id>1</id><name>Web Programming</name><teacher>Turtle</teacher></course><course><id>2</id><name>QF Computer Programming</name><teacher>Han</teacher></course>

JSON[{id: 1,name: ‘Web Programming’,teacher: ‘Turtle’},{id: 2,name: ‘QF Computer Programming’,teacher: ‘Han’}]

Win

Page 76: Website Series 6 - PHP

User

MVC

Page 77: Website Series 6 - PHP

User

Model

MVC

Page 78: Website Series 6 - PHP

User

Model

View

MVC

Page 79: Website Series 6 - PHP

User

Controller

Model

View

MVC

Page 80: Website Series 6 - PHP

User

Controller

Model

View

MVC

Page 81: Website Series 6 - PHP

User

Controller

Model

View

MVC

Page 82: Website Series 6 - PHP

User

Controller

Model

View

Uses

MVC

Page 83: Website Series 6 - PHP

User

Controller

Model

View

Uses

Manipulates

MVC

Page 84: Website Series 6 - PHP

User

Controller

Model

View

Uses

ManipulatesUpdates

MVC

Page 85: Website Series 6 - PHP

User

Controller

Model

View

Uses

ManipulatesUpdates

Sees

MVC

Page 86: Website Series 6 - PHP

User

Controller

Model

View

Uses

ManipulatesUpdates

Sees

MVC

Page 87: Website Series 6 - PHP

User

Controller

Model

View

Uses

ManipulatesUpdates

Sees

MVC

Back-end

Page 88: Website Series 6 - PHP

User

Controller

Model

View

Uses

ManipulatesUpdates

Sees

Back-endFront-end

MVC

Page 89: Website Series 6 - PHP

User

Controller

Model

View

UsesSees

Back-endFront-end

MVC

Page 90: Website Series 6 - PHP

User

Controller

Model

View

UsesSees

Back-endFront-end Call APIs

MVC

Page 91: Website Series 6 - PHP

User

Controller

Model

View

UsesSees

Back-endFront-endReturn Call APIs

MVC

Page 92: Website Series 6 - PHP

Last Example

Page 93: Website Series 6 - PHP

And I have to write it now!