Advanced web 2012 lecture 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

Advanced Web 2012 Lecture 4 PowerPoint PPT Presentation


  • 47 Views
  • Uploaded on
  • Presentation posted in: General

Advanced Web 2012 Lecture 4. PHP. 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?

Download Presentation

Advanced Web 2012 Lecture 4

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Advanced web 2012 lecture 4

Advanced Web2012Lecture 4

Sean Costain 2012


Advanced web 2012 lecture 4

PHP

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

Sean Costain 2012


How to start

How to Start

  • 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

Sean Costain 2012


Hello world

Hello World!

HTML

PHP

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

Sean Costain 2012


Commenting

Commenting

2 ways of doing this

// <- this is a single line comment

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

Sean Costain 2012


Variables

Variables

  • 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

Sean Costain 2012


How to use echo

How to use Echo

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 \

Sean Costain 2012


If then

If … then

  • 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

Sean Costain 2012


Operators

Operators

  • 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 ==

Sean Costain 2012


If then else

If … then … else

  • 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

Sean Costain 2012


Switch

Switch

  • 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%...

Sean Costain 2012


Loopy loops

Loopy Loops!!

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

Sean Costain 2012


For loop

For Loop

  • 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

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

Pretty much Optimal

Sean Costain 2012


While loop

While Loop

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

Sean Costain 2012


Forms

Forms

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:

Sean Costain 2012


Php and mysql

PHP and MySQL

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.

Sean Costain 2012


C r u d

C.R.U.D

  • 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.

Sean Costain 2012


Connecting to a database

Connecting to a Database

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();

Sean Costain 2012


Create

Create

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.

Sean Costain 2012


Advanced web 2012 lecture 4

Read

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

Sean Costain 2012


Update

Update

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

Sean Costain 2012


Delete

Delete

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

Sean Costain 2012


  • Login