2012 | PHP by Jochem

Preview:

Citation preview

PHP & MYSQLThe basics

Thursday, November 15, 12

purpose of this course

You can only master web-development by doing it a lot

This course enables you to start doing it (a lot)

Learn the language

Walk through the possibilities

Know the (online) references for the future

Thursday, November 15, 12

end of this week

?Thursday, November 15, 12

planning

Day 1: PHP

Day 2: CodeIgniter

Day 3: MySQL

Day 4: Form Handling & AJAX

Day 5: Integrating (login system)

Thursday, November 15, 12

today

Overview web-development

PHP

Thursday, November 15, 12

OVERVIEW WEB-DEVELOPMENTWhat does it take to build a website

Thursday, November 15, 12

Browser / Frontend

HTML

CSS

Java-script

Content

Style

Behavior

Thursday, November 15, 12

Server / Backend

PHP

database

Thursday, November 15, 12

Front-end & Back-endFrontend

HTML CSS Java-script

Backend

PHP database

generate

Thursday, November 15, 12

PHPScripting

Thursday, November 15, 12

PHP Hypertext Preprocessing

Executed on server

Communication with database

Handle data

Parsing (parts of) HTML

Thursday, November 15, 12

PHP file

write in text-editor

.php

HTML & PHP code

Executed on request by browser

Return HTML to browser

Thursday, November 15, 12

Syntax

<?php

the php code..;

?>

localhost/day1/hello.php

Thursday, November 15, 12

Commenting

One line comment: //...

Multiple line comment: /* ... */

Thursday, November 15, 12

Variables

Store a value in a variable

Use/change the value later on

PHP var starts with $

Name begins with letter or underscore

Name may contain A-z, 0-9, _

Case sensitive

Thursday, November 15, 12

year.php

Thursday, November 15, 12

Main varsint

number

$x = 25;

$y = 25.5;

string

text

$x = “Hello”;

combine with .

$y = “world”;

$z = $x . $y;

array

list of values

$x = array(1,24,12);

to be continued..

Thursday, November 15, 12

Single & double quotes

Thursday, November 15, 12

Exercise

......

Use double quotes for $sentence 1

Use single quotes for $sentence 2

Thursday, November 15, 12

Arithmetic operators

Thursday, November 15, 12

Shortcuts

Thursday, November 15, 12

If

Thursday, November 15, 12

Else

Thursday, November 15, 12

Elseif

Thursday, November 15, 12

Exercise

Thursday, November 15, 12

Logical operators

Thursday, November 15, 12

Exercise

find largest

use AND or &&

indicate if no largest

Thursday, November 15, 12

Arrays

List of values

Stored in 1 variable

Each value has its own index

Thursday, November 15, 12

Numeric Array

Each index is a number

Thursday, November 15, 12

Associative Array

Each index is a word

Thursday, November 15, 12

For loop

Run block of code several times

for(init; condition; increment){

}

optional: stop with break;

Thursday, November 15, 12

Loop over Array

Thursday, November 15, 12

Foreach

Thursday, November 15, 12

Exercise

Create 1 array with your family members

Find a way to store relation and name

Echo per member “I’ve got a relation named name”

So, “I’ve got a brother named Maarten”

Thursday, November 15, 12

Multidimensional Arrays

Array in array

See multidimensional.php

Thursday, November 15, 12

Function

700+ predefined functions

Self-made functions

Thursday, November 15, 12

Create your own function

Thursday, November 15, 12

Function parameters

Thursday, November 15, 12

Default parameters

Thursday, November 15, 12

Exercise

Create calculating function

3 parameter: 2 numbers and the operator (text)

Function returns result

Example: calc(2,3,‘multiply’); //returns 6

Hint: use switch()

Thursday, November 15, 12

Form Handling

PHP can handle the data of an HTML form

Data is communicated in a array named $_POST

See post.php

Thursday, November 15, 12

CODEIGNITERPHP framework

Thursday, November 15, 12

PHP Framework

Set of folders & PHP files

Predefined structure

Many predefined functions

Thursday, November 15, 12

Model View Controller

Separate Lay-out, Logic & Database traffic

Lay-out / view: HTML

Logic / controller: PHP

Database / model: MySQL

Controller

Model View

Request Answer

Thursday, November 15, 12

Model View Controller

Separate Lay-out, Logic & Database traffic

Lay-out / view: HTML

Logic / controller: PHP

Database / model: MySQL

Controller

Model View

Request Answer

database

Thursday, November 15, 12

Sales TextsProductsUsersModels

Product BuyCatalogHomeControllers History

ProductBuyCatalogHomeViews History

Thursday, November 15, 12

Sales TextsProductsUsersModels

Product BuyCatalogHomeControllers History

BuyCatalogHomeViews HistoryProduct

x5

Thursday, November 15, 12

Sales TextsProductsUsersModels

Product BuyCatalogHomeControllers History

BuyCatalogHomeViews HistoryProduct

x1

Thursday, November 15, 12

Install

www.codeigniter.com

download

unzip

place content in folder day2 (in www or htdocs)

go to http://localhost/day2

Thursday, November 15, 12

From URL to Controller

Go to http://localhost/day2/index.php/welcome/index

Site url Controller Function

http://localhost/day2/index.php welcome index

Check it in application/controllers/welcome.php

Thursday, November 15, 12

Loading views

$this->load->view()

CI knows where the views folder is

Thursday, November 15, 12

Sending variables

Create associative array

Send as second parameter in $this->load->view()

Thursday, November 15, 12

Creating more controllers

Duplicate existing

Change filename

Change ClassName (Capitalized)

function index() is called if no function is called in URL

Thursday, November 15, 12

Creating more functions

Create a public function ...()

Load a view

Create the view

Go to URL

Thursday, November 15, 12

Configuring your website

applications/config/config.php

applications/config/autoload.php

applications/config/routes.php

Thursday, November 15, 12

Config.php

Information for CI

URL of the website

Thursday, November 15, 12

Autoload.php

Load libraries & helpers

E.g. Database, Session

Load in autoload.php for site-wide

Load in controller for only that page

Thursday, November 15, 12

Routes.php

Convert URLs to controllers

Point /about-me to controller about

Thursday, November 15, 12

Navigating through your site

Create a view ‘menu_view.php’

Create simple navigation

Use site_url() in links

Load view in top of every view

Thursday, November 15, 12

Using HTML template

Download ‘Template.php’ from dropbox

Place in application/libraries/

Add to $libraries in application/config/autoload.php

Change $this->load->view() to $this->template->load(‘template_view’, ‘viewfile’, vars)

Thursday, November 15, 12

Creating HTML template

Create template_view.php

Add basic HTML structure

Load ‘menu_view.php’ in body

Thursday, November 15, 12

Recreating Calculator

Create controller calculator.php

Add to menu

Create view calculator_view.php

HTML in view, Logic in controller

Thursday, November 15, 12

Form Validation

Lookup at codeigniter reference

Create contact_view.php

Build email form with name, email, message, hidden, submit

Create controller contact.php

Load Form_validation in controller, set up and check for correct values

Thursday, November 15, 12

Send email

Lookup at codeigniter reference

Load Email in controller contact.php

If correct values, send email

Thursday, November 15, 12

Exercise

Create 5 page website (home, about me, portfolio, calculator, contact)

Use template and ‘menu_view.php’

Make calculator and contact work

Thursday, November 15, 12

SQLDatabases

Thursday, November 15, 12

Structured Query Language

Query is a request/command to a database

In our case, a MySQL database

Thursday, November 15, 12

Possibilities

Retrieve data from DB

Insert data in DB

Update data in DB

Delete data from DB

Create tables in DB

> SELECT

> INSERT

> UPDATE

> DELETE

Thursday, November 15, 12

Database

Database is build up in tables

Tables contain records (rows) with data

Thursday, November 15, 12

Select

Select * FROM persons

Select LastName, FirstName FROM persons

Thursday, November 15, 12

Where

SELECT * FROM persons WHERE City = Sandnes

SELECT LastName FROM Persons WHERE FirstName = Tove

= <> < > <= >=

Thursday, November 15, 12

And

SELECT P_id FROM Persons WHERE FirstName = tove AND LastName = Svendson

Thursday, November 15, 12

Order By

SELECT * FROM persons ORDER BY LastName

Thursday, November 15, 12

Insert

INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')

Thursday, November 15, 12

Update

UPDATE Persons

SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob'

Thursday, November 15, 12

Delete

DELETE FROM PersonsWHERE LastName='Tjessem' AND FirstName='Jakob'

Thursday, November 15, 12

CodeIgniter

$this->db->select(‘*’);

$this->db->from(‘persons’);

$this->db->where(‘P_id’, 5);

$this->db->get()->row_array();

$this->db->where(‘P_id’, 5);

$this->db->get(‘persons’)->row_array();

Thursday, November 15, 12

Configuring

application/config/database.php

Enter MySQL Username

Enter MySQL Password

Enter MySQL Database

Add ‘database’ to autoload.php in Libraries

Thursday, November 15, 12

Security

INSERT INTO persons (FirstName, LastName) VALUES (‘Dirk’, ‘Hens’);

Text in FirstName input: Dirk’, ‘Hens’), (‘Jochem

INSERT INTO persons (FirstName, LastName) VALUES (‘Dirk’, ‘Hens’), (‘Jochem’, ‘van Kapel)

CodeIgniter auto adds “”: INSERT INTO persons (FirstName, LastName) VALUES (“‘Dirk’, ‘Hens’), (‘Jochem’”, “‘van Kapel’”);

Thursday, November 15, 12