PHP and XML. Visual Studio/File/New/File I have switched to Visual Studio to make the XML – I think it provides an easier interface for creating an XML.

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

How to Create a Local Collection
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Line Efficiency     Percentage Month Today’s Date
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
XHTML1 Building Document Structure. XHTML2 Objectives In this chapter, you will: Learn how to create Extensible Hypertext Markup Language (XHTML) documents.
String Variables Visual Basic for Applications 4.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Introducing XHTML: Module B: HTML to XHTML. Goals Understand how XHTML evolved as a language for Web delivery Understand the importance of DTDs Understand.
Inline, Internal, and External FIle
CS 174: Web Programming April 16 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
PHP Programming. Topics Background and History of PHP Installation Comments in PHP Variables Conditions Loops Functions File Handling Database Handling.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
1 MySQL and phpMyAdmin. 2 Navigate to and log on (username: pmadmin)
XHTML1 Building Document Structure Chapter 2. XHTML2 Objectives In this chapter, you will: Learn how to create Extensible Hypertext Markup Language (XHTML)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
1 Tutorial 13 Validating Documents with DTDs Working with Document Type Definitions.
Copyrighted material John Tullis 10/17/2015 page 1 04/15/00 XML Part 3 John Tullis DePaul Instructor
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
 Whether using paper forms or forms on the web, forms are used for gathering information. User enter information into designated areas, or fields. Forms.
HTML ( HYPER TEXT MARK UP LANGUAGE ). What is HTML HTML describes the content and format of web pages using tags. Ex. Title Tag: A title It’s the job.
Tutorial 13 Validating Documents with Schemas
Date processing and conversion Please use speaker notes for additional information!
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
PHP getting data from a MySQL database. Replacing XML as data source with MySQL Previously we obtained the data about the training session from an XML.
Select (drop-down list) Inputs. Insert/Form/List Menu.
CS 174: Web Programming November 4 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Dreamweaver MX. 2 Tools for Code Editing (p. 366) n An HTML editor like Dreamweaver writes most of the code you need, but at times you will need to perform.
Now, let’s examine the page data. We’ve already seen the page name and the directory where the page is saved.
1 An Introduction to Dreamweaver and PHP Part B: Some introductory PHP and JavaScript.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
Exercise 6 Introduction to C# CIS Create a class called ParseDates that will parse a formatted string containing a date into separate integers.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
A little PHP. Enter the simple HTML code seen below.
Jan 2016 Solar Lunar Data.
Having the Handler Use XML
Intro to PHP & Variables
Q1 Jan Feb Mar ENTER TEXT HERE Notes
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
In Class Program: Today in History
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com
Step 3 Step 2 Step 1 Put your text here Put your text here
HTML Structure.
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ITEM 1 ITEM 2 ITEM 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
When I opened the XML (note elements only in a DTD), I selected as an XML list and I got the following. This was customer3a.xml and note it said it was.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Free PPT Diagrams : ALLPPT.com
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
Web Programming and Design
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

PHP and XML

Visual Studio/File/New/File I have switched to Visual Studio to make the XML – I think it provides an easier interface for creating an XML file.

Select XML file from dialog box

An error already? What is this root element anyway?

Recall what an HTML document looks like It starts with the DOCTYPE tag that announce what version of HTML is being used. Then comes the tag and its closing tag which encloses the remainder of the document. XML wants the equivalent of the tag. However, since it’s XML which is defined by the user, it cannot provide this tag itself.

Type in a root element – this tag appears once and only once and contains everything but the <?xml tag Studio provides the closing tag as soon as you finish the opening tag.

Type in your first set of elements – establishing your tags and placing some data within

File/Save As…

View/Data Grid The result is reminiscent of an Excel spreadsheet and/or Access database and makes it convenient to add more data.

Enter data into the Data Grid

Click on the other tab to see the data in XML code

PHP page with empty drop-down list ( tag)

Code view: inside the select

XML Parser PHP provides an XML parser – a function that reads XML Because XML is user-defined, you must supply it with some information, such as –What kind of data should it expect –What should it do with the data

Establish some variable for the xml parsing $insideitem = false; //Boolean that determines is we are //within tags $tag = ""; //holds tag data to determine what kind of //xml tag we are dealing with $code="";//used to hold the code data $title="";//used to hold the title data $date="";//used to hold the date data $time="";//used to hold the time data $location="";//used to hold the location data $description="";//used to hold the description data $itemCount=0;//used to count the number of training items

startElement function /* Function parser will use to recognize the start of an element */ function startElement($parser, $tagName, $attrs) { global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount; if($insideitem)//if we are inside a training item, we should be dealing with internal tags { $tag=$tagName; } elseif(strtolower($tagName)=="training") //if we are not inside a training item, { $insideitem=true; $itemCount++; }

endElement function function endElement($parser, $tagName) { global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount; if(strtolower($tagName) == "training") { print " ";//print option code print $title. "(". $date. ")"; print " "; //clear out data $code = ""; $title = ""; $date = ""; $time = ""; $location = ""; $description = ""; $insideitem=false; }

function characterData($parser, $data) { global $insideitem, $tag, $code, $title, $date, $time, $location, $description, $itemCount; if($insideitem) { switch(strtolower($tag)) { case "code“: $code.=$data; break; case "title“: $title.=$data; break; case "date“: $date.=$data; break; case "time“: $time.=$data; break; case "location“: $location.=$data; break; case "description“: $description.=$data; break; }

Create xml parser //PROGRAM STARTS HERE (above was functions) $xml_parser = xml_parser_create(); //create an xml parser, give it a name xml_set_element_handler($xml_parser, "startElement", "endElement"); //informs parser of above functions so it can identify the data and //knows what to do with it xml_set_character_data_handler($xml_parser, "characterData"); //informs parser of above function

Specify the xml file to be read $fp = fopen("ACT_Training.xml", "r"); /* fp is a "file pointer" it says where a file is found. We have indicated a "relative address" which implies the xml file is in the same folder/directory as the php file "r" indicates the xml file will be read */

Reading xml file while($data = fread($fp, 4096)) { xml_parse($xml_parser, $data, feof($fp)) or die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); }

Comment on reading xml file /* Read data from the file in 4 kilobyte chunks. The while loop says that while there is still data to be read we should stay in the loop. The xml_parse() function tells the parser we established to parse the data just read in. If the xml file was larger than 4KB, then certain closing tags might not be found in the data taken in so far. The feof($fp) seen as the third argument of the xml_parse() lets the parser know whether the end-of-file has been reached. If the end-of-file has been reached and the appropriate closing has not been found, then there is a problem. The xml_parse() function refers to the rules you establish in the startElement, endElement and characterData functions. The or part is used if the xml parsing goes wrong. sprintf is a C-style print function the %s and %d indicate a string and a number that will follow */

Finishing up fclose($fp); //close the "connection/stream" to the data file xml_parser_free($xml_parser); //free up resources used by parser

Result: items in drop-down list coming from XML file

Not displaying old things In this example the first training session in the XML file has passed.

Make an array that will convert months to numbers $months = array ( 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12 );

Use the split function to parse the date into separate $month, $day and $year variables list($month, $day, $year) = split('[/.-]', $date); The function split will break its second argument into an array using its first argument as a delimiter or delimiters. –Here the delimiter could be a slash, a period or a hyphen The list function allows you to take what would be an array and assign the values to individual variables.

Only show future training $trainingTime = mktime(0,0,0,$months[$month], $day+1, $year); $now = time(); if($trainingTime >= $now) { print " "; print $title. "(". $date. ")"; print " \n"; } Use the mktime() to make a time from the xml date data (actually the very beginning of the next day), and make another time corresponding to right now. Only add the option to the drop-down list if the training is in the future.

No old training showing The June 28 th training is in the XML file, but was not placed in the drop-down list.