Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Creating Databases for Web Applications Posting due by next class on project! Lab: using files & work session Class: asp Application object 3-tier, separating."— Presentation transcript:

1 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)

2 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

3 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)

4 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 0 th element is "Curley", 1 st element is "Moe", 2 nd element is "Larry" –php: $arr = explode(",", $stringname); –asp: arr = stringname.split(",");

5 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.

6 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.

7 Organization of both scripts define 3 functions: displayfile handleform displayform if form submitted then handleform (append to file) else displayfile (read from file) displayform

8 File reading & writing <?php $abspath = $PATH_TRANSLATED; $stub=ereg_replace("\\filetest.php","\\",$abspath); $filen = $stub. "scores.txt"; function displayfile() { global $filen; $open = @fopen($filen,"r"); if ($open) { ?> Player Score <? $filecontents = file($filen); for ($n=0;$n<count($filecontents);$n++) { $record = explode(",",$filecontents[$n]); print (" ".$record[0]." "); print (" ".$record[1]." \n"); } print(" "); fclose($open); $ok = TRUE; } else {$ok = FALSE;} return $ok; } replacing slash followed by known name with slash

9 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() { ?> Player handle Score <? } comma part of output

10 if (@$submitted) { if (handleform()) { print("entry made"); } else {print ("entry not made"); } else { if (!displayfile()){ print ("NO PLAYER SCORES ");} displayform(); } ?>

11 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

12 File reading & writing <% 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); %> Player Score <% ok = false; while (!file_stream.AtEndOfStream) { ok = true; record = file_stream.ReadLine(); recorda = record.split(","); Response.Write(" " + recorda[0]+ " "); Response.Write(" "+recorda[1] +" \n"); } Response.Write(" "); file_stream.close(); return ok; } \w*\ any number of alphanumerics

13 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; }

14 function displayform() { %> Player handle Score <% }

15 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 ");} displayform(); } %>

16 Files vs. database Files are cheaper, no special installation. Use when the data is simple, that is, just one table with few fields OR use 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.

17 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

18 File upload test <?php if (@$file) { print ("uploading file named $file_name "); print ("File size is $file_size "); $abspath = $PATH_TRANSLATED; $stub=ereg_replace("\\fileupload.php","\\",$abspath); $fullname = $stub. $file_name; print ("fullname is: $fullname. "); if (copy($file,$fullname)) { print ("file successfully uploaded. "); } else { print ("file could not be copied."); } unlink($file); } print (" upload a file to the server \n"); ?> File need a path with name

19

20 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 retrieve Application.Lock; Application("hitcounter") = Application("hitcounter") + 1; Application.Unlock; In global.asa file, also can set OnEnd routine no php equivalent –use data base!

21 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.

22 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

23 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.

24 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

25 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 (1 st draft of) ER diagram and process diagram of your project. Prepare hardcopy to turn in. –Use this time to get feedback on project.


Download ppt "Creating Databases for Web Applications Posting due by next class on project! Lab: using files & work session Class: asp Application object 3-tier, separating."

Similar presentations


Ads by Google