Advanced Web 2012

cornawakeΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

89 εμφανίσεις

Advanced
Web

2012

Lecture 4

Sean Costain 2012

PHP

Sean Costain 2012

What is PHP?

PHP is a widely
-
used general
-
purpose scripting language that is especially suited
for Web development and can be embedded into HTML.

Ref:http
://www.php.net/

What's
it do?

It is also helpful to think of PHP in terms of what it can do for you. PHP will allow
you to:


Reduce the time to create large websites.


Create a customized user experience for visitors based on information that
you have gathered from them.


Open up thousands of possibilities for online tools.


Allow
creation of shopping carts for e
-
commerce websites


How to Start

Sean Costain 2012

Files extension


.
php


Index.php


Gallery.php

In the Code


<?
php

code goes here ?>

Advantage of :


Decisions can be decided in code


Eg

if the user selects between different colours, each choice could create a
differing outcome.


Complex code written in PHP is never shown to the end user, all code is created
on the server and the end user just gets plain HTML


Allows for the connection to a database

Hello World!

Sean Costain 2012

HTML

PHP

Both codes shown produce the
same output, but if you view
source, you will only see the html.

Commenting

Sean Costain 2012

2 ways of doing this

// <
-

this is a single line comment


/* */ <
-

Anything between the *’s is commented, multi line comment

Variables

Sean Costain 2012

A variable is a reserved memory location to store information.

-
Think of it as a bucket.

-
PHP variables always start with a $

-
PHP variables aren’t strict with a specific
datatype

Displaying the contents of
php

you can use:


Echo


print

How to use Echo

Sean Costain 2012

Echo is one of the main ways to get output to the screen. This is how you can
use it by itself and with multiple variables

If you are displaying text with a single quote ‘ you need to break it with a
\

If … then

Sean Costain 2012

If…then is used for determining choices.


if the user enters in the right name, display the name otherwise, generic welcome


If the number chosen is greater than 10 then apply a discount to the price

Operators

Sean Costain 2012

We can use maths operators and logic to calculate for loops


<

less than


>

greater than


<=

less than or equal to


>=

greater than or equal to


==

equals


!=

not equals


&&

and


||

or



Don’t confuse = as equals, this is the assign a value command.

In programming we use ==


If … then … else

Sean Costain 2012

If…then … else is used for multiple choices.


if the user enters in the right name, display the name otherwise, generic welcome


If the number chosen is greater than 10 then apply a discount to the price and
display this information, otherwise just show the price

Switch

Sean Costain 2012

Switch is like an advanced if then else statement,
used for multiple conditions.



What if we had different levels of discount, > 10
is 10% >20 is 20% >30 is 30%...

Loopy Loops!!

Sean Costain 2012

When it comes to programming we have loops, a loop is a piece of code that runs
until a certain condition is met.


This is normally something like a certain number or until the system has finished
reading the data in a table.

Loops come in different types we have


For loops


While loops


Do While loops <
-

Exists but not always used

For Loop

Sean Costain 2012

The for loop is a counting loop. Repeat X until Y is matched.


Say we need to display the numbers 0 through to 10

This would work, but tedious

This is cleaner and works

Pretty much Optimal

Now Why? What happens if it’s
not 10, but 100 or 1,000 or
10,000 lines you need to display,
this is the power of a loop.

Write once, execute multiple
times

While Loop

Sean Costain 2012

A while loop is similar but doesn’t always have a defined end point. While loops are
used a lot in database retrieval as the end of the table is an unknown, but it can be
used for defined endpoints. Same example as before, display numbers to 10.

Displaying numbers up to 10

Loops until the end of the table

Forms

Sean Costain 2012

By using standard html forms, we can pass the data entered in to a
php

file that
can then do something with the information.

To read this data, you would have the following:

PHP and MySQL

Sean Costain 2012

MySQL is currently the most popular open source database server in existence. On top
of that, it is very commonly used in conjunction with PHP scripts to create powerful
and dynamic server
-
side applications.

When using PHP and MySQL, the first thing to do is to have a database built, in this
course, the database will be constructed in
phpMyAdmin
.

After the database is created, then you can embed MySQL
into
php

commands to perform all of the operations
needed for the website.

C.R.U.D

Sean Costain 2012

CRUD


IT Loves Acronyms, this deals with databases


C


Create


As in create data in a database table


R


Read


As in retrieve and display the data from a database table


U


Update


As in Edit the already stored information in a database table


D


Delete


As in delete the data in a database table


But first…. Connecting to the database


Whenever you need to access to the data stored in a database, there needs to be
a connection established between the page and the database. This can be done
in a couple of different ways.

Connecting to a Database

Sean Costain 2012

This could be done on every single page, but will get tedious

This is my preferred method, with the function being stored in a separate
file and then imported into the page being used.

On each page, you would then call
dbLink
();

Create

Sean Costain 2012

Creating new data into a database table

The breakdown of the above line:


A
php

variable is storing the entire
MySql

command


INSERT into <table name> <
-

When coding MySQL, capitalize the command


<table name>(table field, table field, table field, table field)


VALUE <
-

store in the listed fields…


(NULL, $variable, $variable, $
variable,$variable
)


NULL is a non element, use this on primary key/auto increment fields.

Read

Sean Costain 2012

Reading data from a database table

The breakdown of the above lines:


A
php

variable is storing the entire
MySql

command


While it is not the end of the table


SELECT everything (*) from the <table> (Users)


Put the table field data into the following variables


Then display the name of the person

Update

Sean Costain 2012

Updating data from a database table

The breakdown of the above lines:


A
php

variable is storing the entire
MySql

command


UPDATE <table> where the table field equals a variable the user filled
out and where the table’s
uid

is the same as the user filled out
uid
.


Once done, store the result of the commend in a variable

Delete

Sean Costain 2012

Deleting data from a database table

The breakdown of the above lines:


A
php

variable is storing the entire
MySql

command


DELETE from <table> where the table table’s
uid

is the same as the
user filled out
uid
.


Once done, store the result of the commend in a variable