从数据库中检索PHP数组 - AutoCompletion JQUERY

时间:2015-08-21 16:48:21

标签: php jquery autocomplete

尝试从数据库中检索$ cities中的数据,而不是对其进行硬编码并将其插入$ cities数组中 - 任何关于如何执行此操作的想法......

这是使用JQUERY Autocomplete。

我需要格式完全相同......

这是代码......

<?php

 // Basic oci_connect() database connection

    Connect to DB


   //retrieve the search term that autocomplete sends
   $term = trim(strip_tags($_GET['term']));


   $cities = array(
      array('city'=>'New York', state=>'NY'),
      array('city'=>'Los Angeles', state=>'CA'),
      array('city'=>'Chicago', state=>'IL'),
      array('city'=>'Houston', state=>'TX'),
      array('city'=>'Phoenix', state=>'AZ'),
      array('city'=>'Philadelphia', state=>'PA'),
      array('city'=>'San Antonio', state=>'TX'),
      array('city'=>'Dallas', state=>'TX'),
      array('city'=>'San Diego', state=>'CA'),
      array('city'=>'San Jose', state=>'CA'),
      array('city'=>'Detroit', state=>'MI'),
      array('city'=>'San Francisco', state=>'CA'),
      array('city'=>'Jacksonville', state=>'FL'),
      array('city'=>'Indianapolis', state=>'IN'),
      array('city'=>'Austin', state=>'TX'),
      array('city'=>'Columbus', state=>'OH'),
      array('city'=>'Fort Worth', state=>'TX'),
      array('city'=>'Charlotte', state=>'NC'),
      array('city'=>'Memphis', state=>'TN'),
      array('city'=>'Baltimore', state=>'MD'),
 );


// Rudimentary search
   $matches = array();
   foreach($cities as $city){
    if(stripos($city['city'], $term) !== false){
         // Add the necessary "value" and "label" fields and append to result set
      $city['value'] = $city['city'];
      $city['label'] = "{$city['city']} - {$city['state']}";
      $matches[] = $city;
  }
}

// Truncate, encode and return the results
$matches = array_slice($matches, 0, 5);
print json_encode($matches);

我在$ term

之后尝试了这段代码
$sql = oci_parse($conn, "SELECT CITY, STATE from SCHEMA.TABLE FROM "
. "WHERE CITY LIKE '%".$term."%' ");

oci_execute($sql);

while ($row = oci_fetch_array($sql, OCI_ASSOC+OCI_RETURN_NULLS)) 
{
    $row['CITY']=htmlentities(stripslashes($row['CITY']));
    $row['STATE']=$row['STATE'];
    $row_set[] = $row;//build an array
}

但它不起作用

0 个答案:

没有答案