leso9903 Posted July 31, 2013 Share Posted July 31, 2013 Hello, I've been trying to print out data from an array but I dont understand the code I'm working with.. A part of the xml file is seen down below. How do I only get the description or the name for example to be printed out in my html-file? In other words, my problem is that all the data gets printed out, I only want specific data. <?xml version="1.0" encoding="utf-8"?><pizzeria> <menu> <pizza id="5"> <name>Al tonno</name> <description>A simple pie with Tunafish</description> <popularity>50</popularity> <ingredient id="1" units="2" /> <ingredient id="2" units="2" /> <ingredient id="4" units="4" /> <ingredient id="5" units="2" /> </pizza> <!DOCTYPE html><html><head><meta charset="utf-8"><script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script><link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"><script type="text/javascript" src="scripts.js"></script><link rel="stylesheet" href="style.css" /><?phpinclude_once("JSON.php");$json = new Services_JSON();//$pizzeria = "pizzeria.xml";$writedir = "writehere";$pizzeria = $writedir."/pizzeria.xml";$sxe = simplexml_load_file($pizzeria);$result = array();foreach ($sxe->Children() as $pizza) { $result[] = $pizza;}echo $json->encode($result);?></head><body><h1>PIZZERIA ZORBA</h1><ul><li>Start</li><li>Order</li><li>About</li><li>TOP10</li><li>Foodstock</li><li>Recipies</li></ul></body></html> Link to comment Share on other sites More sharing options...
jeffman Posted July 31, 2013 Share Posted July 31, 2013 Some of the examples in the manual should help. Link to comment Share on other sites More sharing options...
leso9903 Posted August 1, 2013 Author Share Posted August 1, 2013 Some of the examples in the manual should help. I made it work with trial and error but the manuals doesn't make sense to me. Can I tell you how I think it works, then you correct me? the following code, for example: foreach ($sxe->menu->pizza as $pizza->ingredient) { $result[] = (string)$pizza->ingredient["units"];} foreach is a loop $sxe is my root - is it possible to skip one step, for example $sxe->pizza (I skipped $menu)? I know it works in some cases but why does it work? As for the 'as' part I've no clue. $result[] is telling the engine that it is an array and will keep going until every targeted node has been processed? string is remaking the data from xml to javascript-readable data. thanks a bunch! Link to comment Share on other sites More sharing options...
justsomeguy Posted August 1, 2013 Share Posted August 1, 2013 is it possible to skip one step, for example $sxe->pizza (I skipped $menu)? If the $sxe object has a property called pizza, then you can access it that way. If it does not have a property called pizza then that's going to be undefined. $sxe is an object, so you need to treat it as an object. If you don't understand the manual then that is a problem, you're going to have a hard time learning everything if you don't understand the manual. How much PHP do you know? You need to be familiar with most of the things listed here to be effective: http://php.net/manual/en/langref.php Here's part of the section on classes and objects, for example, it shows them using the -> operator to access properties and methods of an object: http://www.php.net/manual/en/language.oop5.basic.php Here's the section about foreach loops: http://www.php.net/manual/en/control-structures.foreach.php In your example, with this: foreach ($sxe->menu->pizza as $pizza->ingredient) There's no reason to set each item in the array to $pizza->ingredient. There might be some edge case scenarios where you might want to do something like that, but for you it would make more sense to just use a single variable like $ingredient instead of a property of an object. $result[] is telling the engine that it is an array and will keep going until every targeted node has been processed? $result should already be an array, putting the square brackets on the end is a shortcut instead of using array_push, it adds a new element onto the end of the array. http://www.php.net/manual/en/function.array-push.php The major difference between array_push and the bracket syntax is that with the brackets it will automatically create a new array if it doesn't exist yet, where array_push will issue a warning. string is remaking the data from xml to javascript-readable data. In that case it's not necessary, the attribute units is already a string. Explicitly casting it to a string doesn't change anything. This talks about converting to a string: http://www.php.net/manual/en/language.types.string.php#language.types.string.casting Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now