Ty44ler Posted December 16, 2009 Share Posted December 16, 2009 Just learning so bear with me.... I want to create a drop down box with values that are pulled from an xml file. I can get the drop down box and the names to be placed within the box right, but I can't get the name of the option to be the value within the drop down box, so instead of NUM it would be the color that the <option> tag surrounds.... For example: <select><option>Please Select</option><option [color=red]value='COLORS'[/color]> <option value='NUM'>red</option> <option value='NUM'>blue</option> <option value='NUM'>yellow</option> <option value='NUM'>green</option> <option value='NUM'>purple</option> <option value='NUM'>orange</option> <option value='NUM'>banana</option> </option></select> Here's my xml: <?xml version="1.0"?> <colors> <num>red</num> <num>blue</num> <num>yellow</num> <num>green</num> <num>purple</num> <num>orange</num> <num>banana</num> </colors> Here's my php file: <?php // The XML file that you wish to be parsed $file = "xml_beginner.xml"; // This function tells the parser what to do with the data once it reaches the contents // that appear between tags. echo "<select><option>Please Select</option>"; function contents($parser, $data){ echo $data; } // This function tells the parser to place a <b> where it finds a start tag. function startTag($parser, $data){ echo "<option value='$data'>"; } // And this function tells the parser to replace the end tags with "<b><br />" function endTag($parser, $data){ echo "</option>"; } // These lines create the parser and then set the functions for the parser to use when // reading the document. $xml_parser = xml_parser_create(); // Sets the functions for start and end tags xml_set_element_handler($xml_parser, "startTag", "endTag"); // Sets the function for the contents/data xml_set_character_data_handler($xml_parser, "contents"); // Opens the file for reading $fp = fopen($file, "r"); // Read the file and save its contents as the variable "data" $data = fread($fp, 80000); // This if statement does two things. 1) it parses the document according to our // functions created above. 2) If the parse fails for some reason it returns an // error message and also tells us which line the error occured at. if(!(xml_parse($xml_parser, $data, feof($fp)))){ die("Error on line " . xml_get_current_line_number($xml_parser)); } // Free the memory used to create the parser xml_parser_free($xml_parser); // Close the file when you're done reading it fclose($fp); echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 you can parse the xml consider the following code <?php $str = '<?xml version="1.0"?> <colors> <num>red</num> <num>blue</num> <num>yellow</num> <num>green</num> <num>purple</num> <num>orange</num> <num>banana</num> </colors>'; $root = simplexml_load_string($str); $data = get_object_vars($root); foreach ($data['num'] as $color) { echo $color . "<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted December 16, 2009 Author Share Posted December 16, 2009 wow, that was a lot simpler than what I previously had. Thanks a bunch! I may have a few more questions once I begin pulling from an actual xml web service, but this is great! 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.