Mysql db result converts as json array

I am trying to write an autocomplete that automatically fills the loading elements once when the PHP page loads. With elements that are retrieved from mysql database, I created 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 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 name this array like this in my auto completion it doesn't work

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

what kind of array it is. how to do it.

it's part of autocomplete

 $(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; } }) } 
+5
source share
5 answers

Please replace

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

with

 var bankSource = <?php echo json_encode($bnkArray); ?>; 
+2
source

Here is an example that I modified, adapting to your requirement. Please study it and let me know if you have any questions.

  <?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> 
+2
source

change this line as follows:

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

to

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

The php variable is enclosed in quotation marks and is assigned to the js variable.

+1
source

You should do the following:

 var bankSource11 = [<?php echo json_encode($bnkArray); ?>]; 
0
source

Pay attention to single quotes, because the line:

 var bankSource = '<?php echo json_encode($bnkArray); ?>'; 
0
source

Source: https://habr.com/ru/post/1234164/


All Articles