Mark Worsnop
Mark Worsnop

Reputation: 4467

loadJSON convert boolean to true or false from mysql

I am loading boolean data from mySql. the db is 0 or 1. However JSON wants true or false for a form to load properly. I have 30 fields in the actual table. And yes I can convert the 0 1 to true false in the select using a case statement. But there must be an easier way to do this?

I have lots of boolean fields in the actual production. Maybe JSON doesn't handle this and only handles plain type="text"?

Otherwise I can load it all with PHP and set the values on load

Here is the original JSON

[{"drscomplete":"0"}]

and this is what I need for the checkbox to work properly on a form

[{"drscomplete":"true"}]

Here is the load php

$SQL ="SELECT * from dealfinalize d where d.ID=" . $id;

  $resultArray = array();

  $result = mysqli_query($con,$SQL);

    if($result->num_rows >0 ) 
     $resultArray = mysqli_fetch_all($result,MYSQLI_ASSOC);

echo json_encode($resultArray);

and here is the ajax

jQuery.ajax({
    url: "dealfinalizeload.php",
    data:'id='+id,
    type: "POST",
    success:function(data){

            var data2 = $.parseJSON(data);
            $('div#white_content_dealfinalize').loadJSON(data2);

            alert(data);

    }, 
    error:function (){
        alert("Error loading tasks");
        }
    });

Upvotes: 0

Views: 1084

Answers (1)

Santosh Ram Kunjir
Santosh Ram Kunjir

Reputation: 1082

You can use case

   select t1.column1,
      case when t2.column == 1
        then 'true'
        else 'false'
      end OrderedAll
    from yourtable t1

Upvotes: 1

Related Questions