Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more

Go Back   Webmaster Forums - Webmaster forum for HTML, PHP, ASP, CSS and more > Web Programming > PHP Development
User Name
Password

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-17-2006, 03:35 PM   #1 (permalink)
Don Logan
Junior Member
 
Join Date: Mar 2006
Posts: 19
Default 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');
}

?>
Don Logan is offline   Reply With Quote
Sponsored Links
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Points Per Thread View: 1.00
Points Per Thread: 11.00
Points Per Reply: 5.00



» Sponsors

» Links

» Affiliates
Web Hosting
Marketing Find
Merchant Select
SiteMap Builder
Host Compare
Dedicated Servers

» Links

» Sports Network
Paintball Forum
Football Forum
Hockey Forum
Golf Forum
Boxing Forum
Lacrosse Forum
Baseball Forum
SnowBoarding Forum
Soccer Forum
MMA Forum


All times are GMT -4. The time now is 05:47 PM.



LinkBacks Enabled by vBSEO 3.0.0 RC8
Webmaster Forums