www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] XML and PHP

  1. #1
    Join Date
    Feb 2012
    Posts
    30

    resolved [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:

    PHP Code:
    $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 />";

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

    Event - MyEventValueFromXMLDocument

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

    HTML Code:
    $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())

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

    <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

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,223
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2012
    Posts
    30
    Quote Originally Posted by NogDog View Post
    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.
    so would it look like this:

    PHP Code:
    $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 />";

    or would it look like:

    PHP Code:
    $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 />";

    many thanks

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,223
    The first one.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Feb 2012
    Posts
    30
    genius! thank you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles