Programming for the Web Data Structure Writing to a file Dónal Mulligan BSc MA

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

PHP Form and File Handling
UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
1 PHP Storing and Retrieving Data. string fgets(resource handle [, int length]) Reads and returns one line from a file; moves file pointer to next line.
More on PHP Coding Lab no. 6 Advance Database Management System.
PHP Workshop ‹#› File Handling with PHP. PHP Workshop ‹#› Files and PHP File Handling –Data Storage Though slower than a database –Manipulating uploaded.
Chapter 10.
Website Development Tutorial 3. Outline Getting data from the user using forms Sending data from a form to a PHP program Writing the data to a file for.
IS 1181 IS 118 Introduction to Development Tools Week 2.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)Slide Topic Files.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Intermediate PHP (2) File Input/Output & User Defined Functions.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
ITM 352 © Port,KazmanFile I/O - 1 File I/O in PHP Persistent Data.
Web Programming 02 – PHP File and Directory Handling Aryo Pinandito, ST, M.MT.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
BBK P1 Module2010/11 : [‹#›] File Handling with PHP.
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
Website Development with PHP and MySQL Saving Data.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
PHP Constructs Advance Database Management Systems Lab no.3.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Storing and Retrieving Data
5 1 Data Files CGI/Perl Programming By Diane Zak.
Introduction to File Processing with PHP - Part 2 Indexed Files.
Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
PHP Programming.
PHP 4 Files, Functions. Opening a File The fopen() function is used to open files in PHP. The first parameter of this function contains the name of the.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
LECTURE 4 Files, File open, Read, Write. File Upload - In this lecture we will teach you how to open, read, and close a file on the server. - PHP Open.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
Files Tutor: You will need ….
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
12 – PHP Contd. Informatics Department Parahyangan Catholic University.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 CSE 2337 Chapter 7 Organizing Data. 2 Overview Import unstructured data Concatenation Parse Create Excel Lists.
PHP Form Processing * referenced from
NMD202 Web Scripting Week2. Web site
Files A collection of related data treated as a unit. Two types Text
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Introduction to File Processing with PHP. Review of Course Outcomes 1. Implement file reading and writing programs using PHP. 2. Identify file access.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 6 Persistence-Saving and Retrieving Data
CGS 3066: Web Programming and Design Spring 2016
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
File Handling with PHP.
Lecture 13 Input/Output Files.
Topics Introduction to File Input and Output
<?php require("header.htm"); ?>
Files Handling In today’s lesson we will look at:
Files.
File I/O in PHP Persistent Data.
Topics Introduction to File Input and Output
CHAPTER 21 LOOPS 1.
Topics Introduction to File Input and Output
Introduction to Computer Science
Presentation transcript:

Programming for the Web Data Structure Writing to a file Dónal Mulligan BSc MA

Data structure Data being manipulated, saved or displayed should be structured in a meaningful way to allow efficiency – Arrays, CSV files, indexed databases Structured data allows easy retrieval, manipulation and provides a template for appending further data to store

Delimiting Delimiters are the separators of data values in a list This can be any character: – Space: one two three four five – %: one%two%3%4%five Most commonly a comma is used as a delimiter (Comma Separated Values – CSV files)

Exploding delimited data The PHP explode() function can be used to take a string with delimited data and convert it to an array of separate strings The function requires two parameters; the delimiting character and the string $str = “one two three four five”; $str_array = explode(" ", $str); echo $str_array[4]; // = five

Imploding an array to string Similarly the implode() function takes an array of values and creates a string with a specified delimiter separating each. The function requires a parameter for the delimiting value (the “glue”) and an array to operate on: $int_array = array(3,1,4,1,5,9); $csv_string = implode(“,", $int_array); Echo $csv_string;

Comma Separated Values A file type consisting of lines of related items of data separated by a comma Each new entry on a new line (\n) CSV files can be saved from excel spreadsheets, etc An example of delimited data: Smith, John, McName, Betty, Fakey, Jimmy,

Resulting CSV (4 fields) Brennan, Cody, Delahunty, Delaney, Doherty, Garvan Doyle, Shane Foley, Mark Hampson, Harper, Keyes, Aisling Kiernan, Loftus, Mc Call, Mc Glone, Kevin Mc Leod, O Mhurchu, Cillian Parnell, Ciaran Parnell, Gavin Rattigan, Rutledge, Gary Smyth, Strong, Toribio-Roura,

Control structures (loops) Structured values in arrays can be displayed or manipulated using control structures to move from value to value – Loops are particularly useful for iterating over arrays of values efficiently – For – While – foreach

for loop for loops require a starting parameter for the loop check value, a condition to end and an increment: for($i = 0; $i < sizeof($array); $i++) { echo $array[$i]; echo " "; }

while loop A while loop continues looping while a given condition is true, checking the condition at the start of each run The variant do-while loop is similar but checks at the end of each run $i = 0; while ($i < sizeof($array)) { echo $array[$i]; echo " "; $i++; }

foreach loop One of the simplest loop types, a foreach takes each value in an array in turn and uses it as a specified variable foreach($array as $f) { echo $f; echo " "; }

File stream A file stream can be opened to access an external file for reading or writing The fopen() function opens the stream and expects two parameters, a path and mode: $stream= fopen(“myfile.txt”, w); Full list of modes is available on php.net: The resource must be closed after use via the fclose() function: fclose($stream);

Reading from a file fread() reads content from an open file stream resource, up to a given length or until the end of file (EOF) is reached: $filename = "something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);

Writing to a file The content of a string can be written to a file using the fwrite() function The written data occurs at the place in the file where the pointer currently resides (See php.net for details on how modes affect this) Writing multiple times to a file stream appends the string each time: $handle = fopen('data.txt', 'w'); fwrite($fp, ‘Hello '); fwrite($fp, ‘My name is Dónal'); fclose($fp);