This is a discussion on "PHP XML Counting Elements" within the PHP Forum section. This forum, and the thread "PHP XML Counting Elements are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
PHP XML Counting Elements
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
PHP XML Counting Elements
Hi
I have some PHP code that reads the content of elements from an XML file. I have used for ( $counter = 0; $counter <= $count; ++$counter) to move from one parent element to the next and set $count = 3 because i know that the XML file has 4 parent elements. My problem is that the XML files I have contain different amounts of parent elements, i would like to use code to count them so i can set $count = $no_of_elements. Also it'd be cool to use code to find out what the element names are. I am using PHP Version 5.0.3 so simplexml & DOM/XML and Simple/XML are enabled, although i haven't yet found the appropriate functions for the job. Any advice would be great. PHP & XML code thus far is pasted below. Cheers Don <?xml version="1.0" encoding="ISO-8859-1"?> <!-- Edited with XML Spy v2006 (http://www.altova.com) --> <British_Birds> <species> <name>Golden oriole</name> <latin>Oriolus oriolus</latin> <status>Summer</status> <breeding>9-42</breeding> <passage>85</passage> <Image ID="123" URI="golden_oriole.jpg"/> <url address="www.rspb.org.uk/birds/guide/g/goldenoriole/index.asp" /> </species> <species> <name>Sparrowhawk</name> <latin>Accipiter nisus</latin> <status>Resident</status> <breeding>34,500</breeding> <passage>0</passage> <Image ID="123" URI="sparrowhawk.jpg"/> <url address="www.rspb.org.uk/birds/guide/s/sparrowhawk/index.asp" /> </species> <species> <name>Siskin</name> <latin>Carduelis spinus</latin> <status>Resident/Winter</status> <breeding>310,000</breeding> <passage>0</passage> <Image ID="123" URI="siskin.jpg"/> <url address="www.rspb.org.uk/birds/guide/s/siskin/index.asp" /> </species> <species> <name>Hoopoe</name> <latin>Upupa epops</latin> <status>Passage</status> <breeding>Occasional</breeding> <passage>100</passage> <Image ID="123" URI="hoopoe.jpg"/> <url address="www.rspb.org.uk/birds/guide/h/hoopoe/index.asp" /> </species> </British_Birds> <?php $database="birds"; $user="root"; mysql_connect(localhost,$user); @mysql_select_db($database) or die( "Unable to select database"); if (file_exists('birds5.xml')) { $xml = simplexml_load_file('birds5.xml'); $count = 3; for ( $counter = 0; $counter <= $count; ++$counter) { $image = $xml->species[$counter]->image; $name = $xml->species[$counter]->name; $latin = $xml->species[$counter]->latin; $status = $xml->species[$counter]->status; $breeding = $xml->species[$counter]->breeding; $passage= $xml->species[$counter]->passage; $url = $xml->species[$counter]->url; mysql_query("INSERT INTO allbirds (image,name,latin,status,breeding,passage,url) VALUES('$image','$name', '$latin','$status', '$breeding','$passage', '$url') ") or die(mysql_error()); } } else { exit('failed'); } ?> |
|
|
|
#2
|
|||
|
|||
|
Re: PHP XML Counting Elements
-- Graham |
![]() |
| Tags |
| php, xml, counting, elements |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [need help-SEO]Counting no. of backlinks | RohanShenoy | Search Engine Optimization (SEO) | 6 | Feb 25th, 2008 06:51 |
| CSS in Pseudo elements | webdeveloper | Web Page Design | 1 | Aug 1st, 2007 13:40 |
| Counting Instances in a table. | Pheonix | Databases | 6 | Mar 18th, 2006 22:16 |
| Counting RecordSets.... | courtjester | Classic ASP | 5 | Aug 29th, 2004 08:44 |