6

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;
              }
            })


        }
2
  • Are you using javascript and PHP on same page? Commented Oct 21, 2015 at 6:17
  • yes both are in one page Commented Oct 21, 2015 at 6:18

5 Answers 5

3

Please replace

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

with

var bankSource = <?php echo json_encode($bnkArray); ?>;
Sign up to request clarification or add additional context in comments.

7 Comments

after changing the var bankSource = <?php echo json_encode($bnkArray); ?>; the auto complete is showing empty results
Does the generated javascript array looks okay? Are there any errors in your console?
there is not any errors in the console, i am confusing with the array
please check the my auto complete code to i have update the codes
Why are there double "[" and "]"s? Please remove the braces around <?php echo json_encode($bnkArray); ?>.
|
2

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>

2 Comments

if the [ ] remove in the bankSource11 variable it is working.. thank you for your effort
@NSK If you find this answer helpful Mark it as accepted and upvote. So, other can also get confidence.
1

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.

Comments

0

You should do following :

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

Comments

0

Note the single quotes, because it's string:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.