Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming for the Web Data Structure Writing to a file Dónal Mulligan BSc MA"— Presentation transcript:

1 Programming for the Web Data Structure Writing to a file Dónal Mulligan BSc MA donal.mulligan@dcu.ie

2 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

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

4 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

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

6 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, johnsmith@gmail.com McName, Betty, betty09@hotmail.com Fakey, Jimmy, fakeyj@something.net

7

8 Resulting CSV (4 fields) Brennan, Eoin,MMA3,eoin.brennan23@mail.dcu.ie Cody, Eoin,MMA3,eoin.cody2@mail.dcu.ie Delahunty, Shane,MMA3,shane.delahunty2@mail.dcu.ie Delaney, Aidan,MMA3,aidan.delaney7@mail.dcu.ie Doherty, Garvan Gerard,MMA3,garvan.doherty5@mail.dcu.ie Doyle, Shane Anthony,MMA3,shane.doyle48@mail.dcu.ie Foley, Mark Alexandre,MMA3,mark.foley24@mail.dcu.ie Hampson, Laura,MMA3,laura.hampson2@mail.dcu.ie Harper, Graham,MMA3,graham.harper2@mail.dcu.ie Keyes, Aisling Geraldine,MMA3,aisling.keyes2@mail.dcu.ie Kiernan, Richard,MMA3,richard.kiernan2@mail.dcu.ie Loftus, Daniel,MMA3,daniel.loftus4@mail.dcu.ie Mc Call, Deirdre,MMA3,deirdre.mccall2@mail.dcu.ie Mc Glone, Kevin Imran,MMA3,kevin.mcglone2@mail.dcu.ie Mc Leod, Alex,MMA3,alex.mcleod3@mail.dcu.ie O Mhurchu, Cillian Aodan,MMA3,cillian.omurchu3@mail.dcu.ie Parnell, Ciaran James,MMA3,ciaran.parnell2@mail.dcu.ie Parnell, Gavin James,MMA3,gavin.parnell2@mail.dcu.ie Rattigan, Robert,MMA3,robert.rattigan2@mail.dcu.ie Rutledge, Gary Michael,MMA3,gary.rutledge3@mail.dcu.ie Smyth, Jack,MMA3,jack.smyth7@mail.dcu.ie Strong, David,MMA3,david.strong2@mail.dcu.ie Toribio-Roura, Esther,MMA3,esther.toribioroura2@mail.dcu.ie

9 Control structures (loops) Structured values in arrays can be displayed or manipulated using control structures to move from value to value – http://ie.php.net/manual/en/language.control-structures.php http://ie.php.net/manual/en/language.control-structures.php Loops are particularly useful for iterating over arrays of values efficiently – For – While – foreach

10 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 " "; }

11 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++; }

12 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 " "; }

13 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: http://ie.php.net/manual/en/function.fopen.php The resource must be closed after use via the fclose() function: fclose($stream);

14 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);

15 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);


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

Similar presentations


Ads by Google