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
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
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
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.
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.
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.
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
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>
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
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
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){
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
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”);
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
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
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)
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
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”;?>
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
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 */
24
IST 210 Comments Example:
<?php//This will write hello world to
the screenecho “hello world”;
/* This script was providedcourtesy of IST210.copyright 2003 */
?>
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
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”;?>
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
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”);
?>
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
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;
?>
31
IST 210 Defining Variables Variable Name Rules
Must start with a letter or underscore Can only contain letters, numbers and
underscores
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 “”
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.
34
IST 210 Different quotes with echo Also, you don’t have to use quotes
to echo a variable
Example:<?php
echo $myVar;?>
35
IST 210 Operators Operators can take one or more
variables or values and return a new value
Several Types of Operators
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;
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;
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
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
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
41
IST 210 Operators Assignment operator
The basic assignment operator is = Example:
$myvar=“hello world”;$i=0;
42
IST 210 Operators Incrementing Operators
4 incrementing operators Pre-Increment Post-Increment Pre-Decrement Post-Decrement
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
?>
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
?>
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
?>
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
?>
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”
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”
50
IST 210 Conditions Conditional statements are
statements that tell the script to do something if a condition is true
Syntax:if( condition ){
action}
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
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
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!”;}
?>
54
IST 210 Conditions Notice that in php the elseif
command is one word unlike in C where it is two words (else if)
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
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
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
60
IST 210 Loops Do-While loops
Alternative syntax of a while loop
do{ action
actionaction
}while(condition)
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)
?>
62
IST 210 Loops For loops
for loops syntax:for(interval variable set; condition;
interval){action
}
63
IST 210 Loops Example:
<?php$myVar=“hello world”;
for($i=0;$i<3;$i++){echo $myVar;
}?>
Produces same result as while loop
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
65
IST 210 Arrays Arrays are lists of information
They are created using the array() function
Syntax:
$myArray=array(“a”,”b”,”c”,”d”,”e”);
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
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”;
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
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
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
72
IST 210 Loops Example:
<?php$myArray=(“a”,”b”,”c”,”d”,”e”);
foreach($myArray as $array_item){echo $array_item;
}?>
Result: abcde
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”;}
?>
74
IST 210 Loops Result:
Array item # 0 = aArray item # 1 = bArray item # 2 = cArray item # 3 = dArray item # 4 = e
75
IST 210 File Inclusions What are file inclusions?
Include the code from one file into another
Example:
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
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
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”;?>
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;
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);
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.";