1 / 25

Advanced PHP & RSS

Advanced PHP & RSS. Utilizing XML, RSS, and PHP. XML ( e X tensible M arkup L anguage). XML is the language of all RSS feeds and subscriptions XML is basically a way of separating data from aesthetics XML is a “poor man’s” database XML is similar to HTML, but you get to create the tags

Download Presentation

Advanced PHP & RSS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced PHP & RSS • Utilizing XML, RSS, and PHP

  2. XML (eXtensibleMarkup Language) • XML is the language of all RSS feeds and subscriptions • XML is basically a way of separating data from aesthetics • XML is a “poor man’s” database • XML is similar to HTML, but you get to create the tags • “Self-describing” data

  3. XML Presentation • XML can be used to present content in different formats: • XHTML/HTML, PDF, Graphics, Mobile Devices, Text, MS Office

  4. XML Syntax • <order> ELEMENT <sold-to> CHILD ELEMENT <person ID=“123”> ATTRIBUTE … VALUE <lastname>Matthews</lastname> <firstname>Darell</firstname> TEXT (Darell) </person> </sold-to> <sold-on>20120904</sold-on> <item> START TAG <price>29.95</price> <book> <title>JavaScript Lessons</title> <author>Neilson, Jason</title> </book> </item> END TAG</order>

  5. XML as a Database • XML data organized into elements, attributes, and values (similar to tables, fields, and elements) • Works great for storing around 50 or less attribute/value pairs • Often used in JS slideshows (1 XML for settings, 1 for photos used) and Flash animations (1 XML for settings telling the flash movie what to do)

  6. XPath • XPath is a language which allows us to quickly access information in an XML document based on our knowledge of its structure • /child::student/child::student[$number=‘0001’] • Example: • movie.xml, movie.php, actor.php

  7. XML Address Book • Create addressbook.php, which lists your name as an <h1>, then a horizontal rule • Create addressbook.xml, which will include 5 people and their contact information (name, address, city, state, zip, phone, email) • Create a drop-down list populated with names pulled from addressbook.xml using SimpleXMLElements. Once the user selects a name, they click “Search” to bring up that person’s contact information on a page called contact.php. • The contact.php page should display that person’s contact information, again being pulled from the XML file using the API, in an organized table. • All PHP pages should be styled (in any manner you wish) using an external CSS file.

  8. XML Address Book • contact.php • addressbook.php

  9. SimpleXML • PHP API that allows PHP to interact with XML • Create an XML document, then use PHP to parse the document for filtering / querying. • SimpleXMLElement is the PHP parsing function • Since RSS feeds use XML to describe data in the feed, we can use PHP to parse that feed for our usage, all we need is the feed address and some knowledge of the XML structure. • Item, title, description, link • Podcasts are RSS feeds with attachments

  10. Feed Parsing • View the page http://sampsoncctech.info/cis115/feed • View the XML source code (notice the element, child elements, attributes, and values) • Most RSS feeds follow the same general structure and element/attribute naming convention • Use PHP to grab the contents of the feed and place them into an HTML document for styling<?php $now = time(); $xml = new SimpleXMLElement(file_get_contents(“http://sampsoncctech.info/cis115/feed”));?>

  11. Feed Styling and Output • Once you have PHP gathering the feed data, now use HTML and PHP to style the output • simplexmlelementdemoCIS115.php • PHP “foreach” statement to loop and grab each feed article and put it into a list where the title is a link to the article<?phpforeach ($xml -> channel -> item as $item) { ?><li><a href="<?php echo $item -> link; ?>"><?php echo $item -> title; ?></a></li><?php } ?>

  12. Parsing Other RSS Feeds • Find an RSS feed, podcast, or vodcast online with an accessible XML feed source page • Use PHP to gather the feed data, then a “foreach” statement to parse each child element of the XML feed • Use HTML to style the output of the PHP parsing into an ordered or unordered list

  13. PHP Data Types & Operators • PHP doesn’t require you to specify data type • You can initialize and declare variables in one line$variable = “some value or string”;echo $variable; • Data Types: • String, Integer, Floating-Point, Exponential, Boolean, NULL

  14. PHP Arrays • Contains a set of data represented by a single variable • Array names are often referred to with array operators ([ ]) at the end of the name$variable = array(“element1”, “element2”, “element3”);$variable[] = “element1”; • Declare an array with $variable = array(values): and access elements within the array by using $variable[index];

  15. PHP Array Example <?php $concerts = array( “Rihanna”, “Taylor Swift”, “Kati Perry”, “Maroon 5”); $concerts[ ] = “Ellie Goulding”; //adds more elements to the end of the array $concerts[2] = “Flo Rida”; //alters or replaces existing array elements echo “<p>The following “, count($concerts), “ concerts are scheduled: </p><p>”; echo “$concerts[0]<br>”; echo “$concerts[1]<br>”; echo “$concerts[2]<br>”; echo “$concerts[3]</p>”;?>

  16. PHP Expressions • Expression: a literal value or number • Operands: variables and literals contained in an expression • Literals: values such as a string or number • Operators: symbols such as +, -, *, / that are used in expressions to manipulate operands • PHP Operator Types: array, arithmetic, assignment, comparison, logical, special, string

  17. Building PHP Expressions • Arithmetic Binary Operators: +, -, *, /, % (modulus) • arithmeticbinaryoperators.php • Arithmetic Unary Operators: ++ (increment), -- (decrement) • arithmeticunaryoperators.php, 100countdown.php, 100countup.php • Logical Operators: && (AND), || (OR), ! (NOT) • logicalexamples.php

  18. PHP Cash Register • cashregister.html • cashregister.php

  19. Type Casting in PHP • Type casting: a way to ensure that a variable is of the correct type$SpeedLimitMiles = “55 mph”;$SpeedLimitKilo = (int) $SpeedLimitMiles * 1.6;echo “$SpeedLimitMiles equals $SpeedLimitKilo.”; • To test whether a variable is of a given data type, you should instead use an “is_*()” function is_numeric(), is_string(), is_int(), is_double()

  20. Comparison Operator Example • Comparison Operators:<?php $storedpassword = “mypassword”; $enteredpassword = “mypassword”; if ($storedpassword == $enteredpassword) { print “The entered password is correct!”; } else { print “The entered password was wrong!”; }?>

  21. Logical Operator Example • Logical Operators:<?php $storedusername = “user”; $enteredusername = “user”; $storedpassword = “mypassword”; $enteredpassword = “mypassword”; if ($storedusername == $enteredusername AND $storedpassword == $enteredpassword) { print “The entered username and password are correct!”; } else { print “The entered username and password were wrong!”; }?>

  22. PHP Do … While Loop • Do…While (perform an action while a condition is being met)<?php $num = 1; do { echo “The number is: $num<br>”; $num++; } while (($num > 0) && ($num < 400));?>

  23. PHP If…Else…If Conditional • If…Else…If (if this happens, do this, else/otherwise, do this)<?php $mood = “sad”; if ($mood == “happy”) { print(“Oh, you’re happy”); } elseif($mood == “sad”) { print(“Oh, you’re sad”); } else { print(“What are you then?”); }?>

  24. PHP For Loop • For (perform this action for a fixed number of times)<?php for ($counter = 1; $counter <= 10; $counter ++) { print(“$counter times 2 is “. ($counter * 2). “<br>”; }?>------------------------------------------------------------------------------------------------------------<?php for ($counter = -4; $counter<=10; $counter++) { if($counter==0) { break; //can also use “continue;” to skip 0 } else { $temp = 4000/$counter; echo “4000 divided by $counter is... $temp<br>”; } }?>

  25. PHP Nested For Loop • Nesting one for loop inside of another to perform vertical and horizontal actions<?php //Multiplication Table echo “<table style=‘border: 1px solid black;’> \n”; for ($y=1; $y<=12; $y++) { echo “<tr> \n”; for ($x=1; $x<=12; $x++) { echo “<td style=‘border: 1px solid black; width: 25px; padding: 4px; text-align: center;’>”; echo ($x * $y); echo “</td> \n”; } echo “</tr> \n”; } echo “</table>”;?>

More Related