Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Applications Spring 2008. Review Last week –XHTML, Webservers.

Similar presentations


Presentation on theme: "Internet Applications Spring 2008. Review Last week –XHTML, Webservers."— Presentation transcript:

1 Internet Applications Spring 2008

2 Review Last week –XHTML, Webservers

3 This week Tech-topic presentations - XML/RSS Introduction to programming

4 XML DC record Alliance of Baptists Alliance of Baptists Records, 1987 - 2001 en 2.1 linear feet [Lots of text omitted] Collection is open. Alliance of Baptists Southern Baptist Alliance Z. Smith Reynolds Library, Wake Forest University text/xml Alan P. Neely Papers, Z. Smith Reynolds Library, Relation> http://zsr.wfu.edu/collections/digital/ead/allianceofbaptists.xml

5 Encoding Schemes (XML) Required syntax –Document type declaration Optional but useful –Processing instruction –Elements notation All elements must be closed or –Attributes Attributes must be enclosed in “” –Text fieldcontent

6 Encoding Schemes (XML) - 2 File attributes –Elements are repeatable (if allowed by DTD/Schema) –An XML file can contain multiple “records” –Certain characters like need to be represented by escape sequences: & = & <= < > = >

7 Encoding Schemes (XML) - 3 Sample RSS File http://urltofile.xml This is a sample en-us Tue, 10 Jun 2003 04:00:00 GMT …

8 XML Components XML data model definition –Document Type Definitions (DTD) –XML Schema –Application Profiles XSL processing instructions –Transformation to new format –Powerful enough to serve as a simple application development platform

9 RSS Example NYT > Media and Advertising http://www.nytimes.com/pages/business/media/index.html?partner=rssnyt Tue, 26 Feb 2008 13:05:01 GMT Nielsen Looks Beyond TV, and Hits Roadblocks Nielsen wants households to let it eavesdrop on Web surfing and cellphone use, but it is finding it a tough sell. LOUISE STORY http://www.nytimes.com/2008/02/26/business/media/26nielsen.ht ml Tue, 26 Feb 2008 12:46:15 GMT http://cyber.law.harvard.edu/rss/rss.html

10 DOM Example A Hierarchy of elements Family based inheritance Parent Child Sibling Element access by name document.getElementById("You")

11 Programming Definitions Concepts A programming framework

12 Definitions Programming Language “A formal language used to write instructions that can be translated into machine language and then executed by a computer.” (definitions)definitions Scripting Language Run-time (does not require compilation) Restricted context (requires a specific environment) Functional / Object oriented Definitions Compiler / Interpreter A program that builds and executes a program. Compilers create a self-executable file, interpreters read a text script at run-time

13 Programming approaches Logical/structural programming Stream of consciousness Starts at line 1 Procedural programming Uses functions, sub-functions, subroutines Encapsulation, modularization Object-oriented programming Further encapsulation Uses concepts of inheritance, modularity

14 The programming process Analyze the problem What do you want your program to do? What are your users expecting, what data do you have? Plan program flow/logic What steps need to occur, in what order? Useful tools include Step-Form, flowcharts, and pseudocodeflowcharts Code the program Create variables, routines, functions Compile/run the program Test, verify Release

15 Algorithms “An effective procedure for solving a problem in a finite number of steps.” Sample Algorithm for an email form (Step Form) –Begin If form data is present then continue processing –Get data from form (Subject, note, etc) –If the subject doesn’t contain bad stuff then continue processing »Write subject, note to email function »Send email »If Email sent successfully then tell user that it did, otherwise output the error code –End

16 Algorithm elements Processes / Sequences Actions are ordered according to need Decision making / Selection If...Then...Else –If today is Friday then go home early –If username = mitcheet then allow access Repetition / Iteration / Looping While –While the database returns data, print it out Foreach –For Each piece of data returned, write it to a file Variables Placeholders for information to be used by program Often “initialized” with specific values (such as 0”

17 Creating an Algorithm Investigate –Identify a specific process (sending email) –Identify the major decisions (presence of data, appropriateness of data) –Identify the loops What needs to happen several times? –Identify variables Lay out the algorithm –Design a sequence of steps incorporating the decisions from step 1. Make changes as necessary Refine algorithm –Implement changes noticed during run-through –Group processes, variables

18 Decision making Single-Alternative / unary outcome –If then Dual-Alternative / binary outcome –If then else Multi-Alternative /x-ary outcome –If then elsif elsif elsif –Switch case statements Switch case1: case2: default:

19 Flowchart Examples Start / End Data Decision Making A process

20 Pseudo code Input/output –Input, Read, Display Iteration –Repeat Until, Dowhile /end dowhile, for/end for Decision –If then else –If <> then <> elsif <> elsif <> endif Processing –Add(+), subtract(-), compute, compare ( ), set Subroutines (functions/sub-functions) –Use to define sub processes: EMAILTHIS: –Input email, subject, note –Send email –Set send result to output variable X – return X Include in your pseudo code with a call statement –Call EMAILTHIS (email, subject, note)

21 PHP Overview Standard PHP Syntax review –Structure, operators, Constructs needed for today –Variables Scalar, Arrays, Objects –Output Echo –Loops Foreach, for($i) –Built-in Functions DomXML functions

22 Language elements (Syntax) Syntax – grammar, order, structure of program –PHP syntax example <? PHP Function () { stuff } $variablename; End of Line markers (;) //comments ?> –Syntax has to be perfect! –PHP is case specific

23 Standard PHP program <?php //Beginning element Example statement;//Use a semicolon to end lines $myvalue;//A scalar variable $myarray[1];//An array with value 1 returned $myObjectref->function()//A function in an object $myvalue = “hello”;//Variable Assignment echo (“stuff”);//A standard PHP Function ?>//Ending Element

24 Variables Text –Strings Numbers –Integers (whole numbers) –Floating point – (decimal numbers) Boolean –True/False

25 Variables – single value Scalars – Single value variables –Strings - $username = “mitcheet” –Numbers $cost = 55.00 –Boolean $ready = True

26 Variables – multiple values Arrays – Multi-value variables –Grouped in numerical order $email[1] = “mitcheet@unc.edu” $email[2] = “burrohj@unc.edu” –Grouped with text $email[1][“username”] = “mitcheet@wfu.edu” $email[1][“realName”] = “Erik Mitchell” –General syntax $email = array ( key=>value, key=>value) Arrays can be nested (think hierarchy)

27 Objects Objects components –Methods (Functions) –Fields (Variables) PHP syntax for objects –$myObjectReference->fieldvalue –$myObjectReference->objectFunction() DomXML Example –$dom = domxml_open_file("http://....xml"); –$rsstag = $dom->get_elements_by_tagname("rss");

28 PHP Variable Operators http://www.w3schools.com/php/php_operators.asp

29 PHP Comparison Operators http://www.w3schools.com/php/php_operators.asp

30 Variable scope Depending on where you initialize a variable, impacts what functions can use it –A variable initialized at the beginning of your file is “global” –A variable initialized within a function is limited to the function.

31 PHP Output functions echo ‘my output text’; –You can use ‘ or “ but they have special meaning echo ‘my output’.$variable.’my output’ –Use the. To concatenate strings Debugging output –print_r ($complexvariable) Prints a user readable array/object

32 Exercise 1 Sample Program –http://www.ils.unc.edu/~mitcheet/class_7ex ercises/class7_ex1.txthttp://www.ils.unc.edu/~mitcheet/class_7ex ercises/class7_ex1.txt –Type in this program –Upload to Ruby –Run and look at sourcecode –Try to create an echo statement that outputs specific values of the variable echo $associativeArrayVariable[‘title’]

33 Looping Definition Loop structures allow re-execution of instructions with multiple sets of data Examples Writing records from a database query onto a webpage Calculating cost, discounts, shipping on items in a shopping cart Comparing values to make decisions Benefits declare logic and operational statements once & re-use Loops are the building blocks of structured programming Use a ‘main’ loop to control the program

34 Loop structures Components Loop control variable –the variable that keeps changing ($i for example) Sentinel value –the value which signals the end of the loop Loop control structures –Do while, While, for, foreach Example for($i=1; $i<=100; $i++) { echo “hello world! ”; } for() { }Control structure $i = 1Variable declaration $i < 100Limit declaration $i++Increment declaration echo “”;operational statement

35 Nesting Use a mix of flow-control and decision making functions to create complex processes If -> then -> else Switch -> case -> default For -> next Do -> while

36 Functions Definition –“A sequence of instructions for performing a specific task” (freedictionary)freedictionary Benefits –Modularization/Abstraction –Code re-use –Variable management (global, local) –Easier to troubleshoot and maintain Key concepts –Global variables vs local variables –Parameters –Returned values

37 PHP Functions Examples –Phpinfo(), for(), foreach(), echo....... Contents –Name, Parameters, operations, return values function myFunctionName (parameters) { parameters; operations; return variables; } Declaration { Parameters passed to function Operations (calculate, lookup, etc) Return values to rest of program }

38 The XMLParse object SimpleXML –Standard in PHP5, but we are using PHP4  –Today, we are relying on some extra code to make things simple! What it does –Reads in an XML file and breaks the DOM into an array of variables –Provides us methods to access XML values http://www.criticaldevelopment.net/xml/doc.php

39 XMLParse Example <?php // Include the code that makes simplexml work require('./php_4_simplexml.php'); //Get the XML document loaded into a variable $xml = file_get_contents('http://www.nytimes.com/services/xml/rss/nyt/Music.xml'); //Set up the parser object $parser = new XMLParser($xml); //Work the magic... $parser->Parse(); //Echo the plot of the first echo $parser->document->rss[0]->channel[0]->title[0]->tagData; //Echo the plot of each title foreach($parser->document->channel[0]->item as $item) { echo $item->title[0]->tagData; } ?>

40 XMLParse Example (2) Mixed array/object syntax –$parser->document->channel[0]->item Available functions for an object –tagData –tagParents –tagChildren –tagAttrs –tagName

41 Exercise 2 In this example, we will create a simplexml object and process some data –Get the SimpleXML file and and upload it to your webspace –Write the simplexml example program

42 Exercise 3 / homework Today we will add RSS feed parsing functionality to our webpage from last week –Create a program outline using pseudocode/flowcharting –Using the XMLParse functions, write the program in php –Program requirements Use XMLParse or another solution (DOMXML is much more complicated but uses only embedded php functions) Your program should be able to output multiple RSS feeds (looping). Your program should only show the first 10 elements of the feed and provide a link to view more

43 Next week Technology Topics CSS/XSLT More on PHP


Download ppt "Internet Applications Spring 2008. Review Last week –XHTML, Webservers."

Similar presentations


Ads by Google