Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Convertire array in json

    Ciao a tutti sto elaborando un autocomplete su un campo e per far uscire i risultati mi serve un array in formato jSon.

    Il codice che utilizzo è il seguente

    Codice PHP:
    $q mysql_escape_string($_REQUEST['q']);

    $query "SELECT UtenteID, UtenteNome FROM Utente WHERE UtenteNome LIKE '%".$q."%'";

    $rs mysql_query($query) or die(mysql_error());
    $nr mysql_num_rows($rs);

    $empty = array();
    while(
    $rows mysql_fetch_array($rs)){
        
    $empty["ID"] = $rows['UtenteID'];
        
    $empty["Nome"] = $rows['UtenteNome'];
        
    $empty["Cognome"] = $rows['UtenteCognome'];
    }
    $json_response json_encode($empty);
    echo 
    $json_response
    il risultato che stampo è il seguente
    codice:
    {"ID":"1108","Nome":"Francesco","Cognome":null}
    Deduco che non funzioni correttamente in quanto mancano le parentesi [ nel risultato.

    Come posso modificare il codice per far si che funzioni

  2. #2
    Se ho ben capito cambi il ciclo while in questo modo:

    Codice PHP:
    while ( $rows mysql_fetch_array($rs) ) {
        
    $empty [] = array (
            
    "ID" => $rows ['UtenteID'], 
            
    "Nome" => $rows ['UtenteNome'], 
            
    "Cognome" => $rows ['UtenteCognome'] );

    CODENCODE \ Branding \ Design \ Marketing
    www.codencode.it

  3. #3
    Grazie mille ora devo capire come mai non mi li visualizza a video

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.