36
Overcoming code fear An introduction to programming for designers

Programming For Designers V3

  • Upload
    sqoo

  • View
    1.362

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming For Designers V3

Overcoming code fear

An introduction to programming for designers

Page 2: Programming For Designers V3

Unintelligible - Yuk

^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Page 3: Programming For Designers V3

But not all code is squiggles!

SUBTRACT Tax FROM GrossPay GIVING NetPay

Page 4: Programming For Designers V3

So what is a script?

• A script for a play defines a cast and gives them instruction to produce the desired result, a play.

• A script for a computer could be said to be the same. It defines the resources and how they are used in a prescriptive manner to achieve a desired result. The words script and program can be used interchangeably.

Page 5: Programming For Designers V3

Languages

• BASIC, C++, Java, JavaScript, Lisp, SQL, PHP, Perl, Python, Ruby

• Why is HTML not considered a programming language?

• Why is ASP not on this list?

• Learn one language and you have learned a lot about many others.

Page 6: Programming For Designers V3

A standard HTTP request

Request hello.HTML

<h1>Hello</h1>

goodbye.HTML

<h1>Hello</h1>

hello.HTML

Browser Server

Page 7: Programming For Designers V3

A standard HTTP request

Response

<h1>Hello</h1>

Browser Server

hello..HTML

<h1>Hello</h1>

goodbye.HTML

<h1>Bye</h1>

Page 8: Programming For Designers V3

A standard HTTP request

Hello

Page 9: Programming For Designers V3

A CGI Request

Request

hello.PHP

Browser Server

hello.PHP

Echo “Hello”;

goodbye.PHP

Echo “Goodbye”;

Page 10: Programming For Designers V3

A CGI Request

Response hello.PHP

Echo “Hello”;

Browser Server

<h1>Hello</h1>

goodbye.PHP

Echo “Goodbye”;

Page 11: Programming For Designers V3

A CGI Request

hello.PHP

<?php echo “<h1>Hello</h1>”;?>

Page 12: Programming For Designers V3

A CGI Request

Hello

Page 13: Programming For Designers V3

Server Side vs Client Side

Javascript

Java

Flash

PHP

Java

ASP

Client Server

HTTP

Page 14: Programming For Designers V3

Sequences

echo “<p>Hello</p>”;

echo “<p>Goodbye</p>”;To Do:•Go Home•Make Tea•Wash Up

To Do:

Page 15: Programming For Designers V3

Variables

Name Price

John 16.50

Page 16: Programming For Designers V3

Variables

Name Price

Pete 16.50

Page 17: Programming For Designers V3

Variables

$price = 20;

$name = “John”;

echo “Hello $name, the price is £$price”;

$name = “Pete”;

echo “Hello $name, the price is £$price”;

$tax = $price * 0.15;

Page 18: Programming For Designers V3

Input

To Do:A PHP scriptGet

Post

DatabaseCookies

Files

Server Info

Page 19: Programming For Designers V3

Output

To Do:A PHP scriptHTML

Database Cookies

Files

Page 20: Programming For Designers V3

Hello $name

$name = $_REQUEST[‘your_name’];

echo “<p>Hello $name</p>”;

helloform.HTML

… … …<form action=“helloreply.php” method=“post”>

<label>Your Name</label>

<input type=“text” name=“your_name” /> <input type=“submit” />

</form>… … …

helloreply.PHP

<html> <head>…..</head> <body><?php

$name = $_REQUEST[‘your_name’];

echo “<p>Hello $name</p>”;

?></body><html>

Page 21: Programming For Designers V3

Expressions and operators

$counter + 1;

($quantity * $price) + $shipping

“blue” . “bird”

1 < 2

$dog == “Spot”

$price != 44;

Page 22: Programming For Designers V3

Selection – Hello maybe

$name = “Martin”;

if ($name != “John”)

{

echo “Hello!”;

}

else

{

echo “Goodbye!”;

}

To Do:Plan for todayif it is hot then go outsideotherwise stay in bed

Page 23: Programming For Designers V3

A more useful Hello maybe

$name = $_REQUEST[‘your_name’];if ($name != “Pete”){

echo “Hello!”;}else{

echo “Goodbye!”;}

hello.php?your_name=Maryhello.php?your_name=Pete

Page 24: Programming For Designers V3

Repetition (looping)

run a lap

until done 100 laps or completely knackered

$counter = 1;

while ($counter <= 10)

{

echo “$counter <br />”;

$counter = $counter + 1;

}

echo “Finished<br />”;

Page 25: Programming For Designers V3

Functions

function fill_kettle()

{

place kettle under tap;

turn on tap;

wait until kettle is full;

turn off tap;

}

fill_kettle();

Page 26: Programming For Designers V3

Hello function

hello();

function hello()

{

$name = $_REQUEST[‘name’];

echo “<p>Hello $name</p>”;

}

Page 27: Programming For Designers V3

Hello parameter

<?php

welcome(“Jane”);welcome($somebody);

function welcome($name){

$message = “<p>Hello $name</p>”; echo $message;

}

?>

Page 28: Programming For Designers V3

Databases

• Databases need code to retrieve data and translate into HTML

• SQL is the most popular language for asking databases for data

• PHP can talk to many databases using SQL but most often is paired with MySQL

Page 29: Programming For Designers V3

SQL

• SQL is used with other languages such as PHP

SELECT * FROM orders;

SELECT profile_pic FROM profiles WHERE profile_name = “Jimbo123”;

Page 30: Programming For Designers V3

Let’s Design an App

What have you done today?

Update

Cooked a curry

Went to work

Jogged 2 miles

Page 31: Programming For Designers V3

Twaddl Database

ID Twaddl

1 Argued with girlfriend – again!

3 Went to work

4 Cooked a curry

2 Jogged 2 miles

Page 32: Programming For Designers V3

Twaddl Main Program Flow

Handle posted twaddl

List twaddles

Page 33: Programming For Designers V3

Handle Posted Twaddl

Is submitted twaddle empty?

Insert twaddle into database

List twaddles

NoYes

Display an error message

Page 34: Programming For Designers V3

List Twaddles

Get list of twaddles from database

End Program

For each twaddle in list: Display twaddle

Page 35: Programming For Designers V3

Complications! An example.

• Ordinals: 1st 2nd 3rd 4th etc.• Mod: 53 % 10

function ordinal($number){

$last_digit = $number % 10;if ($last_digit == 1) $ord = “st”;elseif ($last_digit == 2) $ord = “nd”;elseif ($last_digit == 3) $ord = “rd”;else $ord = “th”;echo $number . $ord;

}