ADVM420- Class #1 Web Design with PHP and MySQL Introduction to PHP © Copyright 2007 Grant Macewan...

30
ADVM420- Class #1 Web Design with PHP and MySQL Introduction to PHP © Copyright 2007 Grant Macewan College http://liftedu.wordpress.com Micah Slavens - [email protected]

Transcript of ADVM420- Class #1 Web Design with PHP and MySQL Introduction to PHP © Copyright 2007 Grant Macewan...

ADVM420- Class #1Web Design with PHP

and MySQL

Introduction to PHP

© Copyright 2007 Grant Macewan Collegehttp://liftedu.wordpress.com

Micah Slavens - [email protected]

Introduction Course Title

Web design with PHP and MySQL Objectives

Build dynamic, data-driven websites Gain the skills to learn and move forward

Target Audience Some knowledge of HTML, databases, & programming is

recommended Facilities

Washrooms, cafeteria, parking Introductions and Interests

What brought you to this course?

Target Environments

Client Machine requirements: Text Editor FTP

Server requirements: Can be Windows/Mac/Linux based Download & install appropriate software See reference

Installing PHP & MySQL on http://liftedu.wordpress.com/resources/

Agenda Day 1:

Web development concepts Tools of the trade HTML review Introduction to Scripting with PHP Include files HTML Forms & Validation

Day 2: Listing data from MySQL Adding data to MySQL Creating Web applications Wrap-up

Web Development Concepts

What is a Web Server? What is a Web Client? Static Web Pages

HTML only Dynamic Web Pages

HTML with Scripting language

Dynamic Web Pages

Client Side AJAX – (Asynchronus Javascript & XML) DOM Scripting Flash

Server Side Pre-processor that generates HTML Numerous scripting languages available

PHP, Python, Ruby, ASP, ColdFusion, JSP, Perl, etc…

Server-Side Technology Advantages

Ability to store data long term Code is protected

Code logic is only visible through FTP (login protected) Potentially faster download times

Code does not travel to client - public only sees results

Disadvantages More load on server Server must support technology Less portability More knowledge required

Development Strategies

Develop locally / upload to server Does not require permanent connection Can use your own resources (but you have

to manage them) Does not interfere with existing site More control if things go wrong…

Develop directly on a server via FTP You’ll be sure it works Easier in a team environement

Tools of the Trade

We will be developing code on our server using a text Editor

There are tools that will write blocks of code for you, but a text editor will give you a greater understanding of what you’re doing

We won’t look at installing PHP or MySQL in class, but you’re welcome to give it a try on your own computer at home

XHTML Review Using Text Editor, create

An index.html page 2 directories, called:

/images /documents

Heading Image (linked to in image directory) An unordered list

Links to your 3 favorite sites An ordered list

Steps to making a pb & j sandwich Save View

Web Standards W3C

Standards make a better internet A usable standards based site will have a greater

return No tables based designs! Semantic markup is beautiful

Use only XHTML elements Make use of all the elements you can

Clean markup and CSS will make a lighter, more usable application

Basic PHP Rules

PHP script files end with the .php extension Scripting statements are enclosed in tags:

<?PHPprint (date("M d, Y"));

?> More simply:

<? print (date("M d, Y")); ?>Or<?= date("M d, Y") ?>

Try it! Rename your file index.php and include the lines

shown above to show the server’s date

Data types in PHP

PHP is four simple data types: Number (1, -5, 22.35, 1000000) String (“My dog”, “123 Any Street”) Boolean (true/false) Null

Compound data types: Array $age[‘Bill’] = 15; $car[1] = “Ford”; Object

Variables and Statements

Statements end with the semi-colon ; Capitalization is important! Variable names always prefixed with $ Comments begin with //

$x = 15 / 9; //line1 comment

$y = (65 * $x) % 15; //line2 comment

PHP Operators

Math operators: + - * / % (Remainder)

String operator: . (Concatenation)

Comparison operators && AND || OR ! NOT

Program Output

As we have seen, all output to the browser window is done using the print() or echo() functions (you can include html):

<?PHP

$x = 15 / 9; //comment

$y = (65 * $x) % 15;

print(“<h2>The answer is: $y </h2>”);

?>

Self-test: Simple Statements

Write a program in PHP that calculates and displays a 10% discount on $199.99.

Ensure all output has labels and make it appear on separate lines.

Program Blocks

Program blocks are always contained in braces {…}

if () {…} else {…}

switch() {…}

while() {…}

do {…} while()

for(;;) {…}

function xyz() {…}

Decisions: if() {…} else {…}

Syntax:if (condition) {

true_statements;}else {

false_statements;}

Example:if ($x >= 10) {

$x = $x * 0.9; //discount 10%}else {

$x = $x * 0.95; //discount 5%}

Self-test: if() {…} else {…}

Write a program in PHP that prints the words:

“infant” if the variable $age is less than 1 “toddler” if the variable $age is less than 2 “child” if the variable $age is less than 13 “teen” if the variable $age is less than 18 “adult” otherwise

Decisions: switch () {…}

Syntax:switch (value) {

case N: statements; break; … default:

statements; break;}

Example:switch ($month) {

case 1: print (“Jan”); break;

case 2: print (“Feb”);

break;default:

print (“Err”);break;

}

Self-test: switch () {…}

Write a program in PHP that displays: “BMW200” if the variable $car is 200 “BMW203” if the variable $car is 203 “BMW205” if the variable $car is 205 “Error” otherwise

Loops: while() {…}

Syntax:while (condition) { statements;}

Example: What does this program do?$i = 0;$s = 0;while ($i < 7) { $s = $s + $i; $i = $i + 1;}print(“s= $s <br>\n”);

Self-test: while() {…}

Powers of Two Write a program that displays the series

2,4,8,16,32,64… until the number becomes greater than 5000.

Loops: do {…} while()

Syntax:do { statements;} while (condition);

Example: What does this program do?$i = 0;do { print(“i=“ . $i . “<br>\n”);} while ($i != 0);

do {…} while() always runs at least once.

Loops: for(;;) {…}

Syntax:for (initialize; condition; increment) { statements;}

Example: What does this program do?$f = 1;for ($i=1; $i<=5; $i++) { $f = $f * $i;}echo(“f=“ . $f . “<br>\n”);

Self-test: for(;;) {…}

Countdown Write a program that displays 10,9,8,…1

Blast Off! using a for loop.

function xyz() {…}

Example:function xyz($arg1,$arg2,$arg3) {

$answer = ($arg1+$arg2) / $arg3;

return ($answer);

}

//calling the function…

$r = xyz(1,2,3);

print($r);

Self-test: function xyz() {…}

weekdayName() Write a function returns the name of the week

depending on the argument passed, for example: If 0 is passed, print “Sunday” If 1 is passed, print “Monday” … If 6 is passed, print “Saturday”

Call this function with each day of the week and display the return value.

Summary

This Module summarizes most of PHP ’s main elements: Check out some code Read blogs and forums Practice and experiment