Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql db result convert as a json array

Tags:

json

arrays

php

I am trying to write a auto complete, which the auto complete items load one time when the PHP page loads. With the items that are fetched from mysql DB , i have create a json array like this

<?php
$bnkArray = array();
$sql_bnk = mysql_query("SELECT BName, BCode, ID  FROM bank");
while($rBnk = mysql_fetch_array($sql_bnk)){

    $bnkDet = array(
        'label' => $rBnk['BName'],
        'value' => $rBnk['BName'],
        'otherDetails' => $rBnk['BName'].'||'. $rBnk['BCode'].'||'. $rBnk['ID'] 
    );  
    array_push($bnkArray, $bnkDet);
}

?>

i need this array as like this javascript array

<script>

var bankSource11 = [
      {
        value: "jquery",
        label: "jQuery",
        otherDetails: "the write less, do more, JavaScript library",

      },
      {
        value: "jquery-ui",
        label: "jQuery UI",
        otherDetails: "the official user interface library for jQuery",

      },
      {
        value: "sizzlejs",
        label: "Sizzle JS",
        otherDetails: "a pure-JavaScript CSS selector engine",

      }
    ];
</script>

if i call this array like this in my auto complete this is not working

var bankSource = [<?php echo  $bnkArray; ?>];

what is this array type.. how to do this.

this is the autocomplete part

$(this).autocomplete({
            minLength: 0,
            source: bankSource,
            focus: function( event, ui ) {
                $(this).val( ui.item.label );
                return false;
            },
            select: function( event, ui ) {
                console.log(ui.item.value +' ____ ' + ui.item.otherDetails);
                $( "#project" ).val( ui.item.label );
                $( "#project-id" ).val( ui.item.value );
                $( "#project-description" ).html( ui.item.otherDetails );

                return false;
              }
            })


        }
like image 844
Nasik Ahd Avatar asked Oct 21 '15 06:10

Nasik Ahd


3 Answers

Please replace

var bankSource = [<?php echo  $bnkArray; ?>];

with

var bankSource = <?php echo json_encode($bnkArray); ?>;
like image 130
mario.van.zadel Avatar answered Oct 04 '22 04:10

mario.van.zadel


Here is a sample example which i modified accroding to your requirement. Kindly study it and let me know if you have any question.

 <?php
    $bnkArray = array();
    $sql_bnk = mysql_query("SELECT BName, BCode, ID  FROM bank");
    while($rBnk = mysql_fetch_array($sql_bnk)){

        $bnkDet = array(
            'label' => $rBnk['BName'],
            'value' => $rBnk['BName'],
            'otherDetails' => $rBnk['BName'].'||'. $rBnk['BCode'].'||'. $rBnk['ID'] 
        );  
        array_push($bnkArray, $bnkDet);
    }

    ?>


    <head>
      <meta charset="utf-8">
      <title>jQuery UI Autocomplete - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script>
      $(function() {
        var bankSource11  = <?php echo json_encode($bnkArray); ?>;
        $( "#My_Input" ).autocomplete({
          source: bankSource11  
        });
      });
      </script>
    </head>
    <body>

    <div class="ui-widget">
      <input id="My_Input">
    </div>
like image 20
sandeepsure Avatar answered Oct 04 '22 04:10

sandeepsure


change this line like this,

var bankSource = [<?php echo  $bnkArray; ?>];

to

var bankSource = "<?php echo  json_encode($bnkArray); ?>";

Php variable is enclosed with quotes and assigned to js variable.

like image 37
Niranjan N Raju Avatar answered Oct 04 '22 04:10

Niranjan N Raju