Upload
sqoo
View
1.369
Download
0
Tags:
Embed Size (px)
Citation preview
Overcoming code fear
An introduction to programming for designers
Unintelligible - Yuk
^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
But not all code is squiggles!
SUBTRACT Tax FROM GrossPay GIVING NetPay
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.
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.
A standard HTTP request
Request hello.HTML
<h1>Hello</h1>
goodbye.HTML
<h1>Hello</h1>
hello.HTML
Browser Server
A standard HTTP request
Response
<h1>Hello</h1>
Browser Server
hello..HTML
<h1>Hello</h1>
goodbye.HTML
<h1>Bye</h1>
A standard HTTP request
Hello
A CGI Request
Request
hello.PHP
Browser Server
hello.PHP
Echo “Hello”;
goodbye.PHP
Echo “Goodbye”;
A CGI Request
Response hello.PHP
Echo “Hello”;
Browser Server
<h1>Hello</h1>
goodbye.PHP
Echo “Goodbye”;
A CGI Request
hello.PHP
<?php echo “<h1>Hello</h1>”;?>
A CGI Request
Hello
Server Side vs Client Side
Javascript
Java
Flash
PHP
Java
ASP
Client Server
HTTP
Sequences
echo “<p>Hello</p>”;
echo “<p>Goodbye</p>”;To Do:•Go Home•Make Tea•Wash Up
To Do:
Variables
Name Price
John 16.50
Variables
Name Price
Pete 16.50
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;
Input
To Do:A PHP scriptGet
Post
DatabaseCookies
Files
Server Info
Output
To Do:A PHP scriptHTML
Database Cookies
Files
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>
Expressions and operators
$counter + 1;
($quantity * $price) + $shipping
“blue” . “bird”
1 < 2
$dog == “Spot”
$price != 44;
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
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
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 />”;
Functions
function fill_kettle()
{
place kettle under tap;
turn on tap;
wait until kettle is full;
turn off tap;
}
fill_kettle();
Hello function
hello();
function hello()
{
$name = $_REQUEST[‘name’];
echo “<p>Hello $name</p>”;
}
Hello parameter
<?php
welcome(“Jane”);welcome($somebody);
function welcome($name){
$message = “<p>Hello $name</p>”; echo $message;
}
?>
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
SQL
• SQL is used with other languages such as PHP
SELECT * FROM orders;
SELECT profile_pic FROM profiles WHERE profile_name = “Jimbo123”;
Let’s Design an App
What have you done today?
Update
Cooked a curry
Went to work
Jogged 2 miles
Twaddl Database
ID Twaddl
1 Argued with girlfriend – again!
3 Went to work
4 Cooked a curry
2 Jogged 2 miles
Twaddl Main Program Flow
Handle posted twaddl
List twaddles
Handle Posted Twaddl
Is submitted twaddle empty?
Insert twaddle into database
List twaddles
NoYes
Display an error message
List Twaddles
Get list of twaddles from database
End Program
For each twaddle in list: Display twaddle
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;
}
Where from here?
PHP Lessons Online:
• http://uk3.php.net/tut.php
• http://www.webmonkey.com/tutorial/PHP_Tutorial_for_Beginners
Tuition