andyfarang Posted July 12, 2016 Share Posted July 12, 2016 (edited) Hello, I want to search for a PRICE of product by his ID. I have this unussual XML structure. I found some examples for XPath. But problem is, that in my XML id is not "parent" and price is not a "child". They are equal. Could somebody help me and show me, how to get a price if I know id of that product? <eshop> <product> <id></id> <name></name> <price></price> </product> <product> <id></id> <name></name> <price></price> </product> <product> <id></id> <name></name> <price></price> </product> </eshop> Edited July 12, 2016 by andyfarang Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/ Share on other sites More sharing options...
Muddy_Funster Posted July 12, 2016 Share Posted July 12, 2016 (edited) You just need to add another stage to the process. Pull out the products with the xpath query and then create an array of product objects, then iterate through that array to find the product with the id that you want and retrieve the info you need: $xmlObj = new SimpleXMLElement($xmlData); $productsArray = $xmlObj->xpath('/eshop/product'); //... //do some stuff //... //then when you need it: foreach($productsArray as $idx=>$productObj){ if($productObj->id == $knownIdYourLookingFor){ $currentProductPrice = $productObj->price; } That gets you what you're looking for - however, it's not the best solution for a busy cart system, what would be better would be to re-index the $products array so that the keys reflect the product id. then you can quickly refferance it multiple times without having to repeatedly loop through it. //top part stays the same foreach($productsArray as $idx=>$productObj){ $products[$productObj->id] = $productObj; } unset($productsArray); // don't really nead this, but as product lists can be sizable there's no point having multiple coppies about the place //now you can grab what you need, when you need it directly from the $products array $priceYouWant = $products[$idYouKnow]->price; Let us know how that works for you. ::Edit - missed out a vary important "not" - fixed now Edited July 12, 2016 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534429 Share on other sites More sharing options...
Barand Posted July 12, 2016 Share Posted July 12, 2016 Here's an example $datax = "<eshop> <product> <id>35</id> <name>aaaaa</name> <price>50.00</price> </product> <product> <id>20</id> <name>bbbb</name> <price>100.00</price> </product> <product> <id>44</id> <name>ccccc</name> <price>70.00</price> </product> </eshop>"; $x = simplexml_load_string($datax); $prods = $x->xpath("//product[id=20]"); // find products with id = 20 echo $prods[0]->price; //--> 100.00 1 Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534430 Share on other sites More sharing options...
andyfarang Posted July 12, 2016 Author Share Posted July 12, 2016 Thank you for both answers! Additional question - XML has about 1000 products (524kB) and I have to update into MySql prices of about 200 products. Which of the solutions would be better to use? It seems that in Muddy_Funsters solution I would go through that XML at least ten thousand times? Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534431 Share on other sites More sharing options...
Muddy_Funster Posted July 12, 2016 Share Posted July 12, 2016 (edited) For that scenario you would be better using the method @barand has described, that way you only need to loop through the 200 id's that you want to update, rather than having to loop through the 1000 products and then the 200 id's. assuming your 200 id's are in an array: //load in your xml element as before - going to call it $xml in this //also going to add a bit to build some of the query string to save another loop $setStr = ""; foreach($idArray as $key=>$id){ $prod = $xml->xpath("/eshop/product[id={$id}]"); $update[$id] = $prod->price; $setStr .= "\tWHEN {$id} THEN {$prod->price}\n" } that will give you an array of the prices that you want to update in the database ::Edit - changed $setStr to set both values directly as source id data feed rather than user input. Edited July 12, 2016 by Muddy_Funster Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534433 Share on other sites More sharing options...
Barand Posted July 12, 2016 Share Posted July 12, 2016 I would be inclined to load the xml data into a temporary mysql table and then simply use an update query to change the prices in your main table. UPDATE product INNER JOIN tempproduct USING (product_id) SET product.price = tempproduct.price 1 Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534434 Share on other sites More sharing options...
andyfarang Posted July 13, 2016 Author Share Posted July 13, 2016 (edited) Thank you both for helping me. It's working. I basically used @Barrands solution with @Muddy_Funster contribution in the last post. Temporary table wasn't even needed. Thank you. Edited July 13, 2016 by andyfarang Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534453 Share on other sites More sharing options...
Muddy_Funster Posted July 13, 2016 Share Posted July 13, 2016 Quite welcome, could you mark post as answered please if there's nothing more you need on it? Quote Link to comment https://forums.phpfreaks.com/topic/301466-xpath-and-searching-in-a-xml-document/#findComment-1534454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.