/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] XML and PHP

Hello,

I have extracted details from an XML document into PHP then into MySql.

I have written the following code to get the XML into PHP:

[code=php]
$xmlDoc = new DOMDocument();
$xmlDoc->load( ‘cdocument-2012-03-03.xml’ );

///// THIS IS AN EVENT TAG //////
$searchNode = $xmlDoc->getElementsByTagName( “Event” );
foreach( $searchNode as $searchNode )
{
$valueEvent = $searchNode->getAttribute(‘VALUE’);
echo “Event – $valueEvent<br /><br />”;
}
[/code]

when this displays the XML is will show the following text on the page:

[B]Event – MyEventValueFromXMLDocument[/B]

i then have to insert it into my sql using the following insert:

[code=html]
$sql = “insert into `Conferences` (`Event`) values (‘$valueEvent’)”;
$perform_insert = mysql_query($sql) or die(“<b>Data could not be entered</b>.n<br />Query: ” . $query . “<br />nError: (” . mysql_errno() . “) ” . mysql_error())
[/code]

This all works perfectly, but… in my XML file i hold about 300 different XML values to the same tags [I] e.g.[/I]

<Basic-Information>
<The-Conference-ID value=”THE ID 1″ />
<This-Conference-ID value=”THIS ID 1″ />
<Subject value=”THIS SUBJECT 1″ />
</Basic-Information>

<Basic-Information>
<The-Conference-ID value=”THE ID 2″ />
<This-Conference-ID value=”THIS ID 2″ />
<Subject value=”THIS SUBJECT 2″ />
</Basic-Information>

<Basic-Information>
<The-Conference-ID value=”THE ID 2″ />
<This-Conference-ID value=”THIS ID 2″ />
<Subject value=”THIS SUBJECT 2″ />
</Basic-Information>

So, my question is: how can i make my xml create a new table row for the next set of information as at the min it just overwrites it.

row 1:
<Basic-Information>
<The-Conference-ID value=”THE ID 1″ />
<This-Conference-ID value=”THIS ID 1″ />
<Subject value=”THIS SUBJECT 1″ />
</Basic-Information>

row 2:
<Basic-Information>
<The-Conference-ID value=”THE ID 2″ />
<This-Conference-ID value=”THIS ID 2″ />
<Subject value=”THIS SUBJECT 2″ />
</Basic-Information>

…….

Please note I’m also using “foreach( $searchNode as $searchNode )” in the PHP, not sure if this will need changing?

Hope this all makes sense

Many Thanks

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 15.2012 — In your foreach() statement, you should use a different variable name for the expression after "as", and then use that within the loop, so that you don't clobber the original variable's value with the loop iteration value.
Copy linkTweet thisAlerts:
@thewebportfolioauthorMar 15.2012 — In your foreach() statement, you should use a different variable name for the expression after "as", and then use that within the loop, so that you don't clobber the original variable's value with the loop iteration value.[/QUOTE]

so would it look like this:

[code=php]
$xmlDoc = new DOMDocument();
$xmlDoc->load( 'cdocument-2012-03-03.xml' );

///// THIS IS AN EVENT TAG //////
$searchNode = $xmlDoc->getElementsByTagName( "Event" );
foreach( $searchNode as $MYsNode )
{
$valueEvent = $MYsNode->getAttribute('VALUE');
echo "Event - $valueEvent<br /><br />";
}
[/code]


or would it look like:

[code=php]
$xmlDoc = new DOMDocument();
$xmlDoc->load( 'cdocument-2012-03-03.xml' );

///// THIS IS AN EVENT TAG //////
$searchNode = $xmlDoc->getElementsByTagName( "Event" );
foreach( $searchNode as $MYsNode )
{
$valueEvent = $searchNode->getAttribute('VALUE');
echo "Event - $valueEvent<br /><br />";
}
[/code]


many thanks?
Copy linkTweet thisAlerts:
@NogDogMar 15.2012 — The first one. ?
Copy linkTweet thisAlerts:
@thewebportfolioauthorMar 15.2012 — genius! thank you ?
×

Success!

Help @thewebportfolio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...