Jump to content
Sign in to follow this  
oikofugic

extract xml id's using php

Recommended Posts

Hi all,

 

hope you can help

 

I have an xml document that is loading nicely into a database, with the exception of the images within the image tags. See structure of xml below

<channel>
    <property>

    <name>Name here</name>

    <description>​Description here etc etc</description>

    <images>

    <img id="0">img url 1 here </img>
    <img id="1">img url 2 here </img>

    </images>


</property>
</channel>

I would like to get the image url's into the database with the | to separate them if possible.

 

Below is the php script I am using get the info from xml into database.

It works well for all fields except the images as they are id's with the main image tag

<?php

include('conn.php');

if(!$xml=simplexml_load_file('test1.xml')){

trigger_error('Error reading XML file',E_USER_ERROR);

}

 
foreach ($xml as $syn)

{

$name = $syn->name; 

$description = $syn->description; 


$images = $syn->img[]; 


$sql    = "INSERT INTO Benek

 (

name, 
description,
images 

 ) 

VALUES

 (

'$name',
'$description',

'$images'

 )";

$query  = mysql_query($sql);

 

if (!$query)

{

    echo ('Error: ' . mysql_error());

}

else

{

    echo "Record added";

}

}



?>    

Thanks for your time.

 

Paul

Share this post


Link to post
Share on other sites

I would start with $syn->images, then loop through the children of that node and get the value for each one.

Share this post


Link to post
Share on other sites

Hi thanks for your reply. That was what I did initially, but it returned no values. Possibly due to poor syntax on my part.

I have found a small piece of javascript that grabs the child elements from the parent by id, and think I may be able to update the database with the image urls after the initial values have been inserted.

Seems a bit of a convoluted approach, but that's where I am at.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...