0

I need some help. I am a newbie to php ..
What I am trying to do is to parse an xml file and create a drop-down for the user.
I have attached how my drop down list looks currently.
What I want to do is to add a sub list to each option.. say to networkSecurity I want to add option a,b,c,d which I have to read from the same xml file.
How this can be done ?
To create normal drop-down list my code looks like

<

?php

    $xml=simplexml_load_file('info.xml');
    foreach($xml->testcase as $var){

        $var=explode('/',$var->script);

        $module[] =$var[2];

        $testName[] = end($var);
 }
        echo "<pre>";
        print_r($module);


        print_r($testName);

        $modules = array_unique($module); 

foreach($modules as $newarr) 
{

 $newmodules[]=$newarr;
 }

 print_r($newmodules);

 ?> 
<select name="module" id="Module">
        <?php
        $i=0;
        foreach($newmodules as $mod)
        {
            ?>
            <option value="<?php echo $mod;?>"><?php echo $newmodules[$i];?></option>
            <?php
         $i++;
       }
    ?>
    </select>

Also the XML file that I am parsing looks like Click Here
What i want to do is to parse the xml file, read the script tag and based on the 3rd position of script (hostAgentFeatures or sdnSTC) I have to create a dropdown list. Later I want to check all such script tags and read for 3rd position and for say sdnSTC I want to read the last value of script tag (ending with ".tcl") and create it as a sub drop-down list for sdnSTC.
How this can be done ?

Attachments modules.jpg 13.99 KB
2
Contributors
1
Reply
17
Views
3 Years
Discussion Span
Last Post by diafol
0
header('Content-Type: text/html; charset=utf-8');

$XML = simplexml_load_file('temp.xml');
$hold = array();

foreach($XML->testcase as $testcase)
{
    $scriptBits = explode("/",$testcase->script);
    $parent = $scriptBits[2];
    $sub = end($scriptBits);
    $hold[$parent][] = substr(trim($sub),0,-4); 
}

$json = json_encode($hold);

$output = "";
foreach($hold as $k=>$v) $output .= "<option value='$k'>$k</option>";

?>


<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form>
    <select id="first">
        <?php echo $output;?>
    </select>
    <select id="second">
    </select>
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    var data = <?php echo $json;?>;

    $('#module').change(function(){
        fillSecond();
    });

    function fillSecond()
    {
        var first = $('#module').val();
        var output = '';
        $.each(data[first], function(i,v){
            output += '<option value="'+ v + '">' + v + '</option>';  
        });
        $('#second').html(output);
    }

    fillSecond();
</script>
</body>
</html>

An example of the type of thing I think you need.

Note I enclosed the XML snippet with a container tag <channel> so that testcase could be looped easily.

<?xml version="1.0" encoding="utf-8"?>
<channel>
    <testcase>
             (content)                                                          </testcase>

    <testcase>
        (content)
    </testcase>
</channel>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.