Smudger
Smudger

Reputation: 10791

remove apostrophe from json returned sql data + codeigniter

I have the below field in my database query:

REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade,

this is then returned to the view from codeigniters model and controller:

Model:

function get_skugrade_grade($q)
{
    $Query="select
              min(grade) as family
            from (SELECT
                    REPLACE(ProductCode,concat((width*1000),(thickness*1000),REPLACE((convert(varchar,convert(decimal(8,1),length))),'.','')),'') as grade,                     
                  FROM database.dbo.ProductList) as p
            where Options like '%$q%'";
    $result = $this->db->query($Query)->row();
    return $result->family;
}

Controller

function get_skufamily_grades(){
    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $viewData   =   $this->Sales_model->get_skugrade_grade($q);
        $data_json = json_encode($viewData);
        echo $data_json;
    }
}

View request:

$.post('get_skufamily_grades', {data:selectedObj.value},function(result) 
 { 
 $('input[name^="skugrade_1"]').val(result);
 });

The output to the view is "CR". How can I remove the two apostrophe's ""

the database result simply has CR. I think my jquery / json is addint these in as it is not a number being returned.

current server response

enter image description here Thanks as always.

Upvotes: 0

Views: 562

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

the "" is added by json encoding, valid json format is { "key" : "value" } where the key and values are enclosed within " for values other than Number and Boolean.

If you configure your ajax request to handle json request you can solve this problem, with dataType: "json"

Update
I'm not a PHP guy, still tryy something like

function get_skufamily_grades(){
    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $viewData   =   $this->Sales_model->get_skugrade_grade($q);

        $arr = array('family' => $viewData);
        echo json_encode($arr);
    }
}

Then change the ajax request to

$.post('get_skufamily_grades', {
    data : selectedObj.value
}, function(result) {
    $('input[name^="skugrade_1"]').val(result.family);
}, 'json');

Upvotes: 1

Related Questions