PHP XML Counting Elements

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.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 17th, 2006, 18:42
Junior Member
Join Date: Mar 2006
Age: 21
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
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');
}

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 18th, 2006, 03:03
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP XML Counting Elements

Code: Select all
foreach ($xml->species as $species) {
  echo $species->name, '<br />';
}
See Example 3 on http://uk.php.net/manual/en/ref.simplexml.php, entitled Accessing non-unique elements in SimpleXML

-- Graham
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
php, xml, counting, elements

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 03:51.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42