+ All Categories
Home > Documents > HTTP Communication HTTP Communication for Web Static Dynamic Dynamic Content PHP PHP Block ...

HTTP Communication HTTP Communication for Web Static Dynamic Dynamic Content PHP PHP Block ...

Date post: 02-Apr-2015
Category:
Upload: felicity-gager
View: 223 times
Download: 1 times
Share this document with a friend
Popular Tags:
51
ITS332
Transcript
Page 1: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

ITS332

Page 2: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Overview

HTTP Communication HTTP Communication for Web

Static Dynamic Dynamic Content

PHP PHP Block PHP Programming Style

Database MySQL

Page 3: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication

Page 4: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web

Static Web

• The contents of the web are fixed, hard coded in the markup language (HTML)

• (What you see is what you get).

• used to display fixed information which most often does not need to be updated on a regular basis

Dynamic Web

• The contents of the web are coded using client or server side scripting which requires special interpretation to produce interactive or dynamic elements in the page• search results• number of visitors• user login• message boards

Page 5: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web – Static

Page 6: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web – Static

Page 7: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web – Dynamic

Page 8: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTML + JavaScript

HTTP Communication for Web – Dynamic

Page 9: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTML + PHP

HTTP Communication for Web – Dynamic

Page 10: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTML + PHP

HTTP Communication for Web – Dynamic

Page 11: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web – Dynamic Content

Page 12: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

HTTP Communication for Web

Benefit of Dynamic Web Content

• Have ability to customize pages for any given user.• Greater ease of modification and update.• Extended functionality (login, transactions,

communication etc.)• Automating the site and allowing it to support itself.

Less pages to manually create.

Page 13: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

PHP

PHP's name is an recursive acronym for: Hypertext Preprocessor

PHP script is an HTML- embedded scriting language Designed to do something only after event occurs. e.g. when user submit forms

A Server-Side language the code resides on a host computer that serves Web pages

to the requesting visitors (client). PHP is easy to use for Web development because it has many

built-in functions that make web programming simpler, so that programmers can focus on the logic of programming without wasting precious development time

Page 14: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

PHP

client serverURL request

HTML

PHP

Script requestHTML

Page 15: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

PHP code skeleton:

PHP Block

<?php …. ?>

<? … ?>

<script language = “PHP”> … </script>

First style

Second style

Third style

Page 16: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

PHP Programming Style

Page 17: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Sending data to web browser

Use PHP built in functions Example

echo ‘Hello Student’; print “ How are you”;

Case-insensitive for function names ECHO, echo, Echo

Other print functions print_r, var_dump - value of variable print_f - formatting what you print

Page 18: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

White spaces - blank lines, tabs and extra spaces To alter spacing of finished web page, use

<br /> - line break <p></p> - paragraph

To alter spacing of HTML source from PHP, use echo() or print() over the course of several lines \n (newline character) within double quotation marks

White space, HTML and PHP

Page 19: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Writing comments

Important aspect to dynamic web site development Viewable in the source but not in the browser

window PHP supports 3 type of comments

# this is a comment // this is also a comment /* this is a larger comment that spans two line */

Page 20: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Variables

Rules of thumbVariable name must start with dollar sign ($)Combination of strings, numbers and the underscoreFirst character after dollar sign cannot be a numberCase sensitiveAssigned value using equals sign (=)

Page 21: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

String

A quoted chunk of letters, numbers, spaces, punctuation .. Example strings

‘hello’ ‘software’ ‘1000’ ’12 January, 2006’

String variable – assign a string value to valid variable name $today =’16 July, 2007’;

To print out echo $today; echo “Today is $today”;

Concatenation string Addition of strings using period (.).

$day=‘12’; $month=‘January’; $year =‘2006’; $today = $day . ’ ‘ . $month . ’ ‘ . $year;

Use it extensively when building database queries in later chapters

Page 22: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Numbers

Valid number-type variables can be 8 3.14 1098727272798 -4.2828282

Arithmetic operators + addition - subtraction * multiplication / division % modular ++ increment -- decrement

Page 23: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Numbers

Functions round()

$j = 3.14; $k = round( $j);

number_format() $p =20980; $g=number_format($p); $g=number_format($p,2);

Page 24: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Constant

Specific data type Retain initial value throughout script Cannot change once it has been set

Use define() define (‘AGE’, ‘value’);

Print constant echo ‘Hello, ‘ . AGE; OR echo ‘Hello,’, AGE;

Page 25: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Single quote -> values treated literally Double quote -> interpolated Example:

$var =‘Hello’;

echo “var equal to $var”; var equal to hello

echo ‘var equal to $var’; var equal to $var

echo “\$var is equal to $var”; $var is equal to hello

Single vs Double Quotation Marks

Page 26: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

“” replace variables name with its value and a special

character’s code (\$) with its represented value ‘’

display exactly what you type, except for the escaped single quote (\’) and the escape backslash(\\).

Single vs Double quotation marks

Page 27: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

PROGRAMMING WITH PHP

PROGRAMMING WITH PHP

Page 28: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Creating an HTML form Handling an HTML form Conditional and Operators Validating Form Data Sending Values to A Script Manually What are Arrays For and While Loops

Discuss on..

Page 29: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Managing HTML form with PHP involves 2 steps :

1. Step 1:Create HTML form with any text editor• HTML form (.htm/.html) is created using the HTML form tags and

various input types.

2. Step 2:Create PHP scripts that receives form data• PHP script (.php) is created to receives the submitted form data

and handle it.

HTML form

Page 30: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Handling HTML form Step 1: create html form

Step 2: create php scripts that receive form data

Page 31: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Step 1: create html form form.html

how dat ais sent(get or post)

which page the form data will be send

Page 32: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Step 2: create php script procesform.php

Page 33: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

If you have a text box in html form with name attribute age, PHP will store the text entered there in a variable called $age (registered global variable)

$age is similar to $_POST[‘age’] (superglobal variables)

eg : ( in HTML form)

eg : ( in PHP scripts)

echo “<p>Thank you, {$_POST[‘age’]} for the following comments”;

Superglobal variable

<p><b>Age: </b><input type ="text" name=“age" size="20" maxlength="40"/></p>

attributes

Page 34: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Dynamic web frequently use conditionals to alter script behavior

3 primary terms – if, else, elseif

Syntax:

if (condition) { // do something!}

if (condition) { // do something!} else { // do something else!}

Conditional and Operators

if (condition1) { // do something!} elseif (condition2) { // do something else!} else { // do something else!}

Page 35: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Condition can be TRUE for any number of reasons. Below are common TRUE conditions:

$var, if $var has a value other than 0, an empty string or NULL

isset($var), if $var has any value other than NULL, including 0 and an empty string.

TRUE

Conditional and Operators

Page 36: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Conditional and Operators

Comparative and Logical Operators

Symbol Meaning Type example

= Is assigned the value of Assignment $n=1

== Is equal to Comparison $x==$y

!= Is not equal to Comparison $x!=$y

< Less than Comparison $x < $y

> Greater than Comparison $x > $y

Page 37: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

switch Used in place of a long if–elseif–else conditional

Syntax :switch ($variable) {

case ‘value1’ :// do thisbreak;

case ‘value2’ :// do thisbreak;

default :// do thisbreak;

}

Conditional and Operators

Page 38: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Use of conditionals and any number of functions, operators and expressions. Common functions : empty(), strlen(), isset()

Validating Form Data

Page 39: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Why validate form?

• To ensure that something was entered or selected in form elements.

• To ensure that submitted data is of the right type (numeric, string, etc), of the right format (eg: email addr), or a specific acceptable value (eg: $gender is either M or F)

Aims of validating data

Page 40: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

2 ways to pass a PHP script variables and values

Use HTML hidden input type<input type=“hidden” name=”name” value=“Brian”/>- As long as this code is anywhere between the form tags, the variable

$name will have a value of ‘Brian’ in the handling PHP script

To append a value to the handling PHP scripts URLwww.smkbbb.edu.my/page.php?name=Brian- Emulate the get method of an HTML form

Sending Values to a script manually

Page 41: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

The $source variable

The page (handle_calculator.php) that handles the form (calculator.html) will receive a $source variable to indicate where the data (hidden input) came from.

The $source variable need also be validated; is set or not and if it is set with value, is the value correct or not.

Observe the action attribute in the form tag below :

appended!

Page 42: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

An array can hold multiple, separate pieces of information List of values Each value being a string or a number or even another array

Structured as a series of key-value pairs. Each item in the list, there is a key associate with it.

Support 2 kind of arrays Indexed – use numbers as the key Associative – use strings as key

What are Arrays?

Page 43: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

What are Arrays?

Example – indexed $artists

Example – associative $states

Key Value

0 Low

1 Aimee

2 Ani Difranco

3 Spiritualized

Key Value

MD Maryland

PA Penssylvania

IL Illinois

MO Missouri

Page 44: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

To retrieve a specific value from an array, refer to the array name, followed by the key, in square brackets echo $artists[2]; echo $states[‘MD’];

To print, wrap array name with curly braces echo “IL is the abbreviation for {$states[‘IL’]}.”;

What are Arrays?

Page 45: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Superglobal Arrays

Very useful and powerful tool By default, PHP uses several types of arrays

$_GET to store all of the variables and values sent via the get method $_POST to store all of the variables and values sent via the post method $_SESSION, $_COOKIE

Page 46: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Creating Arrays

Syntax to create arrays Add an element at a time

$array[] =‘value’; $array[] =‘value one’; $array[‘key’] =‘more values’;

Use array() function to build an entire array $array =array(‘key’ =>’value’, ‘key2’ => ‘value2’); $array =array(‘value’,’value2’,’value3’);

Create an array of sequential numbers, use range() $ten = range(1,10);

Page 47: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Accessing arrays

Access every array element, use foreach loop:Syntax :

foreach ($array as $value) echo $value ;

Iterate through every element in $array, assigning each element value to the $value variable.

To access both the keys and values Syntax : foreach ($array as $key => $value)

echo “The array value at $key is $value”;

Page 48: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Multidimensional arrays

An array consisting of other arrays Combine 2 arrays into 1 multidimensional array

$states = array (‘MD’ =>’MaryLand’, ‘IL’ =>’Illinois’); $provinces = array (‘QC’ =>’Quebec’, ‘AB’ =>’Alberta’); $abbr = array (‘US’ =>$states, ‘Canade =>$provinces’);

To access $states array $abbr[‘US’];

To access Maryland $abbr[‘US’][‘MD’]

To print out echo “The US state whose abbreviation is MD is {$abbr[‘US’][‘MD’]}.”;

Page 49: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Array and strings

2 functions for converting between strings and arrays $array = explode (separator, $string);

Turning a string into an array $string =‘Jan-Feb-Mac-Apr-May’; $array =explode (‘-’, $string);

$string = implode (glue, $array); Turning an array to string

$string =implode (‘ , ’, $array); $string= ‘Jan,Feb,Mac,Apr,May’;

Page 50: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

Sorting Arrays

Use built in PHP function sort() – sort an array by value discarding the original keys

The array keys will be reset after the sorting process Avoid use this function if key-value relationship is important.

asort() – sort an array by value while maintaining the key ksort() – sort an array by key Each of these can sort reverse order by changing them to

rsort() , arsort() , krsort() shuffle() – to randomize the order of an array

Page 51: HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.

For and While Loops

While loop If the condition is still true, the loop will be executed

Syntax :while (condition) { // do something

} For loop first the initial expression is run, checked the condition and if true,

content of the loop is executed. After execution, checked closing expression with condition again.Syntax :

for (initial expression; condition; closing expression) { // do something

}


Recommended