1 DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida Media Software Design.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

HTML Basics Customizing your site using the basics of HTML.
Html: getting started HTML is hyper text markup language. It is what web browsers look at on the Internet. HTML documents should be created in a simple.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
Lesson 16 Enhancing Documents
CPSC 203 Introduction to Computers Tutorial 59 & 64 By Jie (Jeff) Gao.
Calendar Browser is a groupware used for booking all kinds of resources within an organization. Calendar Browser is installed on a file server and in a.
Links and Comments.
Technical Tips and Tricks for User Support Mike Gardner
Server-Side vs. Client-Side Scripting Languages
Its easy to be an information provider Tutorial: Web Publishing.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Web Publishing TutorialIS for Management1 Tutorial: Web Publishing.
Dynamic Web site With PHP and MySQL. MySQL The combination of MySQL database and PHP scripting language is optimum for building dynamic websites. MySQL.
How the World Wide Web Works
Microsoft ® Office Word 2007 Training Mail Merge II: Use the Ribbon and perform a complex mail merge [Your company name] presents:
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
1 Web Design Workshop DIG 4104c Spring 2014 Dr. J. Michael Moshell University of Central Florida Lecture 2: The git source control system
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
1 DIG 3134 – Lecture 3 Forms Michael Moshell University of Central Florida Media Software Design.
Web Sites for amateur radio. So You want to make a Web Site? There are several things you need to know about web sites before you start to think about.
1 Homework / Exam Exam 3 –Solutions Posted –Questions? HW8 due next class Final Exam –See posted schedule Websites on UNIX systems Course Evaluations.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
PHP : Hypertext Preprocessor
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
INTERNET APPLICATION DEVELOPMENT For More visit:
ACM Web Development Workshop - PHP By Luis Torres.
CPSC 203 Introduction to Computers Lab 21, 22 By Jie Gao.
1 John Magee 9 November 2012 CS120 Lecture 17a: Publishing Web pages.
Week 7. Lecture 3 PHP Forms. PHP forms In part 2 of this course, we discussed html forms, php form is similar. Lets do a quick recap of the things we.
Programming in HTML.  Programming Language  Used to design/create web pages  Hyper Text Markup Language  Markup Language  Series of Markup tags 
University of Sunderland CDM105 Session 5 Web Authoring Tools The past and present A history of web authoring tools and an overview of Macromedia Dreamweaver.
Tutorial: Web Publishing. Web Publishing Tutorial IS for Management2 Web Publishing Web pages are stored on a Web server: a computer with a unique Web.
XP Dreamweaver 8.0 Tutorial 3 1 Adding Text and Formatting Text with CSS Styles.
Programming in HTML.  Programming Language  Used to design/create web pages  Hyper Text Markup Language  Markup Language  Series of Markup tags 
 The World Wide Web is a collection of electronic documents linked together like a spider web.  These documents are stored on computers called servers.
PHP - Part 2. More operators... Arithmetic and Assignment Operators e.g., using + and =  $IntA=5; $intB=8; $intC=$intA+$intB; //intC is 13  // Same.
Faculty Webpage Design Minimum Requirements. Go to: then High Schoolhttp://gcsc.groupfusion.net/
1 DIG 3134 – Lecture 11: Two Dimensional Arrays And Dynamic Controls AND CONSTANTS Michael Moshell University of Central Florida Internet Software Design.
The Control Panel is the starting point when you wish to load files into Blackboard. Students cannot see this panel, unless they know your password of.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
INFO1408 Database Design Concepts Week 15: Introduction to Database Management Systems.
MS Power point Tutorial
1 DIG 3134 – Lecture 4: Arrays and Strings Michael Moshell University of Central Florida Media Software Design.
1 DIG 3134 Lecture 9. Dynamic HTML Controls Michael Moshell University of Central Florida Media Software Design.
1 DIG 3134 – Lecture 14 MySQL and PHP Play Together Michael Moshell University of Central Florida Media Software Design.
Tutorial 3 Adding and Formatting Text with CSS Styles.
1 DIG 3563: Lecture 2a: Regular Expressions Michael Moshell University of Central Florida Information Management.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
MGS 351 Introduction to Management Information Systems RECITATION 12.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Python Let’s get started!.
1 DIG 3134 Lecture 6: Maintaining State Michael Moshell University of Central Florida Media Software Design.
PHP Form Processing * referenced from
College:The First Year Demo Template Only ENTER. Table of Contents.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Here are some things you can do while you wait 1.Open your omeka.net site in your browser (e.g. 2.Open.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Intro to WordPress (Using XAMPP)
Weebly Elements, Continued
Landscape Institute Introducing the new Branch Websites
Matlab Training Session 4: Control, Flow and Functions
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Arrays and files BIS1523 – Lecture 15.
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Intro to PHP & Variables
Learning Objective LO: We’re learning to understand when it is appropriate to use particular data types.
PHP and Forms.
Presentation transcript:

1 DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida Media Software Design

2 The Objectives: Learn how to read a text file into a PHP script. Learn how create a text file from data in a PHP script.

Why do I care about Files? Lots of data is available in text files Price tables Customer lists Addresses, lists Setting up a complex program is often easier if you use a file. Example: Wheel of Fortune: -- read the cliche’s from a text file -- easily modified to improve gameplay Jaindiamondtools.com

File Paths: Relative $filename1="cats.txt"; // file is where script is. $filename2="images/cat1.jpg" // file is in sub-directory $filename3="../species/persian.txt" // in SIBLING directory scripts myprogram.php  this script contains that stuff cats.txt images cat1.jpg petstore.php species persian.txt Fulltimewow.blogspot.com directories are shown in italics

File Paths: absolute, on PC: $filename1="c:/wamp/www/cats.txt"; // file is where script is. $filename2="c:/wamp/www/images/cat1.jpg" // file is in subdir. $filename3="c:/wamp/www/species/persian.txt" // in SIBLING dir. c: wamp www  docroot - - where Apache looks for html & php scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt Fulltimewow.blogspot.com

File Paths absolute: Mac $filename1="/Applications/MAMP/htdocs/cats.txt"; $filename2="/Applications/MAMP/htdocs//images/cat1.jpg"; $filename3="/Applications/MAMP/htdocs//species/persian.txt"; Applications MAMP htdocs  docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt Fulltimewow.blogspot.com

File Paths absolute: Unix/Linux (e. g. Sulley) $filename1="/home/faculty/moshell/public_html/scripts/cats.txt"; $filename2="/home/faculty/moshell/public_html/scripts/images/cat1.jpg"; $filename3="/home/faculty/moshell/public_html/species/persian.txt"; /home faculty moshell public_html  docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt

File Paths absolute: Unix/Linux (e. g. Sulley) $filename1="/home/student/aa34567/public_html/scripts/cats.txt"; $filename2="/home/student/aa34567/public_html/scripts/images/cat1.jpg"; $filename3="/home/student/aa34567/public_html/species/persian.txt"; /home student aa34567 public_html  docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt

Why would we need absolute paths? Example: -Perhaps you host websites for three different companies, but all three need access to common tax tables. -The relative paths from the boutiques, and from Pandaboots, is different. So, all could use this absolute path: Docroot PandaBoots$file="c:/wamp/www/taxtables/federal.txt" boutiques library.forboutiques Sammys Tanqueria taxtables federal.txt Fulltimewow.blogspot.com

10 Code for use on both PC and Mac 1.Use relative paths. Then everything starts from 'docroot' BUT if your system needs absolute paths, do it this way: //$Docroot="c:/wamp/www/";// PC version $Docroot="/Applications/MAMP/htdocs/";// Mac version $file=$Docroot."taxtables/federal.txt"; Fulltimewow.blogspot.com

11 Code for use on both PC and mac 1.Use relative paths. Then everything starts from 'docroot' BUT if your system needs absolute paths, uncomment the right one: //$Docroot="c:/wamp/www/";// PC version $Docroot="/Applications/MAMP/htdocs/";// Mac version $file=$Docroot."taxtables/federal.txt"; Why not "$Docroottaxtables/federal.txt"; ? Because PHP will look for a variable $Docroottaxtables and come up with nothing. Fulltimewow.blogspot.com

12 Aside: why $Docroot not $docroot? I use a convention about global variables: Start their names with $Capital letters. It helps to make global variable instantly recognizable. Anything that helps my poor old memory is a good thing!! Fulltimewow.blogspot.com

13 A better way to switch systems $Computing_environment='pc'; // or 'mac' or 'sulley'; //.. And somewhere far below, in the code: if ($Computing_environment=='pc') $Docroot="c:/wamp/www/"; else if ($Computing_environment=='mac') $Docroot="/Applications/MAMP/htdocs/"; else if ($Computing_environment=='sulley') $Docroot="/home/faculty/moshell/public_html/"; else print "Error 221: unknown computing environment was ". "specified:$Computing_environment"; Fulltimewow.blogspot.com

14 What's a text file? It consists of a series of lines of text with some kind of a line separator character (one or more) at the end of each line. The character used is different on PC and on MAC. But PHP will normally 'smooth out' the difference and hide it. The "official character" for line separation is a newline, which can be represented by "\n". Text files can contain numbers, letters and special characters. An important invisible character is a TAB or "\t". Lib.utexas.edu

15 A Mac-specific problem Mac treats end-of-line differently. To make sure that the 'file' function reads ALL the lines of a text file, put this at the head of your code: <?php session_start(); // this should be there anyway ini_set ("auto_detect_line_endings", true);

16 Our design goal: Make a simple program that Displays a text Accepts one new line of input Writes out the text including the new line. The program will have only two screens: Screen 1: display the text, and two buttons. UPDATE and QUIT. Screen 2: If QUIT was selected, report that the text was saved. Lib.utexas.edu

17 Step 1: Build a text-reading function #textloader: function textloader($whatfile) { if (!file_exists($whatfile)) { print "Sorry, can't find $whatfile."; exit; } else { $textarray=file($whatfile); return $textarray; } }# textloader EXAM ALERT: Look up and UNDERSTAND Each of the Green built-in functions

18 Step 2: Build a text-display function #textdisplay: function textdisplay($intextarray) { // each item in the $textarray is one line of text. $linecount=count($intextarray) for ($i=0; $i<$linecount; $i++) print $intextarray[$i]." "; }# textdisplay

19 Step 3: Get that much working (filedemo1.php) Then build a file writing function. (NOTE: Filedemo1 and filedemo2 in ONE text file named filedemo.txt, with filedemo3 too!) function textsaver($whatfile,$whattext) { $fp = fopen($whatfile, 'w'); // the 'w' means 'write over the previous contents.' $arraysize=count($whattext); for ($i=0; $i<$arraysize; $i++) fwrite($fp, $whattext[$i]); fclose($fp); } # textsaver

20 Putting it all together: Examine filedemo2.php Some useful functions like makeheader, makefooter The file i/o functions The classical if-then-else chain *** you may steal any or all of this code for use in your Wheel of Fortune program **** >>>> You must understand everything in this program, for mastery of the Midterm Exam <<<<<

21 Another example:studentlist.php I have a class roster (list of student names and NIDS) I want a class link-list (HTML document to link me to each person's Sulley account) via URLs like So I created studentlist.php Examine this code... understand how it works  MTX Material!

22 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: $thecharacters locationvalue 0W 1o 2m 3b 4a 5t 6s 7 8h etc

23 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: $thecharacters locationvalue 0W 1o 2m 3b 4a 5t 6s 7 8h etc here’s one way: for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i]=substr($theline,$i,1); Advantage: I built this because I didn’t know any automatic way to do it.

24 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: $thecharacters locationvalue 0W 1o 2m 3b 4a 5t 6s 7 8h etc here’s another way: $thecharacters=str_split($theline); Disadvantage: you have to know about str_split, a built-in function.

25 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: $thecharacters locationvalue 0W 1o 2m 3b 4a 5t 6s 7 8h etc A useful test function: print_r ("Print recursive") $thecharacters=str_split($theline); Testing the concept: the print_r function. print_r($thecharacters); Array ( [0] => W [1] => o [2] => m [3] => b [4] => a [5] => t [6] => s [7] => [8] => h [9] => a [10] => v [11] => e [12] => [13] => n [14] => o [15] => [16] => w [17] => i [18] => n [19] => g [20] => s )

26 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: Disadvantage of str_split: What if you wanted to push the character in string position 0 into the array at location 1?  Like THIS? $thecharacters locationvalue 1W 2o 3m 4b 5a 6t 7s 8 9h etc

27 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: Disadvantage of str_split: What if you wanted to push the character in string position 0 into the array at location 1?  Like THIS? $thecharacters locationvalue 1W 2o 3m 4b 5a 6t 7s 8 9h etc for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i+1]=substr($theline,$i,1); Advantage: I built this code, I understand it, I can make it do whatever I want!

28 One final problem: If you have a string and you need an array... what to do? example: $theline= ”Wombats have no wings”; positions: ” ”; but we want: RECOMMENDATION: Use this kind of code in your project2 to translate one line of your text file into the $letters array. $thecharacters locationvalue 1W 2o 3m 4b 5a 6t 7s 8 9h etc for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i+1]=substr($theline,$i,1); Advantage: I built this code, I understand it, I can make it do whatever I want!

29 Step by Step, building Project 2 You are of course free to build it in any way you want. If you need a hint, here are some suggested steps. Start with "Wheel of Fortune" starter kit Add a copy of textloader & print out the text (to prove it works) Add a session variable named $linenumber which recycles (keeping its same value) except when the NEXT PHRASE button was clicked. (Print this out, to make sure that NEXT PHRASE is working.) Now use $linenumber to select a line from the stored text. like $line=$mytext[$linenumber]; Finally, add an error message when there is no more text.