andrewgreenwood247@hotmail Posted March 15, 2011 Report Share Posted March 15, 2011 Hi All, I've watched the tutorial which shows how we can return the first 5 records from XML data. It all makes sense, however, what I would like to do is return specific data. See attachment... phonebook.xml I would like to return the record where <name></name> = Stan including all other elements relating to the record (number, address and email), But how would I code this in simplexml? I assume it will be similar to the tutorial with some kind of arguement but I am well and truely stuck here... Thanks Andy Quote Link to comment Share on other sites More sharing options...
syn3rgy Posted March 15, 2011 Report Share Posted March 15, 2011 Hi All, I've watched the tutorial which shows how we can return the first 5 records from XML data. It all makes sense, however, what I would like to do is return specific data. See attachment... phonebook.xml I would like to return the record where <name></name> = Stan including all other elements relating to the record (number, address and email), But how would I code this in simplexml? I assume it will be similar to the tutorial with some kind of arguement but I am well and truely stuck here... Thanks Andy First off let me tell you I am an absolute PHP beginner. I have a similar need and just so happen to be also working off the same tutorial. In your example, to get the information in the tags you can do this: <?php $file = "phonebook.xml"; $xml = simplexml_load_file($file); //get data from XML tags and store in array foreach($xml->note as $item) { $info = array( "name" => $item->name, "number" => $item->number, "address" => $item->address, "email" => $item->email ); //simple echo out echo "<br />"; print $info["name"]; echo "<br />"; print $info["number"]; echo "<br />"; print $info["address"]; echo "<br />"; print $info["email"]; echo "<br />"; } ?> If you want to test with this, just put your php and xml file in the same directory and run it. That's at least one way to access the data in the tags, after you get it I'm not sure how to deal with it. I wont hijack your thread, so I will start another one with my question. Hope this helps. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 15, 2011 Report Share Posted March 15, 2011 @benjamin - I'm not sure if that was completely what @AndyG23 was looking for, since he wanted to display the results associated with one specific record (not all of them), but posting the code helped me have code to start from. @AndyG23 - Here's one way to approach it: <?php $xml = simplexml_load_file("xml.xml"); foreach($xml->note as $item) { if ($item->name == "Stan") { $info = array( "name" => (string)$item->name, "number" => (string)$item->number, "address" => (string)$item->address, "email" => (string)$item->email ); break; } } echo "<pre>"; print_r($info); echo "</pre>"; ?> The "(string)" section is required to convert the $item->name (which is a SimpleXML object) into a string. The "break;" causes the PHP parser to jump out of the foreach loop once it finds the record it is looking for. However, keep in mind that you still need to figure out what to do if the correct record doesn't exist, or what happens if two of the same record exist (two people named "Stan", for example.) According to the code above, only the first "Stan"'s info would be returned. If you have any questions let me know. Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 15, 2011 Author Report Share Posted March 15, 2011 Ah, Thanks Ben, That is what I was looking for. So, Say if each item had it's own unique id (in this case the telephone number) I could add further agruments to the "if" statement? Like this... if ($item->name == "Stan" && $item->number == "+44325656565") Thanks for your help on this guys (both Benjamin and Ben), it's very much appreciated. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 16, 2011 Report Share Posted March 16, 2011 if ($item->name == "Stan" && $item->number == "+44325656565") Yes. Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 16, 2011 Author Report Share Posted March 16, 2011 However, keep in mind that you still need to figure out what to do if the correct record doesn't exist Hi Ben, I had a look at this last night and tried to figure out what code I would write to account for a record that didn't exist. Again, I'm baffled. Any ideas? Regards Andy Quote Link to comment Share on other sites More sharing options...
BeeDev Posted March 16, 2011 Report Share Posted March 16, 2011 Something like this: <?php $xml = simplexml_load_file("xml.xml"); $found = false; foreach($xml->note as $item) { if ($item->name == "Stan") { $found = true; $info = array( "name" => (string)$item->name, "number" => (string)$item->number, "address" => (string)$item->address, "email" => (string)$item->email ); break; } } if($found){ // Item found echo "<pre>"; print_r($info); echo "</pre>"; }else{ // Item not found echo "<p>Nooooooo! I can't find itttt! SAD FACE</p>"; } ?> Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 16, 2011 Author Report Share Posted March 16, 2011 <?php $xml = simplexml_load_file("xml.xml"); foreach($xml->note as $item) { if(!$item->name) { // no name tag to process echo "No items found"; } else { if ($item->name == "Stan") { //there's a name tag. check and return if criteria is met. $info = array( "name" => (string)$item->name, "number" => (string)$item->number, "address" => (string)$item->address, "email" => (string)$item->email ); break; } } } // Item was found and now returned echo "<pre>"; print_r($info); echo "</pre>"; ?> I was thinking something along the lines of the above. But, I'm at work now and can't try any of them out. Thanks BeeDev Just thought I would add onto this... I've just read the "Help Vampires" post, I hope I'm not deemed as a "Help Vampire". Quote Link to comment Share on other sites More sharing options...
falkencreative Posted March 16, 2011 Report Share Posted March 16, 2011 I've just read the "Help Vampires" post, I hope I'm not deemed as a "Help Vampire". No -- that post was aimed primarily at people who post questions and don't try to help themselves first (by searching Google, or this forum in case others have asked the same question, experimenting and trying things on their own, etc.) Basically, people who post lots of questions and simply expect someone to provide them with all the answers, without trying to solve them on their own first. If you've made an attempt and are still struggling, we are happy to help. Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 16, 2011 Author Report Share Posted March 16, 2011 Hi Ben, Thanks for that confirmation Phew!!! Quote Link to comment Share on other sites More sharing options...
andrewgreenwood247@hotmail Posted March 21, 2011 Author Report Share Posted March 21, 2011 Hi Guys, I just wanted to thank all of the people who posted above. I had a technical test for a software engineers job on Thursday and it was based on extracting data from numerous xml files. Without the knowledge you all passed on and with myself having no prior experience with extracting xml data I would have been very stuck indeed! So, thanks again from a very happy AndyG Interview is on Thursday, fingers crossed. Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.