Jump to content

Beginner: Selecting values from xml file


Ty44ler

Recommended Posts

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>";
?>

Link to comment
Share on other sites

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 />";
}


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.