[ASK] decode json di Codeigniter


Status
Not open for further replies.

artayoga

Beginner 2.0
permisi, mau tanya kalo decode json di CI untuk dimasukin ke select button gmn ya?
misalkan saya punya 1 halaman view dan controler

1 halaman views:
Code:
<select name="service_name" id="service_name">
        ???
</select>
jQuery(document).ready(function(){

       var pathparts = location.pathname.split('/');
       if (location.host == 'localhost') {
           var base_url = location.origin+'/'+pathparts[1].trim('/');
       }else{
           var base_url = location.origin;
       }

       var productDetails = {
           "xxx":"xxx",
           "xxx":"xxx",
           "xxx":"xxx",
           "xxx":"xxx",
           "xxx":"xxx"
       };

       $.ajax({
           type: "POST",
           url: base_url+'/backend/orders/product',
           async: true,
            dataType: 'json',
           data: productDetails,
           success: function(data){
              
               ????
              
           }

       });

   });

controller :
Code:
$curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "xxx",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => xxx",
            CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache",
                "content-type: application/json",
            ),
        ));
       
        $response = curl_exec($curl);
        $err = curl_error($curl);
       
        curl_close($curl);
       
        if ($err) {

          echo "cURL Error #:" . $err;

        } else {

            echo json_encode($response, true);
           
        }

json response :
Code:
"{
\"xx\":\xx\",
\"xx\":\"xx\",
\"xx\":\"xx\",
\"xx\":\"xx\",
\"xx\":xx,
\"fruit_name\":
    [ {
        \"product_name\" : \"product1\",
        \"product_code\" : \"code1\",
        \"price\" : price1,
        \"etd\" : \"1-1\",
        \"time\" : \"Day\"},
        {\"product_name\" : \"product2\",
        \"product_code\" : \"code2\",
        \"price\" : price2,
        \"etd\" : \"3-3\",
        \"time\" : \"Day\"},
        {\"product_name\" : \"product3\",
        \"product_code\" : \"code3\",
        \"price\" : price3,
        \"distance\" : 0,
        \"etd\" : \"11-11\",
        \"time\" : \"Day\"
    }]
}"

saya mau masukin perulangan product_name dari json itu ke select button. gimana caranya ya Tuan? terima kasih atas bantuannya
 
Last edited:
Status
Not open for further replies.

Top