Top Banner
1 IST 210 Database and the Web IST 210
85

IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

Dec 26, 2015

Download

Documents

Jody Bridges
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: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

1

IST 210 Database and the Web

IST 210

Page 2: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

04/19/23

IST 210 Dynamic Web Applications

Static Web application Request with a URL (e.g.,

http://www.psu.edu)Which contains three components: protocol, web

server name, and folder path to an HTML page Server simply send back the page

From static to dynamic web pages Take user input and respond accordingly Allow access to information stored in a

database

Page 3: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

3

IST 210

Internet

HTML

1. Allows connection2. Receives SQL3. Returns data

1. Request to execute script2. Connects to the db3. Sends SQL to DBMS4. Receives Data from DBMS5. Creates and sends HTML to client

HTML

HTML

HTML

Page 4: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

4

IST 210

HTML

Script

SQL

Page 5: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

04/19/23

IST 210 Server-Side Processing

Programs that run on the server and interact with the server through a well-defined API

Reusable software components Code inside a web page that is

interpreted by the web server

Page 6: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

6

IST 210 PHP PHP is programming model that allows

dynamic, interactive Web pages to be created on the server.

PHP runs in-process with the server, and is optimized to handle large volume of users.

When an ‘.php’ file is requested, Web server calls PHP, which reads requested file, executes any commands, and sends generated HTML page back to browser.

Page 7: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

7

IST 210 PHP

PHP

Scripts

Page 8: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

8

IST 210 PHP Server-side script language It resides on Web Server side

You need to put your script into your account

Then, any client can access it.

Page 9: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

9

IST 210 Starting A PHP File What can I use to write a php file?

Any simple text editor. ie. Notepad, wordpad, pico, etc.

What can’t I use to write a php file? any formatted text editors

ie. Microsoft word, etc.

Page 10: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

10

IST 210 Starting A PHP file The first thing to type is: <?php ….. ?>

what does “<?php” do opens a php area

what does “?>” do? closes a php area

whatever is enclosed in between <?php and ?> is considered to be PHP code

Page 11: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

11

IST 210

How do I use php with a webpage?

Inside of a php document, php can intertwine with html:

<html><body><h2>welcome to the site</h2><?php

echo $name;?><br><br> please stay<?php echo $name; ?></body></html>

Page 12: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

12

IST 210

How do I use php with a webpage? All text in between the <?php

and ?> tags will be parsed as PHP

All text not in between the <?php and ?> tags will be parsed as HTML

Page 13: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

13

IST 210 Saving a php file How do I save a php file?

When saving choose a plain text type for file type, and save with the .php extension

Example: filename.php

Page 14: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

14

IST 210 Basic Rules All complete statements must end

with a semicolon Similar to C in this way

These are complete statementsecho “hello world”;$myVar=“hello world”;

These are partial statementsif( condition ){while( condition){

Page 15: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

15

IST 210 The echo command What is the echo command?

The PHP version of cout for c++ or document.write for javascript

Outputs html to screen Example:

<?php

echo ‘hello world’;?>

Writes “hello world” to the screen

Page 16: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

16

IST 210 Alternate syntax for echo There are several ways to use the

echo (or mirror commands) echo “hello world”; echo(“hello world”); print “hello world”; print(“hello world”);

Page 17: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

17

IST 210 Escape Characters Escape characters are special

characters that can be used to represent a character that cannot be literally represented or that would be treated as an command itself

Page 18: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

18

IST 210 Escape Characters Example:

<?phpecho ‘ I don’t know ‘;

?>

This example would cause an error because the echo command would end when the second ‘ appeared and php would not know what to do with the rest of the statement

Page 19: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

19

IST 210 Escape Characters This can be prevented by adding a

backslash ( \ )

Same example with backslash<?php

echo ‘I don\’t know’;?>

This would work because php is told that any character after \ is to be ignored (as a command)

Page 20: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

20

IST 210 Escape Characters Certain letters when used after a

backslash will have special effects \n inserts a line break \t inserts a tab etc.

Example:echo “hello\n\thow are you”;

Result:hello

how are you

Page 21: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

21

IST 210 Escape Characters Remember, PHP is parsed on the server

This means that using \n will not make the result go to a new line on the webpage but rather will make the result go to a new line in the source code

To go to a new line on the webpage, use a <br> tag

Example:<?php

echo “hello\n<br>\thow are you”;?>

Page 22: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

22

IST 210 Comments Comments allow you to make

comments on the code that are ignored by the server

Two kinds of comments Single line comments Multi-line comments

Page 23: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

23

IST 210 Comments Single line comment:

Makes a comment that can only appear on one line

//This is a single line comment

Multi-line comment: Makes a comment that can go on until you

tell it to stop/* This

is a multiline comment */

Page 24: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

24

IST 210 Comments Example:

<?php//This will write hello world to

the screenecho “hello world”;

/* This script was providedcourtesy of IST210.copyright 2003 */

?>

Page 25: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

25

IST 210 Variables Variables are defined with a $

Example: A variable named “myVar” would be

referred to with $myVar

Variables are CASE SENSITIVE This means that $myVar and $myvar

are two different variables

Page 26: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

26

IST 210 Defining Variables Unlike C++ you don’t have to define a

variable with it’s type (int, char, etc.) This is done automatically in php

Here is a sample of code that defines a variable, myVar, as the string “hello world”

<?php

$myVar=“hello world”;?>

Page 27: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

27

IST 210 Defining Variables Constant Variables

Constant variables are those that are constant and will be there throughout the whole script

Constant variables are good if you have a variable that will be used in multiple scripts

Constant variables cannot be redefined or undefined after being set

Page 28: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

28

IST 210 Defining Variables define()

The define() function is used to define a variable as a constant

Syntax:define(variable_name,variable_value);

Example:<?php

define(“myConstVar”, “hello world”);

?>

Page 29: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

29

IST 210 Defining Variables You can also define a constant

variable as case insensitive by using a third parameter

<?phpdefine(“myConstVar”,”hello

world”,TRUE);?>

Constant variables are case sensitive by default

Page 30: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

30

IST 210 Defining Variables You don’t use a $ when referring to

constant variables Example:

<?phpdefine(“constVar1”,”hello world”);define(“constVar2”,” from

psu”,TRUE);

echo constVar1;echo constVar2;echo CONSTVAR2;

?>

Page 31: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

31

IST 210 Defining Variables Variable Name Rules

Must start with a letter or underscore Can only contain letters, numbers and

underscores

Page 32: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

32

IST 210 Different quotes with echo Using ‘’ and “” is different with the

echo command

Using “” will parse the content and ‘’ will take it literally

Special escape commands (like \n) will only work with “”

Page 33: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

33

IST 210 Different quotes with echo

In the following script:<?php

$myVar=“hello world”;echo ‘$myVar’;echo “$myVar”;

?>

The first echo would write $myVar to the screen, the second would write hello world.

Page 34: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

34

IST 210 Different quotes with echo Also, you don’t have to use quotes

to echo a variable

Example:<?php

echo $myVar;?>

Page 35: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

35

IST 210 Operators Operators can take one or more

variables or values and return a new value

Several Types of Operators

Page 36: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

36

IST 210 Mathematical Operators

Operator Description Example

+ Addition - adds two variables or values together

$x=$y+3;

- Subtraction - subtracts one variable or value from another

$y=$x-3;

* Multiplication - multiplies two variables or values together

$z=$x*3;

/ Division – divides one variable or value by another

$z=$x/$y;

% Modulo – divides one variable or value by another and returns the remainder

$r=$a%$b;

Page 37: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

37

IST 210 Shorthand Mathematical operators

Operator

Description Example

+= Addition – adds a variable or value to the original value

$x+=3;

-= Subtraction - subtracts a variable or value from the original value

$x-=3;

*= Multiplication - multiplies original variable by a variable or value

$x*=3;

/= Division – divides original variable by a variable or value

$x/=3;

%= Modulo – divides original variable by a variable or value and returns the remainder

$x%=3;

Page 38: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

38

IST 210 Comparison Operators

Operator Description Example

== Equal – true if $a is equal to $b $a==$b

!= Not equal – true if $a is not equal to $b

$a!=$b

=== Identical – true if $a is equal to $b and they are of the same type

$a===$b

!== Not Identical – true if $a is not equal to $b or they are not of the same type

$a!==$b

<> Not equal – true if $a is not equal to $b (alternate syntax for !=)

$a<>$b

Page 39: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

39

IST 210 Comparison Operators cont.

Operator Description Example

< Less than – true if $a is less than $b

$a<$b

> Greater than – true if $a is greater than $b

$a>$b

<= Less than or equal to – true if $a is less than or equal to $b

$a<=$b

>= Greater than or equal to – true if $a is greater than or equal to $b

$a>=$b

Page 40: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

40

IST 210 Logical Operators

Operator Description Example

&& And – true if both $a and $b are true

$a && $b

|| Or – true if either $a or $b are true

$a || $b

! Not – true if $a is not true !$a

andor

And – alternate syntax for && Or – alternate syntax for ||

$a and $b$a or $b

<> Xor – true if either $a or $b but not both are true

$a xor $b

Page 41: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

41

IST 210 Operators Assignment operator

The basic assignment operator is = Example:

$myvar=“hello world”;$i=0;

Page 42: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

42

IST 210 Operators Incrementing Operators

4 incrementing operators Pre-Increment Post-Increment Pre-Decrement Post-Decrement

Page 43: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

43

IST 210 Operators Pre-Increment (++$a)

Increments $a by one, then returns $a

Example:<?php

$a=5;echo ++$a; //this would write 6echo $a; //this would write 6

?>

Page 44: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

44

IST 210 Operators Post-Increment ($a++)

Returns $a, then increments $a by one

Example:<?php

$a=5;echo $a++; //this would write 5echo $a; //this would write 6

?>

Page 45: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

45

IST 210 Operators Pre-Decrement (--$a)

Decrements $a by one, then returns $a

Example:<?php

$a=5;echo --$a; //this would write 4echo $a; //this would write 4

?>

Page 46: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

46

IST 210 Operators Post-Decrement ($a--)

Returns $a, then decrements $a by one

Example:<?php

$a=5;echo $a--; //this would write 5echo $a; //this would write 4

?>

Page 47: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

47

IST 210 Operators String operators

There are two string operators

. and .=

Page 48: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

48

IST 210 Operators The . Operator

Used to combine two strings Example:

<?php$myVar=“hello”;$myVar=$myVar.“ world”;echo $myVar.” how are you”;

?>

This echoes “hello world how are you”

Page 49: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

49

IST 210 Operators The .= Operator

Used to combine two strings Example:

<?php$myVar=“hello”;$myVar.=“ world”;echo $myVar.” how are you”;

?>

This echoes “hello world how are you”

Page 50: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

50

IST 210 Conditions Conditional statements are

statements that tell the script to do something if a condition is true

Syntax:if( condition ){

action}

Page 51: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

51

IST 210 Conditions Example:

<?php$myVar=“hello world”;if($myVar==“hello world”){ echo $myVar;}

?>

since the variable $myVar=“hello world”, the condition returns true and $myVar is displayed on the screen

Page 52: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

52

IST 210 Conditions Multiple conditions

You can also use multiple if statements to do different things based on results

There is also the else keyword which does an action if none of the if statements are true

To relate more than one if statements to each other, you have to use else if

Page 53: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

53

IST 210 Conditions Example:

<?php$myVar=“hello world”;if($myVar==“hello world”){

echo $myVar;}elseif($myVar==“hello user”){

echo $myVar;echo “goodbye”;

}else{

echo “leave now!”;}

?>

Page 54: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

54

IST 210 Conditions Notice that in php the elseif

command is one word unlike in C where it is two words (else if)

Page 55: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

55

IST 210 Conditions The previous example says:

If $myVar=“hello world” display $myVar on the screen

If $myVar=“hello user” display $myVar followed by “goodbye” on the screen

Otherwise write “leave now” to the screen

Page 56: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

56

IST 210 Loops What are loops?

Loops allow you to loop through something until a condition is true

Different types of loops While loops Do-while loops For loops Foreach loops

Page 57: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

57

IST 210 Loops While loops

While loops syntax:while(condition){

do action;}

Page 58: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

58

IST 210 Loops Example

<?php

$myVar=“hello world”;$i=0;

while($i<3){echo $myVar;$i++;

}?>

Page 59: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

59

IST 210 Loops The previous example will result

in…hello worldhello worldhello world

While $i is less than 2, the loop keeps performing the action. Since $i is increased by one each time

through the loop, the loop will perform the action 3 times

Page 60: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

60

IST 210 Loops Do-While loops

Alternative syntax of a while loop

do{ action

actionaction

}while(condition)

Page 61: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

61

IST 210 Loops The same loop used with while would look

like this in do-while syntax:

<?php$myVar=“hello world”;$i=0;

do{ echo $myVar; $i++;} while($i<3)

?>

Page 62: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

62

IST 210 Loops For loops

for loops syntax:for(interval variable set; condition;

interval){action

}

Page 63: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

63

IST 210 Loops Example:

<?php$myVar=“hello world”;

for($i=0;$i<3;$i++){echo $myVar;

}?>

Produces same result as while loop

Page 64: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

64

IST 210 Loops For loop broken down:

for($i=0;Sets a variable, $i, equal to 0

$i<3;Sets condition that needs to return false to end loop (same as condition in while(condition) )

$i++){Sets interval for $i to change each time through the loop, in this case it will increase by 1 each time

Page 65: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

65

IST 210 Arrays Arrays are lists of information

They are created using the array() function

Syntax:

$myArray=array(“a”,”b”,”c”,”d”,”e”);

Page 66: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

66

IST 210 Arrays Defining indexes in arrays

If you define an array as shown in the syntax example, the first item will be array item 0, the second will be array item 1, and so on.

You can tell php which number in the array (or array key) each item will be

Page 67: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

67

IST 210 Arrays Example:

<?php$myArray=array(“a”, 5 => “b”, 2 => “c”);

?>

Results in:$myArray[0]=“a”;$myArray[1]=NULL;$myArray[2]=“c”;$myArray[3]=NULL;$myArray[4]=NULL;$myArray[5]=“b”;

Page 68: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

68

IST 210 Loops Using for loops and while loops with arrays

This Is an easy way to loop through an array and display its contents

The sizeof() function Finds the number of items in an array

Example:<?php

$myArray=array(“a”,”b”,”c”);$size_of_array=sizeof($myarray);echo $size_of_array;

?> Result is 3

Page 69: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

69

IST 210 Loops Using a for loop to display an

array:<?php

$myArray=array(“a”,”b”,”c”,”d”,”e”);for($i=0;$i<sizeof($myArray);$i++){

echo $myArray[$i];}

?>

Result: abcde

Page 70: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

70

IST 210 Loops Using a while loop to display an array:

<?php$myArray=array(“a”,”b”,”c”,”d”,”e”);$i=0;while($i<sizeof($myArray)){

echo $myArray[$i];$i++;

}?>

Result: abcde

Page 71: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

71

IST 210 Loops foreach loop

Used with arrays Syntax:

foreach(array_item as $value){

action}

Page 72: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

72

IST 210 Loops Example:

<?php$myArray=(“a”,”b”,”c”,”d”,”e”);

foreach($myArray as $array_item){echo $array_item;

}?>

Result: abcde

Page 73: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

73

IST 210 Loops You can also get the array key and the

value using a foreach loop Example:

<?php$myArray=array(“a”,”b”,”c”,”d”,”e”);

foreach($myArray as $array_item => $array_value){echo ‘Array item # ’.$array_item.’ = ‘.

$array_value.”\n”;}

?>

Page 74: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

74

IST 210 Loops Result:

Array item # 0 = aArray item # 1 = bArray item # 2 = cArray item # 3 = dArray item # 4 = e

Page 75: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

75

IST 210 File Inclusions What are file inclusions?

Include the code from one file into another

Example:

Page 76: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

76

IST 210 File Inclusions This is file1.php

<?phpecho “hello world”;

?>

Page 77: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

77

IST 210 File Inclusions This is file2.php

<?phpinclude(“file1.php”);echo “ from psu”;

?>

Page 78: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

78

IST 210 File Inclusions The result of running file2.php will

be “hello world from psu”

This is because all of the code from file1.php was put into file2.php in place of the include() command when the code was parsed

Page 79: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

79

IST 210 File Inclusions All kinds of files can be included (html, php,

etc.)

Two commands to include files: include() and require()

Difference is that if the file isn’t found, include() will return a warning and move on, require() will return a fatal error and stop the script from running

Page 80: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

80

IST 210 File Inclusions Syntax example of include and require:

include(filename);require(filename);

Example of include and require:<?php

require(“check_user.php”); //check validity of user

include(“header.php”); //include page header

echo “content”;?>

Page 81: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

81

IST 210 Connect to DB2 with PHP<?php function db_connect() { $dsn = "tsb4"; $uid = "tsb4"; $password = "********"; $db = odbc_connect($dsn, $uid, $password); if ($db == 0) { echo "Error: Connection problem occurred.\n"; $err = odbc_errormsg($db); echo($err); return FALSE; } return $db;

Page 82: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

82

IST 210 Simple queries in PHP

$db = db_connect();

$query = "select item.ItemName, item.Price from Sells, Item

where Sells.DepartmentNum=1 AND Sells.ItemNum=Item.ItemNum";

if ($db != 0) { $result = odbc_exec($db, $query); if ($result != 0) { echo "<table border=1 width=\"50%\">";

$num = odbc_num_fields($result);

Page 83: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

83

IST 210 Displaying queries // print column names first echo "<tr>"; for ($i=1; $i<=$num; $i++) { echo "<th>" . odbc_field_name($result, $i) . "</th>\n"; } echo "</tr>";

// print all the value rows second while (odbc_fetch_row($result)) {

if ($i%2 == 0) { echo "<tr bgcolor=lightgrey>"; } else { echo "<tr>"; } for ($i=1; $i<=$num; $i++) {

echo "<td>" . odbc_result($result, $i) . "</td>"; } echo "</tr>\n"; } echo "</table>\n"; } else { echo "Sorry, your search did not return any results.";

Page 84: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

84

IST 210 Don't forget this

odbc_close($db); ?>

Page 85: IST 210 1 Database and the Web IST 210. Dynamic Web Applications Static Web application Request with a URL (e.g., ) Which contains three.

85

IST 210 Resources PHP Official site

http://www.php.net

PHP Official documentationhttp://www.php.net/manual/en/