Creating Databases for Web Applications Posting due by next class on project! Lab: using files &...

25
Creating Databases for Web Applications Posting due by next class on project! Lab: using files & work session Class: asp Application object 3-tier, separating design & content Homework: Decide on

Transcript of Creating Databases for Web Applications Posting due by next class on project! Lab: using files &...

Creating Databases for Web Applications

Posting due by next class on project!

Lab: using files & work session

Class: asp Application object

3-tier, separating design & content

Homework: Decide on project. Next week: Initial presentation (diagrams)

Files (aka flat files)

• php & asp example of reading and appending (adding at the end) to a flat file

• regular expression replace function to determine complete path for file

Modes

• asp: 1 (default) for read, 8 for append (will NOT create new file), – 2 is for write

• php: r for read, a for append (creates a new file if none exists),– w for write (creates a new file, discards old contents),– r+ read and write, – w+ read and write AND create new file, discard old

contents before writing, – a+ read and append (create new)

Working with a record

• how to explode / split up a string into an array of elements based on a defined delimiter:– "Curley, Moe, Larry" Array where 0th

element is "Curley", 1st element is "Moe", 2nd element is "Larry"

– php: $arr = explode(",", $stringname);– asp: arr = stringname.split(",");

Demonstrate on sharon

• filetest.php• filetest.asp• Note: both scripts use the same file. It is created

by the filetest.php script the first time. You could also upload a file using ws-ftp.

• NOTE: on home computer, needed to create empty file in NotePad, find it (from My computer), left click, choose properties and deselect Archive.

Where's the file?

• In each case, need to do something to get to the file, even if it is in the same folder as the script file.

• Get the system variable that indicates where the script file is. Alter it (using regular expression replace function) to be the complete path to the file.

Organization of both scripts

define 3 functions: displayfilehandleformdisplayform

if form submitted thenhandleform (append to file)

elsedisplayfile (read from file)displayform

<html> <head><title>File reading & writing </title> </head><body><?php$abspath = $PATH_TRANSLATED;$stub=ereg_replace("\\filetest.php","\\",$abspath);$filen = $stub . "scores.txt";function displayfile() {global $filen; $open = @fopen($filen,"r");if ($open) {?><table><tr><td>Player </td> <td> Score </td> </tr><?

$filecontents = file($filen);for ($n=0;$n<count($filecontents);$n++) {

$record = explode(",",$filecontents[$n]);print ("<tr><td>".$record[0]."</td>");print ("<td>".$record[1]."</td></tr>\n");}

print("</table>");fclose($open);$ok = TRUE;}else {$ok = FALSE;}return $ok;

}

replacing slash followed by known name with slash

function handleform(){global $player;global $score;global $filen;$open=fopen($filen,"a");if ($open) {

fwrite($open,"$player,$score\n");fclose($open);$ok=TRUE;

}else {$ok=FALSE;

}return $ok;

}function displayform() {

?><form action="filetest.php" method=get>Player handle <input type=text name='player'><br> Score <input type=text name='score'><br><input type=hidden name='submitted' value = 'TRUE'> <input type=submit name='submit' value='submit entry'></form><?}

comma part of output

if (@$submitted) {

if (handleform()) {

print("entry made");

}

else {print ("entry not made");

}

}

else {

if (!displayfile()){

print ("NO PLAYER SCORES<br>");}

displayform();

} ?> </body> </html>

Comments

• Using these functions is a neat way to compartmentalize actions.

• Functions could be put in files to be included (require/include).

• on to asp

<html> <head><title>File reading & writing </title> </head><body><%@ Language=JavaScript %><%var abspath=String(Request.ServerVariables("PATH_TRANSLATED"));var filepath=abspath.replace(/\\\w*\.asp/,"\\") + "scores.txt";function displayfile() {fso=new ActiveXObject("Scripting.FileSystemObject");file_stream=fso.OpenTextFile(filepath);%><table><tr><td>Player </td> <td> Score </td> </tr><%ok = false;while (!file_stream.AtEndOfStream) { ok = true; record = file_stream.ReadLine(); recorda = record.split(","); Response.Write("<tr><td>" + recorda[0]+ "</td>"); Response.Write("<td>"+recorda[1] +"</td></tr>\n");

} Response.Write("</table>"); file_stream.close(); return ok;}

\w*\ any number of alphanumerics

function handleform(){

ok = true;

var player = String(Request("player"));

var score = String(Request("score"));

fso=new ActiveXObject("Scripting.FileSystemObject");

file_stream=fso.OpenTextFile(filepath, 8); // 8 for append

file_stream.Write(player + "," + score + "\n");

return ok;

}

function displayform() {

%>

<form action="filetest.asp" method=get>

Player handle <input type=text name='player'>

<br> Score <input type=text name='score'><br>

<input type=hidden name='submitted' value = 'TRUE'>

<input type=submit name='submit' value='submit entry'>

</form>

<%

}

var submitted = String(Request("submitted"));

if (submitted!="undefined") {

if (handleform()) {

Response.Write("entry made");

}

else {Response.Write ("entry not made");

}

}

else {

if (!displayfile()){

Response.Write ("NO PLAYER SCORES<br>");}

displayform();

}

%> </body> </html>

Files vs. database

• Files are cheaper, no special installation. Use when the data is simple, that is, just one table with few fields ORuse when data is complex, many links, many variable length fields AND/OR performance & space are critical

• Databases provide many functions for tables of data, but at a cost.

Uploading files

• Allow site user to upload file

• Appropriate for applications such as in inputproducts that involves citing an image file for each product

• php implementation

• HTML <form action='fileupload.php' method=POST

ENCTYPE="multipart/form-data">

<html><head><title>File upload test </title> </head> <body><?phpif (@$file) {

print ("uploading file named $file_name <br>");print ("File size is $file_size <br>");$abspath = $PATH_TRANSLATED;$stub=ereg_replace("\\fileupload.php","\\",$abspath);$fullname = $stub . $file_name;print ("fullname is: $fullname.<br>");if (copy($file,$fullname)) {

print ("file successfully uploaded. <br>"); }else { print ("file could not be copied."); }unlink($file);

}print ("<br>upload a file to the server<br>\n");?><form action='fileupload.php' method=POST ENCTYPE="multipart/form-data">File <input type=file name="file"><br><input type=submit name="submit" value="upload file"></form> </body> </html>

need a path with name

asp Application object• Maintained by system across all users (all

sessions). Example (not fully tested)• In global.asa file, OnStart routine: set and initialize

application variables. In code, store and retrieveApplication.Lock;Application("hitcounter") = Application("hitcounter") + 1;Application.Unlock;

• In global.asa file, also can set OnEnd routine

• no php equivalent– use data base!

Recall: my terms

• 3 levels of language:– HTML (text to be interpreted by the browser)

– php OR asp and JavaScript

– SQL

• Really more complicated: for example, what is the interpreter of the include/require command?

• Useful to distinguish 'who' is audience for a fragment of code.

Data base idea: 3 tier view of systems

• operation – interface/display processor

– business logic processor

– data base management processor

• This does correspond to– client computer interpreting HTML

– server computer interpreting php or asp

– MySql engine or Access engine operating the data base management system

display versus content• implementation

– what the content is– how content is presented

• Recent 'new thing'– Use extended markup language XML to specify form of

content and the content itself.– Use extended style language XSL and extended style

language transform XSLT to specify the look.

• One use of this could be to have web pages show up appropriately on a computer and on a cell phone type of device.

Comments

• Dividing task into subtasks is good…but there are associated costs.

• XML also has role in B2B transactions (followon to EDI for supplier/vendor communications)

• php/XML and asp/XML connections appear to be still in development stage

Homework

• Post proposal for project– May be team of 2. From more, more is expected.

• Next class: time to work on projects and also catch up doing practice scripts.

• Next week:– present (1st draft of) ER diagram and process diagram

of your project. Prepare hardcopy to turn in.

– Use this time to get feedback on project.