Ciao Raga, urge aiuto prima di impazzire.
Stò cercando di fare funzionare l'autocomplete di jquery (remote + json) con chiamata al db MySQL tramite script php.
In realtà funziona tutto, il problema che ho è che l'autocomplete non visualizza tutto il contenuto della query, invece se richiamo direttamente la pagina.php vengono fuori tutti i dati che chiedo, per esempio se inserisco dei nomi che iniziano con HI non viene fuori nulla, invece richiamando direttamente la pag.php si.
Un'altra cosa che ho notato è la lentezza nell'output, c'è forse qualcosa accorgimento che dovrei prendere ?
Questo è i codice utilizzato:
<HEAD>
oltre ai seguenti link js (jquery, core, position, autocomplete, widget )
ho inserito questa func
codice:
<script>
$(function() {
$( "#i_search" ).autocomplete({
source: "inc/i_search.php",
minLength: 2
});
});
</script>
<BODY>
codice:
<label for="i_search">Ragione sociale cliente</label><input name="ragsoc" id="i_search" type="text" />
Pagina PHP
Codice PHP:
<?php
include('condb.php');
include('mysql.class.php');
$q = strtoupper(trim($_GET["term"]));
if (!$q) return;
$items = $db->getArrayRow( "SELECT
id,
nome AS label,
full_name AS value
FROM tabella
WHERE label LIKE '%$q%'" );
function array_to_json( $array ){
if( !is_array( $array ) ){
return false;
}
$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
if( $associative ){
$construct = array();
foreach( $array as $key => $value ){
// We first copy each key/value pair into a staging array,
// formatting each key and value properly as we go.
// Format the key:
if( is_numeric($key) ){
$key = "key_$key";
}
$key = "\"".addslashes($key)."\"";
// Format the value:
if( is_array( $value )){
$value = array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "\"".addslashes($value)."\"";
}
// Add to staging array:
$construct[] = "$key: $value";
}
// Then we collapse the staging array into the JSON form:
$result = "{ " . implode( ", ", $construct ) . " }";
} else { // If the array is a vector (not associative):
$construct = array();
foreach( $array as $value ){
// Format the value:
if( is_array( $value )){
$value = array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "'".addslashes($value)."'";
}
// Add to staging array:
$construct[] = $value;
}
// Then we collapse the staging array into the JSON form:
$result = "[ " . implode( ", ", $construct ) . " ]";
}
return $result;
}
$result = $items;
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
}
if (count($result) > 11)
break;
}
echo array_to_json($result);
?>
many thanks 
.