Help with loop please

Hi

I need some new eyes to take a look at this please.

XML FILE:


<root>

<property>
<id>Example1</id>

<details>

<category id="1">
<name>
<en>Surroundings</en>
</name>

<value id="1">
<en>Rural</en>
</value>

<value id="2">
<en>Close To Railway</en>
</value>
</category>

<category id="2">
<name>
<en>Orientation</en>
</name>

<value id="1">
<en>South West</en>
</value>
</category>

</details>

</property>

</root>

I have the following which returns the id and category in my string as required.

INSERT:

$xml = 'categories.xml'; // URL for feed.



try{
  $feed = new SimpleXMLElement($xml, null, true);
}catch(Exception $e){
  echo $e-&gt;getMessage();
  exit;
}


$sql = 'INSERT INTO database (`id`, `category`) VALUES ';


foreach($feed-&gt;property as $property) // LOOP THROUGH EACH PROPERTY TO GET id
{

$xpath="details/category/name"; // PATH TO CATEGORIES
$category_elements = $property-&gt;xpath($xpath);

foreach ($category_elements as $category) //LOOP THROUGH CATEGORIES 

{

$sql .= sprintf(
    "\
('%s', '%s'),",
   $property-&gt;id,
   $category-&gt;en
  );
  
  
      }
  }




$sql = rtrim($sql, ',') . ';';

echo $sql;

This produces

INSERT INTO database (`id`, `category`) VALUES  ('Example1', 'Surroundings'), ('Example1', 'Orientation')

All good so far. What I now need to do is add the value to those categories relevant to each id.

I have tried various loops but none of my logic seems to return the correct results.

This was my final attempt below which I can see is not correct because it loops through the categories for every value. I need to loop the values though because one category can contain more than one value.

$sql = 'INSERT INTO database (`id`, `category`, `value`) VALUES ';


foreach($feed-&gt;property as $property) // LOOP THROUGH EACH PROPERTY TO GET id
{

$xpath="details/category/name"; // PATH TO CATEGORIES
$category_elements = $property-&gt;xpath($xpath);

foreach ($category_elements as $category) //LOOP THROUGH CATEGORIES 

{

$xpath="details/category/value"; // PATH TO CATEGORY VALUES
$value_elements = $property-&gt;xpath($xpath);

foreach ($value_elements as $value) //LOOP THROUGH CATEGORY VALUES 

 {


 
  $sql .= sprintf(
    "\
('%s', '%s','%s'),",
   $property-&gt;id,
   $category-&gt;en,
   $value-&gt;en
  );
  }
  
    }
  }

Perhaps somebody cleverer than me could help out here. I hope I have explained this well enough to understand what I am trying to achieve.

Thanks in advance

Colin

INSERT INTO database (`id`, `category`) VALUES  ('Example1', 'Surroundings'), ('Example1', 'Orientation') 

You are doing 3 things:

Reading some xml
Creating an sql statement
Putting that sql into your database

at each stage stop and echo out some values and prove they are what you want, not just that they look about right eg the sql statement you quoted above, for example will either choke because id is unique, Example1, Example1 or enter wobbly data into your database.

Thanks for the reply. I take your comments on board, I need to start again from scratch I suppose. :frowning:

No, you don’t need to start again from scratch. Just use ECHO as the last person said and ECHO your SQL statement to the browser so that you can see if it is correct.

Thanks for the reply. Sorry if I was a bit snappy but I have been trying to figure this out for too long.

I have been echoing my SQL and it returns the following in current guise.

INSERT INTO database (`id`, `category`, `value`) VALUES  ('Example1', 'Surroundings','Close To Golf'), ('Example1', 'Surroundings','South West'), ('Example1', 'Surroundings','Communal'), ('Example1', 'Surroundings','Hot A/C'), 

Well that is just some of it. In fact it returns a value for every category then starts over returning the same values for the next category and so on.

What I need to insert into my DB should be:

INSERT INTO database (`id`, `category`, `value`) VALUES  ('Example1', 'Surroundings','Close To Golf'), ('Example1', 'Orientation','South West'), ('Example1', 'Pool','Communal'), ('Example1', 'Air Conditioner','Hot A/C'), 

Colin

I suspect what you need to insert into my DB should be:


INSERT INTO database (`id`, `category`, `value`) 
VALUES  ('Example1', 'Surroundings','Close To Golf')
, ('Example2', 'Orientation','South West')
, ('Example3', 'Pool','Communal')
, ('Example4', 'Air Conditioner','Hot A/C');

Now if you cannot insert that into your database then maybe you should tell us what kind of error you are getting back from mysql - I mean bypass PHP, just paste that into PhpMyAdmin or whatever you use to manage your database.

Next, PHPs task is to assemble the query so that it exactly matches a good sql statement.

Come back and tell us what you did and what error messages you can see, if you cannot see any then tell us that too.

or print mysql_error()

No errors I just cannot assemble the query together correctly I get

INSERT INTO database (`id`, `category`, `value`) VALUES 
('Example1', 'Surroundings','Close To Golf'),
('Example1', 'Surroundings','South West'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Hot A/C'),
('Example1', 'Surroundings','Cold A/C'),
('Example1', 'Surroundings','U/F Heating'),
('Example1', 'Surroundings','Sea'),
('Example1', 'Surroundings','Golf'),
('Example1', 'Surroundings','Country'),
('Example1', 'Surroundings','Garden'),
('Example1', 'Surroundings','Ensuite Bathroom'),
('Example1', 'Surroundings','Marble Flooring'),
('Example1', 'Surroundings','Double Glazing'),
('Example1', 'Surroundings','Not Furnished'),
('Example1', 'Surroundings','Fully Fitted'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Electricity'),
('Example1', 'Surroundings','Drinkable Water'),
('Example1', 'Orientation','Close To Golf'),
('Example1', 'Orientation','South West'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Hot A/C'),
('Example1', 'Orientation','Cold A/C'),
('Example1', 'Orientation','U/F Heating'),
('Example1', 'Orientation','Sea'),
('Example1', 'Orientation','Golf'),
('Example1', 'Orientation','Country'),
('Example1', 'Orientation','Garden'),
('Example1', 'Orientation','Ensuite Bathroom'),
('Example1', 'Orientation','Marble Flooring'),
('Example1', 'Orientation','Double Glazing'),
('Example1', 'Orientation','Not Furnished'),
('Example1', 'Orientation','Fully Fitted'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Electricity'),
('Example1', 'Orientation','Drinkable Water'),
('Example1', 'Pool','Close To Golf'),
('Example1', 'Pool','South West'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Hot A/C'),
('Example1', 'Pool','Cold A/C'),
('Example1', 'Pool','U/F Heating'),
('Example1', 'Pool','Sea'),
('Example1', 'Pool','Golf'),
('Example1', 'Pool','Country'),
('Example1', 'Pool','Garden'),
('Example1', 'Pool','Ensuite Bathroom'),
('Example1', 'Pool','Marble Flooring'),
('Example1', 'Pool','Double Glazing'),
('Example1', 'Pool','Not Furnished'),
('Example1', 'Pool','Fully Fitted'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Electricity'),
('Example1', 'Pool','Drinkable Water'),
('Example1', 'Climate Control','Close To Golf'),
('Example1', 'Climate Control','South West'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Hot A/C'),
('Example1', 'Climate Control','Cold A/C'),
('Example1', 'Climate Control','U/F Heating'),
('Example1', 'Climate Control','Sea'),
('Example1', 'Climate Control','Golf'),
('Example1', 'Climate Control','Country'),
('Example1', 'Climate Control','Garden'),
('Example1', 'Climate Control','Ensuite Bathroom'),
('Example1', 'Climate Control','Marble Flooring'),
('Example1', 'Climate Control','Double Glazing'),
('Example1', 'Climate Control','Not Furnished'),
('Example1', 'Climate Control','Fully Fitted'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Electricity'),
('Example1', 'Climate Control','Drinkable Water'),
('Example1', 'Views','Close To Golf'),
('Example1', 'Views','South West'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Hot A/C'),
('Example1', 'Views','Cold A/C'),
('Example1', 'Views','U/F Heating'),
('Example1', 'Views','Sea'),
('Example1', 'Views','Golf'),
('Example1', 'Views','Country'),
('Example1', 'Views','Garden'),
('Example1', 'Views','Ensuite Bathroom'),
('Example1', 'Views','Marble Flooring'),
('Example1', 'Views','Double Glazing'),
('Example1', 'Views','Not Furnished'),
('Example1', 'Views','Fully Fitted'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Electricity'),
('Example1', 'Views','Drinkable Water'),
('Example1', 'Features','Close To Golf'),
('Example1', 'Features','South West'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Hot A/C'),
('Example1', 'Features','Cold A/C'),
('Example1', 'Features','U/F Heating'),
('Example1', 'Features','Sea'),
('Example1', 'Features','Golf'),
('Example1', 'Features','Country'),
('Example1', 'Features','Garden'),
('Example1', 'Features','Ensuite Bathroom'),
('Example1', 'Features','Marble Flooring'),
('Example1', 'Features','Double Glazing'),
('Example1', 'Features','Not Furnished'),
('Example1', 'Features','Fully Fitted'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Electricity'),
('Example1', 'Features','Drinkable Water'),
('Example1', 'Furniture','Close To Golf'),
('Example1', 'Furniture','South West'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Hot A/C'),
('Example1', 'Furniture','Cold A/C'),
('Example1', 'Furniture','U/F Heating'),
('Example1', 'Furniture','Sea'),
('Example1', 'Furniture','Golf'),
('Example1', 'Furniture','Country'),
('Example1', 'Furniture','Garden'),
('Example1', 'Furniture','Ensuite Bathroom'),
('Example1', 'Furniture','Marble Flooring'),
('Example1', 'Furniture','Double Glazing'),
('Example1', 'Furniture','Not Furnished'),
('Example1', 'Furniture','Fully Fitted'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Electricity'),
('Example1', 'Furniture','Drinkable Water'),
('Example1', 'Kitchen','Close To Golf'),
('Example1', 'Kitchen','South West'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Hot A/C'),
('Example1', 'Kitchen','Cold A/C'),
('Example1', 'Kitchen','U/F Heating'),
('Example1', 'Kitchen','Sea'),
('Example1', 'Kitchen','Golf'),
('Example1', 'Kitchen','Country'),
('Example1', 'Kitchen','Garden'),
('Example1', 'Kitchen','Ensuite Bathroom'),
('Example1', 'Kitchen','Marble Flooring'),
('Example1', 'Kitchen','Double Glazing'),
('Example1', 'Kitchen','Not Furnished'),
('Example1', 'Kitchen','Fully Fitted'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Electricity'),
('Example1', 'Kitchen','Drinkable Water'),
('Example1', 'Garden','Close To Golf'),
('Example1', 'Garden','South West'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Hot A/C'),
('Example1', 'Garden','Cold A/C'),
('Example1', 'Garden','U/F Heating'),
('Example1', 'Garden','Sea'),
('Example1', 'Garden','Golf'),
('Example1', 'Garden','Country'),
('Example1', 'Garden','Garden'),
('Example1', 'Garden','Ensuite Bathroom'),
('Example1', 'Garden','Marble Flooring'),
('Example1', 'Garden','Double Glazing'),
('Example1', 'Garden','Not Furnished'),
('Example1', 'Garden','Fully Fitted'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Electricity'),
('Example1', 'Garden','Drinkable Water'),
('Example1', 'Parking','Close To Golf'),
('Example1', 'Parking','South West'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Hot A/C'),
('Example1', 'Parking','Cold A/C'),
('Example1', 'Parking','U/F Heating'),
('Example1', 'Parking','Sea'),
('Example1', 'Parking','Golf'),
('Example1', 'Parking','Country'),
('Example1', 'Parking','Garden'),
('Example1', 'Parking','Ensuite Bathroom'),
('Example1', 'Parking','Marble Flooring'),
('Example1', 'Parking','Double Glazing'),
('Example1', 'Parking','Not Furnished'),
('Example1', 'Parking','Fully Fitted'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Electricity'),
('Example1', 'Parking','Drinkable Water'),
('Example1', 'Utilities','Close To Golf'),
('Example1', 'Utilities','South West'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Hot A/C'),
('Example1', 'Utilities','Cold A/C'),
('Example1', 'Utilities','U/F Heating'),
('Example1', 'Utilities','Sea'),
('Example1', 'Utilities','Golf'),
('Example1', 'Utilities','Country'),
('Example1', 'Utilities','Garden'),
('Example1', 'Utilities','Ensuite Bathroom'),
('Example1', 'Utilities','Marble Flooring'),
('Example1', 'Utilities','Double Glazing'),
('Example1', 'Utilities','Not Furnished'),
('Example1', 'Utilities','Fully Fitted'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Electricity'),
('Example1', 'Utilities','Drinkable Water'),
('Example2', 'Surroundings','Close To Golf'),
('Example2', 'Surroundings','Close To Shops'),
('Example2', 'Surroundings','Close To Schools'),
('Example2', 'Surroundings','West'),
('Example2', 'Surroundings','Communal'),
('Example2', 'Surroundings','Garden'),
('Example2', 'Surroundings','Fully Fitted'),
('Example2', 'Surroundings','Communal'),
('Example2', 'Surroundings','Private'),
('Example2', 'Surroundings','Covered'),
('Example2', 'Surroundings','Electricity'),
('Example2', 'Surroundings','Drinkable Water'),
('Example2', 'Surroundings','Telephone'),
('Example2', 'Orientation','Close To Golf'),
('Example2', 'Orientation','Close To Shops'),
('Example2', 'Orientation','Close To Schools'),
('Example2', 'Orientation','West'),
('Example2', 'Orientation','Communal'),
('Example2', 'Orientation','Garden'),
('Example2', 'Orientation','Fully Fitted'),
('Example2', 'Orientation','Communal'),
('Example2', 'Orientation','Private'),
('Example2', 'Orientation','Covered'),
('Example2', 'Orientation','Electricity'),
('Example2', 'Orientation','Drinkable Water'),
('Example2', 'Orientation','Telephone'),
('Example2', 'Pool','Close To Golf'),
('Example2', 'Pool','Close To Shops'),
('Example2', 'Pool','Close To Schools'),
('Example2', 'Pool','West'),
('Example2', 'Pool','Communal'),
('Example2', 'Pool','Garden'),
('Example2', 'Pool','Fully Fitted'),
('Example2', 'Pool','Communal'),
('Example2', 'Pool','Private'),
('Example2', 'Pool','Covered'),
('Example2', 'Pool','Electricity'),
('Example2', 'Pool','Drinkable Water'),
('Example2', 'Pool','Telephone'),
('Example2', 'Views','Close To Golf'),
('Example2', 'Views','Close To Shops'),
('Example2', 'Views','Close To Schools'),
('Example2', 'Views','West'),
('Example2', 'Views','Communal'),
('Example2', 'Views','Garden'),
('Example2', 'Views','Fully Fitted'),
('Example2', 'Views','Communal'),
('Example2', 'Views','Private'),
('Example2', 'Views','Covered'),
('Example2', 'Views','Electricity'),
('Example2', 'Views','Drinkable Water'),
('Example2', 'Views','Telephone'),
('Example2', 'Kitchen','Close To Golf'),
('Example2', 'Kitchen','Close To Shops'),
('Example2', 'Kitchen','Close To Schools'),
('Example2', 'Kitchen','West'),
('Example2', 'Kitchen','Communal'),
('Example2', 'Kitchen','Garden'),
('Example2', 'Kitchen','Fully Fitted'),
('Example2', 'Kitchen','Communal'),
('Example2', 'Kitchen','Private'),
('Example2', 'Kitchen','Covered'),
('Example2', 'Kitchen','Electricity'),
('Example2', 'Kitchen','Drinkable Water'),
('Example2', 'Kitchen','Telephone'),
('Example2', 'Garden','Close To Golf'),
('Example2', 'Garden','Close To Shops'),
('Example2', 'Garden','Close To Schools'),
('Example2', 'Garden','West'),
('Example2', 'Garden','Communal'),
('Example2', 'Garden','Garden'),
('Example2', 'Garden','Fully Fitted'),
('Example2', 'Garden','Communal'),
('Example2', 'Garden','Private'),
('Example2', 'Garden','Covered'),
('Example2', 'Garden','Electricity'),
('Example2', 'Garden','Drinkable Water'),
('Example2', 'Garden','Telephone'),
('Example2', 'Parking','Close To Golf'),
('Example2', 'Parking','Close To Shops'),
('Example2', 'Parking','Close To Schools'),
('Example2', 'Parking','West'),
('Example2', 'Parking','Communal'),
('Example2', 'Parking','Garden'),
('Example2', 'Parking','Fully Fitted'),
('Example2', 'Parking','Communal'),
('Example2', 'Parking','Private'),
('Example2', 'Parking','Covered'),
('Example2', 'Parking','Electricity'),
('Example2', 'Parking','Drinkable Water'),
('Example2', 'Parking','Telephone'),
('Example2', 'Utilities','Close To Golf'),
('Example2', 'Utilities','Close To Shops'),
('Example2', 'Utilities','Close To Schools'),
('Example2', 'Utilities','West'),
('Example2', 'Utilities','Communal'),
('Example2', 'Utilities','Garden'),
('Example2', 'Utilities','Fully Fitted'),
('Example2', 'Utilities','Communal'),
('Example2', 'Utilities','Private'),
('Example2', 'Utilities','Covered'),
('Example2', 'Utilities','Electricity'),
('Example2', 'Utilities','Drinkable Water'),
('Example2', 'Utilities','Telephone');

Example1 is the identifier for each category and value. The Db table has id, category and value columns. It functions perfectly if I manually add values to my insert query. It is the assembly of this query from the XML file that I am having problems with. It should read like this:

INSERT INTO database (`id`, `category`, `value`) VALUES 
 ('Example1', 'Surroundings','Close To Golf'),
 ('Example1', 'Orientation','South West'),
 ('Example1', 'Pool','Communal'),
 ('Example1', 'Air Conditioner','Hot A/C'),
 ('Example2', 'Surroundings','Close To Golf'),
 ('Example2', 'Surroundings','Close To Railway Station'),
 ('Example2', 'Orientation','South West'),
 ('Example2', 'Pool','Private'),
 ('Example2', 'Air Conditioner','None'),
  

Colin

Well Colin if your id field does not need to be unique, as you have explained - do what I said to do earlier.

Echo your sql statement onto the page, copy that text, put that text into PhpMyAdmin - if there are no errors and the text is inserted then you are barking up the wrong tree asking about mysql errors.

You have to isolate each operation and prove they are working, as I said earlier.

Hang on - database is a reserved word.http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html rename it or quote is with backticks database.

Hi

OK so the database is not actually named “database” I am using this as an example.

I am simply trying to put the insert string together here. The resulting query will be something like SELECT Category and Value FROM database WHERE id = Example1. It is not this part that I am having an issue with.

I need to get the loop working to create the insert string in the correct way. If you take a look at what I have so far you will see that it loops through ALL the values for each category then the same again for the next and the next.

For example

INSERT INTO database (`id`, `category`, `value`) VALUES 
('Example1', 'Surroundings','Close To Golf'),
('Example1', 'Surroundings','South West'),

The second line should say Orientation South West NOT surroundings.

Take another look at my examples and you will see what I mean.

Thanks for your time on this

Colin

OK

So to further explain, this would be the result I am looking for:

INSERT INTO database (`id`, `category`, `value`) VALUES 
('Example1', 'Surroundings','Close To Golf'),
('Example1', 'Orientation','South West'),
('Example1', 'Pool','Communal'),
('Example1', 'Climate Control','Hot A/C'),
('Example1', 'Climate Control','Cold A/C'),
('Example1', 'Climate Control','U/F Heating'),
('Example1', 'Views','Sea'),
('Example1', 'Views','Golf'),
('Example1', 'Views','Country'),
('Example1', 'Views','Garden'),
('Example1', 'Features','Ensuite Bathroom'),
('Example1', 'Features','Marble Flooring'),
('Example1', 'Features','Double Glazing'),
('Example1', 'Furniture','Not Furnished'),
('Example1', 'Kitchen','Fully Fitted'),
('Example1', 'Garden','Communal'),
('Example1', 'Parking','Communal'),
('Example1', 'Utilities','Electricity'),
('Example1', 'Utilities','Drinkable Water'),
('Example2', 'Surroundings','Close To Golf'),
('Example2', 'Surroundings','Close To Shops'),
('Example2', 'Surroundings','Close To Schools'),
('Example2', 'Orientation','West'),
('Example2', 'Pool','Communal'),
('Example2', 'Views','Garden'),
('Example2', 'Kitchen','Fully Fitted'),
('Example2', 'Garden','Private'),
('Example2', 'Parking','Covered'),
('Example2', 'Utilities','Electricity'),
('Example2', 'Utilities','Drinkable Water'),
('Example2', 'Utilities','Telephone');

and what are you getting?

I am getting this:

INSERT INTO database (`id`, `category`, `value`) VALUES 
('Example1', 'Surroundings','Close To Golf'),
('Example1', 'Surroundings','South West'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Hot A/C'),
('Example1', 'Surroundings','Cold A/C'),
('Example1', 'Surroundings','U/F Heating'),
('Example1', 'Surroundings','Sea'),
('Example1', 'Surroundings','Golf'),
('Example1', 'Surroundings','Country'),
('Example1', 'Surroundings','Garden'),
('Example1', 'Surroundings','Ensuite Bathroom'),
('Example1', 'Surroundings','Marble Flooring'),
('Example1', 'Surroundings','Double Glazing'),
('Example1', 'Surroundings','Not Furnished'),
('Example1', 'Surroundings','Fully Fitted'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Communal'),
('Example1', 'Surroundings','Electricity'),
('Example1', 'Surroundings','Drinkable Water'),
('Example1', 'Orientation','Close To Golf'),
('Example1', 'Orientation','South West'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Hot A/C'),
('Example1', 'Orientation','Cold A/C'),
('Example1', 'Orientation','U/F Heating'),
('Example1', 'Orientation','Sea'),
('Example1', 'Orientation','Golf'),
('Example1', 'Orientation','Country'),
('Example1', 'Orientation','Garden'),
('Example1', 'Orientation','Ensuite Bathroom'),
('Example1', 'Orientation','Marble Flooring'),
('Example1', 'Orientation','Double Glazing'),
('Example1', 'Orientation','Not Furnished'),
('Example1', 'Orientation','Fully Fitted'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Communal'),
('Example1', 'Orientation','Electricity'),
('Example1', 'Orientation','Drinkable Water'),
('Example1', 'Pool','Close To Golf'),
('Example1', 'Pool','South West'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Hot A/C'),
('Example1', 'Pool','Cold A/C'),
('Example1', 'Pool','U/F Heating'),
('Example1', 'Pool','Sea'),
('Example1', 'Pool','Golf'),
('Example1', 'Pool','Country'),
('Example1', 'Pool','Garden'),
('Example1', 'Pool','Ensuite Bathroom'),
('Example1', 'Pool','Marble Flooring'),
('Example1', 'Pool','Double Glazing'),
('Example1', 'Pool','Not Furnished'),
('Example1', 'Pool','Fully Fitted'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Communal'),
('Example1', 'Pool','Electricity'),
('Example1', 'Pool','Drinkable Water'),
('Example1', 'Climate Control','Close To Golf'),
('Example1', 'Climate Control','South West'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Hot A/C'),
('Example1', 'Climate Control','Cold A/C'),
('Example1', 'Climate Control','U/F Heating'),
('Example1', 'Climate Control','Sea'),
('Example1', 'Climate Control','Golf'),
('Example1', 'Climate Control','Country'),
('Example1', 'Climate Control','Garden'),
('Example1', 'Climate Control','Ensuite Bathroom'),
('Example1', 'Climate Control','Marble Flooring'),
('Example1', 'Climate Control','Double Glazing'),
('Example1', 'Climate Control','Not Furnished'),
('Example1', 'Climate Control','Fully Fitted'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Communal'),
('Example1', 'Climate Control','Electricity'),
('Example1', 'Climate Control','Drinkable Water'),
('Example1', 'Views','Close To Golf'),
('Example1', 'Views','South West'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Hot A/C'),
('Example1', 'Views','Cold A/C'),
('Example1', 'Views','U/F Heating'),
('Example1', 'Views','Sea'),
('Example1', 'Views','Golf'),
('Example1', 'Views','Country'),
('Example1', 'Views','Garden'),
('Example1', 'Views','Ensuite Bathroom'),
('Example1', 'Views','Marble Flooring'),
('Example1', 'Views','Double Glazing'),
('Example1', 'Views','Not Furnished'),
('Example1', 'Views','Fully Fitted'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Communal'),
('Example1', 'Views','Electricity'),
('Example1', 'Views','Drinkable Water'),
('Example1', 'Features','Close To Golf'),
('Example1', 'Features','South West'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Hot A/C'),
('Example1', 'Features','Cold A/C'),
('Example1', 'Features','U/F Heating'),
('Example1', 'Features','Sea'),
('Example1', 'Features','Golf'),
('Example1', 'Features','Country'),
('Example1', 'Features','Garden'),
('Example1', 'Features','Ensuite Bathroom'),
('Example1', 'Features','Marble Flooring'),
('Example1', 'Features','Double Glazing'),
('Example1', 'Features','Not Furnished'),
('Example1', 'Features','Fully Fitted'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Communal'),
('Example1', 'Features','Electricity'),
('Example1', 'Features','Drinkable Water'),
('Example1', 'Furniture','Close To Golf'),
('Example1', 'Furniture','South West'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Hot A/C'),
('Example1', 'Furniture','Cold A/C'),
('Example1', 'Furniture','U/F Heating'),
('Example1', 'Furniture','Sea'),
('Example1', 'Furniture','Golf'),
('Example1', 'Furniture','Country'),
('Example1', 'Furniture','Garden'),
('Example1', 'Furniture','Ensuite Bathroom'),
('Example1', 'Furniture','Marble Flooring'),
('Example1', 'Furniture','Double Glazing'),
('Example1', 'Furniture','Not Furnished'),
('Example1', 'Furniture','Fully Fitted'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Communal'),
('Example1', 'Furniture','Electricity'),
('Example1', 'Furniture','Drinkable Water'),
('Example1', 'Kitchen','Close To Golf'),
('Example1', 'Kitchen','South West'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Hot A/C'),
('Example1', 'Kitchen','Cold A/C'),
('Example1', 'Kitchen','U/F Heating'),
('Example1', 'Kitchen','Sea'),
('Example1', 'Kitchen','Golf'),
('Example1', 'Kitchen','Country'),
('Example1', 'Kitchen','Garden'),
('Example1', 'Kitchen','Ensuite Bathroom'),
('Example1', 'Kitchen','Marble Flooring'),
('Example1', 'Kitchen','Double Glazing'),
('Example1', 'Kitchen','Not Furnished'),
('Example1', 'Kitchen','Fully Fitted'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Communal'),
('Example1', 'Kitchen','Electricity'),
('Example1', 'Kitchen','Drinkable Water'),
('Example1', 'Garden','Close To Golf'),
('Example1', 'Garden','South West'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Hot A/C'),
('Example1', 'Garden','Cold A/C'),
('Example1', 'Garden','U/F Heating'),
('Example1', 'Garden','Sea'),
('Example1', 'Garden','Golf'),
('Example1', 'Garden','Country'),
('Example1', 'Garden','Garden'),
('Example1', 'Garden','Ensuite Bathroom'),
('Example1', 'Garden','Marble Flooring'),
('Example1', 'Garden','Double Glazing'),
('Example1', 'Garden','Not Furnished'),
('Example1', 'Garden','Fully Fitted'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Communal'),
('Example1', 'Garden','Electricity'),
('Example1', 'Garden','Drinkable Water'),
('Example1', 'Parking','Close To Golf'),
('Example1', 'Parking','South West'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Hot A/C'),
('Example1', 'Parking','Cold A/C'),
('Example1', 'Parking','U/F Heating'),
('Example1', 'Parking','Sea'),
('Example1', 'Parking','Golf'),
('Example1', 'Parking','Country'),
('Example1', 'Parking','Garden'),
('Example1', 'Parking','Ensuite Bathroom'),
('Example1', 'Parking','Marble Flooring'),
('Example1', 'Parking','Double Glazing'),
('Example1', 'Parking','Not Furnished'),
('Example1', 'Parking','Fully Fitted'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Communal'),
('Example1', 'Parking','Electricity'),
('Example1', 'Parking','Drinkable Water'),
('Example1', 'Utilities','Close To Golf'),
('Example1', 'Utilities','South West'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Hot A/C'),
('Example1', 'Utilities','Cold A/C'),
('Example1', 'Utilities','U/F Heating'),
('Example1', 'Utilities','Sea'),
('Example1', 'Utilities','Golf'),
('Example1', 'Utilities','Country'),
('Example1', 'Utilities','Garden'),
('Example1', 'Utilities','Ensuite Bathroom'),
('Example1', 'Utilities','Marble Flooring'),
('Example1', 'Utilities','Double Glazing'),
('Example1', 'Utilities','Not Furnished'),
('Example1', 'Utilities','Fully Fitted'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Communal'),
('Example1', 'Utilities','Electricity'),
('Example1', 'Utilities','Drinkable Water'),
('Example2', 'Surroundings','Close To Golf'),
('Example2', 'Surroundings','Close To Shops'),
('Example2', 'Surroundings','Close To Schools'),
('Example2', 'Surroundings','West'),
('Example2', 'Surroundings','Communal'),
('Example2', 'Surroundings','Garden'),
('Example2', 'Surroundings','Fully Fitted'),
('Example2', 'Surroundings','Communal'),
('Example2', 'Surroundings','Private'),
('Example2', 'Surroundings','Covered'),
('Example2', 'Surroundings','Electricity'),
('Example2', 'Surroundings','Drinkable Water'),
('Example2', 'Surroundings','Telephone'),
('Example2', 'Orientation','Close To Golf'),
('Example2', 'Orientation','Close To Shops'),
('Example2', 'Orientation','Close To Schools'),
('Example2', 'Orientation','West'),
('Example2', 'Orientation','Communal'),
('Example2', 'Orientation','Garden'),
('Example2', 'Orientation','Fully Fitted'),
('Example2', 'Orientation','Communal'),
('Example2', 'Orientation','Private'),
('Example2', 'Orientation','Covered'),
('Example2', 'Orientation','Electricity'),
('Example2', 'Orientation','Drinkable Water'),
('Example2', 'Orientation','Telephone'),
('Example2', 'Pool','Close To Golf'),
('Example2', 'Pool','Close To Shops'),
('Example2', 'Pool','Close To Schools'),
('Example2', 'Pool','West'),
('Example2', 'Pool','Communal'),
('Example2', 'Pool','Garden'),
('Example2', 'Pool','Fully Fitted'),
('Example2', 'Pool','Communal'),
('Example2', 'Pool','Private'),
('Example2', 'Pool','Covered'),
('Example2', 'Pool','Electricity'),
('Example2', 'Pool','Drinkable Water'),
('Example2', 'Pool','Telephone'),
('Example2', 'Views','Close To Golf'),
('Example2', 'Views','Close To Shops'),
('Example2', 'Views','Close To Schools'),
('Example2', 'Views','West'),
('Example2', 'Views','Communal'),
('Example2', 'Views','Garden'),
('Example2', 'Views','Fully Fitted'),
('Example2', 'Views','Communal'),
('Example2', 'Views','Private'),
('Example2', 'Views','Covered'),
('Example2', 'Views','Electricity'),
('Example2', 'Views','Drinkable Water'),
('Example2', 'Views','Telephone'),
('Example2', 'Kitchen','Close To Golf'),
('Example2', 'Kitchen','Close To Shops'),
('Example2', 'Kitchen','Close To Schools'),
('Example2', 'Kitchen','West'),
('Example2', 'Kitchen','Communal'),
('Example2', 'Kitchen','Garden'),
('Example2', 'Kitchen','Fully Fitted'),
('Example2', 'Kitchen','Communal'),
('Example2', 'Kitchen','Private'),
('Example2', 'Kitchen','Covered'),
('Example2', 'Kitchen','Electricity'),
('Example2', 'Kitchen','Drinkable Water'),
('Example2', 'Kitchen','Telephone'),
('Example2', 'Garden','Close To Golf'),
('Example2', 'Garden','Close To Shops'),
('Example2', 'Garden','Close To Schools'),
('Example2', 'Garden','West'),
('Example2', 'Garden','Communal'),
('Example2', 'Garden','Garden'),
('Example2', 'Garden','Fully Fitted'),
('Example2', 'Garden','Communal'),
('Example2', 'Garden','Private'),
('Example2', 'Garden','Covered'),
('Example2', 'Garden','Electricity'),
('Example2', 'Garden','Drinkable Water'),
('Example2', 'Garden','Telephone'),
('Example2', 'Parking','Close To Golf'),
('Example2', 'Parking','Close To Shops'),
('Example2', 'Parking','Close To Schools'),
('Example2', 'Parking','West'),
('Example2', 'Parking','Communal'),
('Example2', 'Parking','Garden'),
('Example2', 'Parking','Fully Fitted'),
('Example2', 'Parking','Communal'),
('Example2', 'Parking','Private'),
('Example2', 'Parking','Covered'),
('Example2', 'Parking','Electricity'),
('Example2', 'Parking','Drinkable Water'),
('Example2', 'Parking','Telephone'),
('Example2', 'Utilities','Close To Golf'),
('Example2', 'Utilities','Close To Shops'),
('Example2', 'Utilities','Close To Schools'),
('Example2', 'Utilities','West'),
('Example2', 'Utilities','Communal'),
('Example2', 'Utilities','Garden'),
('Example2', 'Utilities','Fully Fitted'),
('Example2', 'Utilities','Communal'),
('Example2', 'Utilities','Private'),
('Example2', 'Utilities','Covered'),
('Example2', 'Utilities','Electricity'),
('Example2', 'Utilities','Drinkable Water'),
('Example2', 'Utilities','Telephone');  

Can you actually perform a loop within a loop within a loop?

Still trying to get a result on this.

Changed my code to:

$sql = 'INSERT INTO mydatabase (`id`, `category`, `value`) VALUES '; 
 
foreach($feed-&gt;property as $property) {

    $id = (string) $property-&gt;id;

    foreach ($property-&gt;details-&gt;category-&gt;name as $category) {

        $category = (string) $category-&gt;en;
        
        foreach ($property-&gt;details-&gt;category-&gt;value as $value) {

            $value = (string) $value-&gt;en;

            
            $sql .= sprintf( "\
('%s', '%s','%s'),", $id, $category, $value );

      }
   }
}

 echo $sql;

This returns:

INSERT INTO mydatabase (`id`, `category`, `value`) 
VALUES  
('Example1', 'Surroundings','Close To Golf'), 
('Example2', 'Surroundings','Close To Golf'), 
('Example2', 'Surroundings','Close To Shops'), 
('Example2', 'Surroundings','Close To Schools'), 

This is a step in the right direction but the category is not looping. :frowning:

C

Have you tried removing the newline character from that sprintf ?

Just tried it and it makes no difference I’m afraid.

C

Bummer! Sorry it was just a last ditch effort as nothing else seemed to be working for you either :frowning:

Bummer indeed!! I am not even sure you can loop 3 times, perhaps someone could confirm this.

I am not sure in what direction to go with this now. Please offer some ideas everyone.

C

post the results of this SQL query:

describe mydatabase;

Just had a quick play about with your code. Is this nearer to what you’re looking for? I might not have understood exactly what you’re trying to achieve, so sorry if it’s no use to you! But worth a try…


<?php

$xml = simplexml_load_file("categories.xml");

$sql = "INSERT INTO mydatabase ('id', 'category', 'value') VALUES";
$sqlxml = "";
$sqlxmlarr = array();

foreach($xml->property as $property) {

 $propertyid = (string)$property->id;
 
 foreach($property->details->category as $category) {
 
  $cat = (string)$category->name->en;
  $sqlxml = "('".$propertyid."','".$cat."',";
  
  $i = 0;
  foreach($category->value as $value) {
  
   $val = (string)$value->en;
   $sqlxmlarr[] = $sqlxml."'".$val."'),";
   $i++;
  
  }
  
 
 }

}

foreach($sqlxmlarr as $sqlchk) $sql .= $sqlchk;
$sql = rtrim($sql, ",");

echo $sql;

?>