steve
steve

Reputation: 41

php help cannot get code to loop in xml file to wight to database

i have put this code in i get all the right products_id but all the products_model are the same number

 foreach($xml->xpath('//PRODUCT/@ITEM') as $productitemid){
 foreach($xml->CREATED->CATEGORY->PRODUCT as $product)

 mysql_query("INSERT INTO products (products_id,products_model) VALUES ('$productitemid','$product->MODEL')");

} 

Upvotes: 0

Views: 81

Answers (1)

Romi Halasz
Romi Halasz

Reputation: 2009

If you have multiple 'product' items in your input file, then you need to iterate through them. You 'foreach' needs to look look something like this:

foreach($xml->CREATED->CATEGORY->PRODUCT as $product)
    mysql_query("INSERT INTO products (products_model) VALUES ('$product->model')")

Notice that the problem is that your code $xml->CREATED->CATEGORY->PRODUCT->MODEL means "get me the 'MODEL' elements from the first 'PRODUCT' element. So that's why you only get one item.

Hope this helps.

Upvotes: 1

Related Questions