Professional Documents
Culture Documents
A 4 days Course
Presented by
Shaizar Md Daud @ Jat
Course Outline
Introduction to HTML
Introduction to PHP
PHP - The Language
Using Variables
Giving Your Code Life – Flow Control
Using Functions
Creating Custom Functions
Making Things Constant
Variable Scope – Now You See Me, Now You Don’t!
Introduction to Database & MySQL
Using MySQL from PHP
Putting things Together – Lab Project
Single Tags
– No Content or Child Tags
Syntax:
<tagname/> or <tagname>
Example:
<br/>, <br>, <hr>
Syntax:
<tagname>Some Content</tagname>
Example:
<p>The quick brown fox jumps over the lazy
dog</p>
Syntax:
<!-- comment text -->
Example:
<!--
This is a HTML comment
-->
Document Type
Declaration
HTML Container
– HTML Header
– HTML Body
Common DOCTYPEs:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
Syntax:
<ol type=“[ 1 | I | i | A | a ]”>
[ list items ]</ol>
Example:
<ol>
<li>Item 1</li>
<li>Item 2</li>
</ol>
Syntax:
<UL type=“[ DISC | CIRCLE | SQUARE ]”>
[ list items ]</UL>
Example:
<UL>
<li>Item 1</li>
<li>Item 2</li>
</UL>
Syntax:
<FORM action=“<url>” method=“[ GET | POST ]”>
Example:
<FORM action=“register.php” method=“post”>
. . .
. . .
</FORM>
Example:
<INPUT type=“text” name=“fullname” size=“50”/>
<INPUT type=“password” name=“pass” size=“50”/>
<INPUT type=“radio” name=“sex” value=“m”/> Male
<INPUT type=“radio” name=“sex” size=“f”/> Female
<INPUT type=“checkbox” name=“class” size=“php_b”/>
<INPUT type=“checkbox” name=“class” size=“php_i”/>
<INPUT type=“checkbox” name=“active” size=“1”/> Yes
<INPUT type=“submit” name=“cmdSave” value=“Save”/>
Attributes
– name
– size
– multiple
Example:
<SELECT name=“addr_state” size=“1”>
<OPTION value=“sel” selected>Selangor</OPTION>
<OPTION value=“joh”>Johor</OPTION>
<OPTION value=“ked”>Kedah</OPTION>
</SELECT>
Example:
<SELECT name=“avail_state” size=“10” multiple>
<OPTION value=“sel” selected>Selangor</OPTION>
<OPTION value=“joh”>Johor</OPTION>
<OPTION value=“ked” selected>Kedah</OPTION>
</SELECT>
Attributes
– rows
– cols
– wrap: [ soft | hard | off ]
CDATA
– Text to be displayed in the form field.
Example:
<TEXTAREA name=“notes” rows=“10” cols=“40”>
Text displayed in TextArea form field.
It supports multiple text lines.
</TEXTAREA>
Using Variables
Giving Your Code Life – Flow Control
Array
Object
Examples
– $my_string = “My String”;
– $my_numbers = 123;
– $fn_return_value = my_function();
Numeric indexes
Examples
– $index_array[0] = “Item 1”;
– $index_array[1] = “Item 2”;
– $index_array[2] = “Item 3”;
Index starts from 0
$_GET[<field_name>]
$_POST[<field_name>]
$_REQUEST[<field_name>]
Conditional Statements
Loops
Syntax:
if ( <condition> ) {
[ block to be executed if <condition> is true ]
}
Syntax:
if ( <condition> ) {
[ block to be executed if <condition> is true ]
}
else {
[ block to be executed if <condition> is false ]
}
Syntax:
if ( <condition1> ) {
[ block to be executed if <condition1> is true ]
}
else if ( <condition2> ) {
[ block to be executed if <condition2> is true ]
}
Syntax:
if ( <condition1> ) {
[ block to be executed if <condition1> is true ]
if ( <condition2> ) {
[ block to be executed if
<condition2> is true ]
}
[ continue ]
}
WHILE loop
FOR loop
FOREACH loop
Syntax:
while ( <condition> ) {
[ block to be executed
while <condition> is true ]
}
Example:
$i = 0;
while( $i < sizeof( $profile ) ) {
print “Item $i: “
. $profile[ $i ]
. ‘<br />’;
$i++;
}
Syntax:
for ( <init>; <condition>; <iterator> ) {
[ block to be executed
while <condition> is true ]
}
Example:
for ( $i = 0; $i < sizeof( $profile ); $i++ ) {
print “Item $i: “
. $profile[ $i ]
. ‘<br />’;
}
Syntax 1:
foreach ( <array> as <var_value> ) {
[ block to be executed for each item in array ]
}
Syntax 2:
foreach ( <array> as <var_key> => <var_value> ) {
[ block to be executed for each item in array ]
}
Example 1:
foreach ( $profile as $item ) {
print “Item: “
. $item
. ‘<br />’;
}
Example 2:
foreach ( $profile as $key => $item ) {
print “Item “ . $key . “: “
. $item
. ‘<br />’;
}
Using Functions
Creating Custom Functions
Making Things Constant
Variable Scope – Now You See Me, Now
You Don’t!
Syntax:
function_name([<param>])
Example:
echo date();
echo date( ‘d-m-Y’ );
$a = sprintf( ‘hello’ );
Syntax:
function function_name() {
}
function function_name($param1, $param2=2) {
return $rv;
}
Global Scope
– Accessible throughout script
Local Scope
– Accessible within a function
GLOBAL keyword
– Access global variables within a function
Example:
$cfgHost = ‘localhost’;
function getHostName() {
global $cfgHost;
$label = ‘The hostname is: ‘; // local scope.
echo $label . $cfgHost;
}
include([<path>]<filename>);
include_once([<path>]<filename>);
require([<path>]<filename>);
require_once([<path>]<filename>);
Tables
db1.table_2 db1.table_4
Syntax:
CREATE DATABASE dbname;
Example:
CREATE DATABASE training;
Syntax:
USE dbname;
Example:
USE training;
Syntax:
CREATE TABLE table_name (
field1_name field_spec,
field2_name field_spec
);
Example:
CREATE TABLE student (
st_id bigint primary key auto_increment,
st_fname varchar(100),
st_age tinyint unsigned
);
9 - 12 April 2007 Trainer: Shaizar Md Daud (jat@shaiz
ar.com)
Querying a Table
Syntax:
SELECT <field_list> FROM <table_name>
[ WHERE <condition> ];
Example:
SELECT * FROM student;
SELECT * FROM student WHERE st_age > 20;
Syntax:
INSERT INTO `table_name` (<fieldlist>) VALUES
(<valuelist>};
Example:
INSERT INTO student (st_fname, st_age) VALUES
( ‘Ahmad Albab’, 20 );
Syntax:
UPDATE table_name SET <field value list>
[ WHERE <condition> ];
Example:
UPDATE student
SET
st_fname = ‘Jat’,
st_age = 22
WHERE st_id = 1;
Syntax:
DELETE FROM table_name [ WHERE <condition> ];
Example:
DELETE FROM student WHERE st_id = 2;
DELETE FROM student WHERE st_age < 18;
Syntax:
resource mysql_connect (
[string server
[, string username
[, string password
[, bool new_link
[, int client_flags]]]]]);
Example:
$dbh = mysql_connect( ‘localhost’, ‘root’, ‘’ );
Syntax:
bool mysql_select_db (
string database_name
[, resource link_identifier] )
Example:
$stat = mysql_select_db( ‘training’, $dbh );
Syntax:
resource mysql_query (
string query
[, resource link_identifier] )
Example:
$rh = mysql_query( ‘SELECT * FROM student’, $dbh );
Syntax:
array mysql_fetch_array (
resource result
[, int result_type] )
Example:
$row = mysql_fetch_array( $rh );
Syntax:
int mysql_insert_id(
[resource link_identifier] )
Example:
$newId = mysql_insert_id( $dbh );
Syntax:
string mysql_error( [resource link_identifier] )
Example:
$errMsg = mysql_error( $dbh );
Lab Project