Search code examples
arraysjsonrestdropdownbox

Create drop down list from json/xml returned arrary


I am trying to make a drop down list by food_id and food_name from the array returned by Fatsecret REST API but having no success.

My code is

    <?php
    $base = rawurlencode("GET")."&";
    $base .= "http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&";
    $params = "format=json&";
    $params = "method=foods.search&";
    $params .= "oauth_consumer_key=my key&";
    $params .= "oauth_nonce=123&";
    $params .= "oauth_signature_method=HMAC-SHA1&";
    $params .= "oauth_timestamp=".time()."&";
    $params .= "oauth_version=1.0&";
    $params .= "search_expression=flour";
    $params2 = rawurlencode($params);
    $base .= $params2;
    //encrypt it!
    $sig= base64_encode(hash_hmac('sha1', $base, "4e0b6b00578b4a8995975b289879ae4e&", true));
    $url = "http://platform.fatsecret.com/rest/server.api?".$params."&oauth_signature=".rawurlencode($sig);

The returned array object is

    object(SimpleXMLElement)#555 (4) { 
    ["max_results"]=> string(2) "20" 
    ["total_results"]=> string(3) "186" 
    ["page_number"]=> string(1) "0" 
    ["food"]=> array(20) { 
    [0]=> object(SimpleXMLElement)#549 (5) { 
    ["food_id"]=> string(4) "3419" 
    ["food_name"]=> string(11) "White Flour" 
    ["food_type"]=> string(7) "Generic" 
    ["food_url"]=> string(63) "http://www.fatsecret.com/calories-nutrition/generic/flour-white" 
    ["food_description"]=> string(75) "Per 100g - Calories: 364kcal | Fat: 0.98g | Carbs: 76.31g | Protein: 10.33g" } 
    [1]=> object(SimpleXMLElement)#554 (6) { 
    ["food_id"]=> string(7) "1727595" 
    ["food_name"]=> string(17) "All-Purpose Flour" 
    ["brand_name"]=> string(10) "Gold Medal" 
    ["food_type"]=> string(5) "Brand" 
    ["food_url"]=> string(72) "http://www.fatsecret.com/calories-nutrition/gold-medal/all-purpose-flour" 
    ["food_description"]=> string(77) "Per 1/4 cup - Calories: 100kcal | Fat: 0.00g | Carbs: 22.00g | Protein: 3.00g" } 
    [2]=> object(SimpleXMLElement)#553 (6) { 
    ["food_id"]=> string(5) "56342" 
    ["food_name"]=> string(17) "All Purpose Flour" 
    ["brand_name"]=> string(7) "Wegmans" 
    ["food_type"]=> string(5) "Brand" 
    ["food_url"]=> string(69) "http://www.fatsecret.com/calories-nutrition/wegmans/all-purpose-flour" 
    ["food_description"]=> string(78) "Per 1/4 cup - Calories: 100kcal | Fat: 0.00g | Carbs: 22.00g | Protein: 4.00g" } 

at this point I tried the following code

    echo '<select>';

    $jsonData = file_get_contents($url); 
    $jsonDataObject = json_decode($jsonData);

    foreach($jsonDataObject->response->values as $option){
    echo '<option value=' . $option->food_id . '>' . $option->food_name . '</option>';
    }

    echo '</select>';

but had no luck.

eventually I tried

    $food_feed = file_get_contents($url);
    $food = simplexml_load_string($food_feed);
    $obj_xml = simplexml_load_string($food_feed);
    // Total number of elements present ///
    $total = $obj_xml->count(); // total number of elements for PHP 5.3 and above
    $str="<select name='student'>";
    for($i=0; $i<$total; $i++) {
    $str= $str . "<option value=".$obj_xml->food[$i]->food_id.">".$obj_xml->food[$i]->food_name. "</option>";
    }
    $str = $str. "</select>";
    echo $str;

this code did work but it adds couple of extra blank line at the end of the drop down, for which I cant find a reason.

Would appreciate if any one can help me to find a better way to achieve the goal.

Regards


Solution

  • Ok, I have managed to get this solved, just in case if this can help someone.

    $food would be a simplexml object, we can access elements like

    foreach( $food->{'[array]'} as $array_variable )
    

    so our loop will become like

        $food = simplexml_load_string($food_feed);
        //print_r($food);
        if ($food->total_results != "0") {
        foreach( $food->{'food'} as $food_entry ) {
        echo $food_entry->food_id. "<br />\n";
        echo $food_entry->food_name. "<br />\n";
        echo $food_entry->food_description. "<br />\n";
        echo "<br />\n";
        }
    
        }
        else { //do some thing else
    
        }