Top Banner
LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19
45

LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

Mar 27, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

LIS651 lecture 0

Gathering and showing data

Thomas Krichel

2008-10-19

Page 2: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

today

• Introduction to the course• Introduction to PHP• Using form data in PHP

Page 3: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

course resources• Course home page is at

http://wotan.liu.edu/home/krichel/courses/lis651p08a

• The course resource page http://wotan.liu.edu/home/krichel/courses/lis651

• The class mailing list https://lists.liu.edu/mailman/listinfo/cwp-lis651-krichel

• Me.

– Send me email. Unless you request privacy, I answer to the class mailing list.

– Skype me at thomaskrichel. Get skype from skype.com.

Page 4: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

today• We introduce PHP. Understanding PHP is the

most difficult aspect of the course. • We look at how PHP can be used to show the

data that we get from the form. • You should think about what data to get and how

to show it.• Everybody will build an example form and then a

PHP script to show it. • Finally we build a new PHP script that contains

the form. So instead of two files, we only have one.

Page 5: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

PHP introduction

• PHP is the PHP Hypertext Processor.• It is a tool that allows for server-side scripting.• Its predecessor is PHP/FI, Personal Home Page /

Forms Interpreter.• PHP/FI was released by Rasmus Lerdorf in 1995.

It was written in Perl.• PHP/FI version 2 was released in 1997. It was

written in C.• PHP version 5 is the current version.

Page 6: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

Apache and PHP

• When a file ends in .php, is not simply sent down the http connection like other files.

• Instead, apache sends the file to the PHP processor.

• It sends to the client whatever the PHP processor returns.

• The PHP processor is a module that lives inside Apache.

Page 7: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

PHP language

• PHP is an interpreted language.– You write a series of statements.– Apache hands these statements to the PHP interpreter.– The interpreter executes these statements one by one.– When it find an error, it stops running and signals the

error.

• Compiled languages are different. They read the whole program before starting to execute it.

Page 8: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

try it out

• Remember we duplicate validated.html when creating a new new file.

• Right-click on validated.html, choose duplicate.• You may be asked to supply your password

again.• You erase the contents of the dialog box that

suggests a new file name and put your new file name in there.

• If it contains PHP code, it has to end in .php.

Page 9: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

first PHP script

• Create a file with the name info.php, and the following contents<?php

phpinfo();

?>

• nothing else. This will create a test page that tells you everything PHP knows about. Look at some of the variables.

Page 10: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

comment on info.php

• In terms of XML, the "<?php" until "?>" part is called a processing instruction. It is a type of node that we did not encounter in LIS650.

• We can call any part of the file between "<?php" and "?>" a PHP part of the file.

• The XML file here contains just the processing instruction.

Page 11: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

output of phpinfo()

• phpinfo() create a whole web page for you, that validates against a loose HTML specification.

• That page contains a lot of technical detail.• The section we may be interested in is “PHP

Variables”. It contains variables that we may be interested in. These are variables that PHP can understand – from its environment– from the client

Page 12: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

the magic of PHP

• The client never sees the PHP code. It only sees what the PHP processor has done with the code.

• You can write normal HTML code, and you can switch to writing PHP code pretty much at any stage.

• You can have several PHP parts.• PHP parts can not be nested. • The contents of the PHP part can be called a

PHP script.

Page 13: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

statements

• Like a normal text is split into sentences, a PHP script is split into statements.

• A PHP script contains one or more statements.• Each statements tells the interpreter something.• Each statement is ended by a semicolon. • In our first script there is only one statement.• Each statement is ended with a semicolon!• Think of a statement like a rule in CSS. But never

forget the semicolon!

Page 14: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

expressions

• The stuff before the semicolon is called an expression.

• You can think of an expression as anything anyone may want to write in a computer program.

• So an expression is just a way to talk about “stuff” in a program in a more edifying way than just calling it “stuff”.

Page 15: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

functions

• phpinfo() is a function.• Functions are one of the most fundamental

concepts in computer programming.• A function is an expression that does

something to something else. The “something else” is in the parenthesis. It is called the argument of the function.

• The argument of phpinfo() is empty.

Page 16: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

second php script: hello.php

• Normally we write HTML code and then we add PHP parts.

• Take validated.html, copy to hello.php• make the body

<div>

<?php

print("Hello, world!");

?>

</div>

• Validate the resulting XHTML.

Page 17: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

comment on hello.php• print() is also a function. print() prints its

argument. Here the argument is a string. A string is a sequence of characters enclosed by single or double quotes.

• For print, the () can be omitted.• You could have written three statements

<?php

print "<div>";

print "Hello, world!";

print "</div>";

?>

Page 18: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

good style

• Write each statement on a new line.• Add plenty of comments. There are three styles of

comments in a PHP program– // the rest of the line is a comment– # the rest of a line is a comment– /* this is a comment */

• Only last style can be used over several lines. • Do you recognize two of the commenting styles?

Page 19: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

another way to write hello.php

<?php

$greeting="Hello, world!";

print "<div>$greeting</div>";

?>

• Here $greeting is a variable. The first statement assigns it the string value "Hello, world!". The second statement prints it out.

• This example is important because it illustrates the concept of a variable.

• The name of the variable is greeting.

Page 20: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

variable names• Variable name must start with a letter or

underscore. They can contain letters, digits and underscores. The following are examples of illegal names– $2drunk– $bottle-content– $brewer@grosswald

• Variable names are case sensitive. I use lowercase only and add underscores in long names.

• The variable name "$this" is reserved. • It is good to give variables meaningful names.

Page 21: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

strings

• a piece of text in PHP is called a string. • A string is often surrounded by single quotes.

print 'I want beer';

$want='beer';

print 'I want $want'; // prints: I want $want

• If you want to use the values of variables, use double quotes$want='beer';

print "I want $want";

// prints: I want beer

Page 22: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

single and double quotes

• You can use single quotes to quote double quotesprint 'She wrote: "I want beer." and sighed.';

// prints: She wrote: "I want beer." and sighed.

• and vice versaprint "She wrote: 'I want beer.' and sighed";

// prints: She wrote: 'I want beer.' and sighed.

• Sometimes it is not obvious when to put single quotes, double quotes, and when to leave them out. If one thing does not work, try something else.

Page 23: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

the backslash escape

• The backslash is used to quote characters that otherwise are special.print 'Don\'t give me bad beer!';

$kind='bock';

$beer='Festbock';

print "<p class=\"$kind\">$beer</p>";

// prints: <p class="bock">Festbock</p>

• The backslash itself is quoted as \\print "a \\ against beer consumption";

// prints: a \ against beer consumption

Page 24: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

more backslash escapes

• \n makes the newline character• \r make the carriage return (no use in Unix)• \t makes the tab (seldomly used in HTML)• \$ makes the dollar (used in the shop)

– $amount='1.50';– print "you owe \$$amount per bottle.";– // prints: you owe $1.50 per bottle.

If the backslash was not there $ would be considered to be a variable.

Page 25: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

concatenation

• This is done with the . operator. It puts two strings together, the second one after the first one. $cost='5.23';

$message='This costs ' . $cost;

print $message;

// prints: This costs 5.23

Page 26: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

numbers• Numbers are set without the use of quotes.• You can +, -, * and / for the the basic calculations.• There also is the modulus operator %. It gives the

remainder of the division of the first number by the secondprint 10 % 7; // prints 3

• Use parenthesis for complicated calculations$pack=2 * (10 % 7);

print "a $pack pack"; // prints: a 6 pack

Page 27: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

geeky increment/decrement

• ++ is an operator that adds one. The value of the resulting expression depends on the position of the operator$a=4;

print ++$a; // prints: 5

print $a; // prints: 5

$b=4;

print $b++; // prints 4

print $b; // prints 5

• -- works in the same way

Page 28: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

type conversion

• In some circumstance, PHP converts numbers to strings and back. It works like magic. It converts numbers to strings when required$one_in_three=1/3;

print $one_in_three; // prints: 0.333333333333

• and numbers to $string="1.500";

$number=3*$string;

print $number;

• Sometimes it converts to Boolean!

Page 29: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

Boolean value• Every expression in PHP has a Boolean value.• It is either 'true' or 'false'.• In certain situation, an expression is evaluated as

a Boolean • For example

if(expression)

expression1 or expression2

Page 30: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

what is truth?• All strings are true except

– the empty string– the string "0"

• All numbers are true except– 0– 0.0

• example$a=5-4-1; // $a is false

• Note that variables that you have not assigned contents are false. This includes misspelled variables!!

Page 31: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

isset()

• isset() is a function that returns true if a variable is set.

• Under strict coding rules, that are enforced by PHP running on wotan, the PHP processor will issue a notice when you use a variable that has not been set to a value.

• isset($variable) can be used to find out if the variable variable was set before using it.

Page 32: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

comparison operators

• Expressions that are evaluated in Boolean often use comparison operators.$beer == 'grosswald' ; // checks for equality

• Note difference from $beer='grosswald' ; // this is always true

• Other comparisons are< smaller than <= smaller or equal than

> larger than >= larger or equal than

Page 33: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

logical operators

• ‘and’ is logical AND. ‘or’ is logical OR.if($brand=='Budweiser' or $brand="Sam Adams") {

print "Commiserations for buying a lousy beer\n";

} # where is the mistake in this piece of code?

• ‘!’ is Boolean NOT• These can be combined. Use parenthesis

if((($pints) > 2 and ($vehicle=='car')) or (($pints > 6) and ($vehicle=='bicycle'))) {

print "order a cab!\n";

}

Page 34: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

if( condition ) { }

• if( condition ) evaluates an expression condition as Boolean, and executes a block of code surrounded by curly brackets if the expression is true.if($drunk) {

print "Don't drive!\n";

}

• Note you don't need to indent the block as done above, but the way Thomas has done it there is pretty much standard, so do it in the same way.

Page 35: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

if( condition ) {} else {}

• if you have an if() you can add an else block of code to execute when the condition is falseif($sober) {

print "You can drive\n";

}

else {

print "Check if you are fit to drive\n";

}

Page 36: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

elseif( condition ) { }• You can build chain of conditions

if($pints_drunk==0) {

print "You are ok to drive\n";

}

elseif($pints_drunk<3) {

print "Don't use the car, get on your bike\n";

}

elseif($pints_drunk<=6) {

print "Take a cab home\n";

}

else { print "Call the local hospital!\n";

}

Page 37: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

while( condition ) { }

• while( condition ) { } executes a piece of code while the condition condition is true$count=0;

while($count < 100) {

print "Пиво без водки -- деньги на ветер!<br/>";

$count=$count+1; # don't forget to increment $count!

}

Page 38: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

getting back to forms

• Forms deliver data to the server. The server can then process the data and deliver a response.

• If the server process uses PHP, each control is visible to PHP as a PHP variable. It can be read into the script.

Page 39: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

control name and PHP variable• When the form is passed to the PHP script

named with the action= of the the <form> the controls are accessible as PHP variables.

• If name is the name of the control, and if the method is POST, the control is read as the variable $_POST['name'].

• If name is the name of the control, and if the method is GET, the control is read as the variable $_GET['name'].

Page 40: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

example• HTML file greet.html has

<form action="greet.php" method="get"><p>

your last name: <input type="text" name="lastname"/></p></form>

• PHP file greet.php has<?php

print "Hello ";

print $_GET['lastname'];

?>

in addition to the usual HTML stuff.

Page 41: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

iterative form input

• When users start to use your site, the shit hits the fan. Users have many ways to do things wrong.

• Many times you will have to print the form again, with values already filled in.

• In such circumstance a static HTML file for the form is unsuitable.

• Therefore we need a PHP file that writes out the form and processes the form.

Page 42: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

• We include a hidden element in the form to see if it was submitted<input type="hidden" name="submitted" value="1"/>

• We start the script we check for submissionif($_GET['submitted']) {

// work on the data that was submitted

}

else {

// print form

}

check for submission

Page 43: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

master example greet.php<?phpif($_GET['submitted']) { $name=$_GET['name']; print "<div>Hello $name!</div>\n";}else { print "<form action=\"greet.php\" method=\"get\">\n"; print "<div><input type=\"hidden\" "; print "name=\"submitted\" value=\"1\" /></div>\n"; print "<p>Your name <input type=\"text\" "; print "name=\"name\" value=\"$name\" />"; print "</p></form>\n";}?>

Page 44: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

PHP calling itself• One cool thing to help with that is

$_SERVER[PHP_SELF]

It gives the file name of your script in the form. As

you change your script file name, you do not need to change the name of the form submitted.

• So in the previous slide, if you replace action=\"greet.php\"

• with action=\"".$_SERVER['PHP_SELF']."\"

• you can change the name of the PHP file. It will still find the itself as the PHP file.

Page 45: LIS651 lecture 0 Gathering and showing data Thomas Krichel 2008-10-19.

http://openlib.org/home/krichel

Thank you for your attention!

Please switch off computers when you are done!