02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious Video

foreach

<p>We now look at how to write a basic <a href="/courses/video/47/374/Looping-XML.html">loop</a> in PHP. Generally, our XML document may have several instances of the same element category. This element list is organized as just that %u2013 a list %u2013 and the individual elements need to be processed one at a time. The PHP language provides a looping construct - &quot;foreach&quot; - which can be used to step through the list of element instances just like an array is indexed using <a href="/courses/video/5/161/Numbers-int-And-unit.html">integer</a> <a href="/courses/video/4/153/What-Are-Variables.html">variables</a> to obtain specific element values.</p> <p>A practical example will clear things up, if there's any doubt remaining as to how foreach looping constructs work! Here's a real-life example: </p> <p>Consider a bar that's one of a chain. Suppose each bar has a specification in XML,so that it can be read and processed by a computer program for maintaining drink records. An XML document that specifies the structure of a particular bar might look like:</p> <p>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt; </p> <p>&lt;bar&gt; </p> <p>&lt;drink id=&quot;Martini&quot; base=&quot;gin&quot; /&gt; </p> <p>&lt; drink id=&quot;Sex_on_the_Beach&quot; base=&quot;peach_creme,&quot; /&gt; </p> <p>&lt; drink id=&quot;Fuzzy_Navel&quot; base=&quot;schnapps&quot; /&gt; </p> <p>&lt; drink id=&quot;Vodka_Collins&quot; base=&quot;vodka&quot; /&gt; </p> <p>&lt;wine&gt; </p> <p>&lt;name type=&quot;Red&quot; id=&quot;Merlot&quot; /&gt; </p> <p>&lt;name type=&quot;White&quot; id=&quot;Riesling&quot; /&gt; </p> <p>&lt;name type=&quot;White&quot; id=&quot;Chardonnay&quot; /&gt; </p> <p>&lt;/wine&gt; </p> <p>&lt;/bar&gt; </p> <p>Let us now write a PHP script with source code that loads this bar.xml document, iterates through the &quot;duplicate&quot; elements (which have different attribute values) and prints out information that is useful for auditing purposes. Have a look at the following sample: </p> <p>&lt;? </p> <p>$bar = simplexml_load_file(&quot;bar.xml&quot;); </p> <p>foreach($bar-&gt;drink as $drink){ </p> <p>echo $drink['id'] . 'is made from:' . $drink['base']; </p> <p>}</p> <p>foreach($bar-&gt;wine as $wine){ </p> <p>foreach($wine-&gt;name as $name){ </p> <p>echo $name['type']; </p> <p>}</p> <p>}</p> <p>?&gt;</p> <p>The organization of the PHP code segment is intuitive and can easily be seen to match with the XML structure explained above it. Such looping constructs are extremely useful for progammers because they don't need to know the exact number of <a href=" /courses/video/47/396/Elements-and-Attributes-in-PHP.html">element</a> in an XML document. The program that processes the XML document simply indexes the element list (of the same category) just as an array of values is read by a conventional program. Arbitrarily-sized XML documents can be easily processed without much ado. And such code structure is also easy to read, since loops are fundamental constructs of all programming languages.</p>

Overview and Context of XML

We will learn about the historical perspective and overview of the XML standard. The main focus is on Information organization and cross platform usage

12:47

XML Elements

The first step in learning about how XML documents are structured is to get familiar with elements, the basic components of an XML document.

08:22

XML Nested Elements

Nested elements allow for complex data to be represented effortlessly in an XML document.

09:19

Attributes

Attribute-value pairs are an alternative to nesting of elements in an XML document. We'll see the pros and cons of this feature

08:12

More Then Just Leftovers

Comments, White-space, Special characters, Version Information and Parser by-passing content; we discuss what these constructs are and how to use them effectively

13:54

XML Validation

XML documents should be well formed structure- and content-wise for applications to be configured and behave properly. We discuss an analogy with a real life scenario to highlight the point

08:17

E4X – ActionScript 3.0

We describe the importance and beneficial features of E4X library, and why it should be used in browsers at the earliest

24:56

XML DOM

An XML document is often pictured as an inverted nodes (elements) tree and connected nodes have a relation between them. DOM allows the program to traverse and retrieve these related nodes.

11:12

JavaScript

We describe the basic syntax of Javascript to load XML documents in client-side applications

13:51

Looping XML

We describe the looping constructs of PHP scripting language and how they can be used to parse and render XML information in client software

14:19

XML as a Remote

We describe the utility of XML for the modern web, especially when there's a plethora of programming languages and platforms and how XML builds several bridges

09:19

Loading XML in PHP

We describe how to load XML documents into server side applications in the PHP scripting language

04:57

Elements and Attributes in PHP

We describe how to access elements and attribute values while processing XML content via PHP scripts in server-side software

04:44

foreach

We describe the foreach looping construct in PHP and also how it can be used to process XML documents in server-side software

07:01

PHP XML compare

We describe how to compare XML document components and content in different documents having the same syntax

05:08

Modifying XML

We describe how to modify the content of XML documents in PHP : insertion/deletion/update operations in server-side applications

08:34