Ajay Kumar Ajay Kumar - 7 months ago 37
HTML Question

Convert JSON data to HTML dropdown list with PHP

How can I convert this XML/JSON data into an HTML

select
droplist using PHP?

JSON/XML Data:

{"CountryList":"<Countries><Country><Code>AF<\/Code><Name>Afghanistan<\/Name><\/Country><Country><Code>AL<\/Code><Name>Albania<\/Name><\/Country><Country><Code>DZ<\/Code><Name>Algeria<\/Name><\/Country><Country><Code>AS<\/Code><Name>American Samoa<\/Name><\/Country><Country><Code>AD<\/Code><Name>Andorra<\/Name><\/Country><Country><Code>AO<\/Code><Name>Angola<\/Name><\/Country><Country><Code>AI<\/Code><Name>Anguilla<\/Name><\/Country><Country><Code>AQ<\/Code><Name>Antarctica<\/Name><\/Country><Country><Code>AG<\/Code><Name>Antigua &amp; Barbuda<\/Name><\/Country><Country><Code>AR<\/Code><Name>Argentina<\/Name><\/Country><Country><Code>AM<\/Code><Name>Armenia<\/Name><\/Country><Country><Code>AW<\/Code><Name>Aruba<\/Name><\/Country><Country><Code>AU<\/Code><Name>Australia<\/Name><\/Country><Country><Code>AT<\/Code><Name>Austria<\/Name><\/Country><Country><Code>AZ<\/Code><Name>Azerbaijan<\/Name><\/Country><Country><Code>BS<\/Code><Name>Bahamas<\/Name><\/Country><Country><Code>BH<\/Code><Name>Bahrain<\/Name><\/Country><Country><Code>BD<\/Code><Name>Bangladesh<\/Name><\/Country><Country><Code>BB<\/Code><Name>Barbados<\/Name><\/Country><Country><Code>BY<\/Code><Name>Belarus (Belorussia)<\/Name><\/Country><\/Countries>","Error":{"ErrorCode":0,"ErrorMessage":""},"Status":1,"TokenId":"bdf0738c-7a47-410e-961a-52da9b5df935"}


Desired HTML output:

<select>
<option value="AF|Afghanistan">Afghanistan</option>
<option value="AL|Albania">Albania</option>
<option value="DZ|Algeria">Algeria</option>
<option value="AS|American Samoa">American Samoa</option>
<option value="AD|Andorra">Andorra</option>
<option value="AO|Angola">Angola</option>
<option value="AI|Anguilla">Anguilla</option>
<option value="AQ|Antarctica">Antarctica</option>
<option value="AG|Antigua & Barbuda">Antigua & Barbuda</option>
<option value="AR|Argentina">Argentina</option>
<option value="AM|Armenia">Armenia</option>
<option value="AW|Aruba">Aruba</option>
<option value="AU|Australia">Australia</option>
<option value="AT|Austria">Austria</option>
<option value="AZ|Azerbaijan">Azerbaijan</option>
<option value="BS|Bahamas">Bahamas</option>
<option value="BH|Bahrain">Bahrain</option>
<option value="BD|Bangladesh">Bangladesh</option>
<option value="BB|Barbados">Barbados</option>
<option value="BY|Belarus (Belorussia)">Belarus (Belorussia)</option
</select>




after parsing, am getting array like this

Array

(

        [CountryList] => AFAfghanistanALAlbaniaDZAlgeriaASAmerican SamoaADAndorraAOAngolaAIAnguillaAQAntarcticaAGAntigua & BarbudaARArgentinaAMArmeniaAWArubaAUAustraliaATAustriaAZAzerbaijanBSBahamasBHBahrainBDBangladeshBBBarbadosBYBelarus (Belorussia)


         [Error] => Array

                 (

                         [ErrorCode] => 0

                         [ErrorMessage] =>

                 )


         [Status] => 1


         [TokenId] => 5a5e32c4-77ee-4703-b0b1-4ff275ac61asw0
)




Data that i am getting see in the below link
https://drive.google.com/file/d/0B9VV_J4sKTatdWJGeHJkOVZzZ00/view?usp=sharing

Answer

try this one may be it will helpfull for you.

Solution

$json = json_decode($data);$source = new DOMDocument();
$source->loadXml($json->CountryList);
$xpath = new DOMXpath($source);
$target = new DOMDocument();
$options = $target->appendChild($target->createElement('select'));

foreach ($xpath->evaluate('/Countries/Country') as $country) {
    $countryCode = $xpath->evaluate('string(Code)', $country);
    $countryName = $xpath->evaluate('string(Name)', $country);
    $option = $options->appendChild(
        $target->createElement('option')
    );
    $option->setAttribute('value', $countryCode . "|" . $countryName);
    $option->appendChild($target->createTextNode($countryName));
}

$target->formatOutput = TRUE;
echo $target->saveXml($options);

Output:

    <select>
        <option value="AF|Afghanistan">Afghanistan</option>
        <option value="AL|Albania">Albania</option>
        <option value="DZ|Algeria">Algeria</option>
        <option value="AS|American Samoa">American Samoa</option>
        <option value="AD|Andorra">Andorra</option>
        <option value="AO|Angola">Angola</option>
        <option value="AI|Anguilla">Anguilla</option>
        <option value="AQ|Antarctica">Antarctica</option>
        <option value="AG|Antigua & Barbuda">Antigua & Barbuda</option>
        <option value="AR|Argentina">Argentina</option>
        <option value="AM|Armenia">Armenia</option>
        <option value="AW|Aruba">Aruba</option>
        <option value="AU|Australia">Australia</option>
        <option value="AT|Austria">Austria</option>
        <option value="AZ|Azerbaijan">Azerbaijan</option>
        <option value="BS|Bahamas">Bahamas</option>
        <option value="BH|Bahrain">Bahrain</option>
        <option value="BD|Bangladesh">Bangladesh</option>
        <option value="BB|Barbados">Barbados</option>
        <option value="BY|Belarus (Belorussia)">Belarus (Belorussia)</option
     </select>
Comments