Foros del Web » Programando para Internet » PHP »

If en AutoSuggest

Estas en el tema de If en AutoSuggest en el foro de PHP en Foros del Web. Hola chicos, yo de nuevo xD Veran, tengo un AutoSuggest basado en PHP - MYSQL y Ajax, al escribir algo en el textield de un ...
  #1 (permalink)  
Antiguo 06/10/2009, 13:35
Avatar de JessicaTJ  
Fecha de Ingreso: enero-2007
Ubicación: 127.0.0.1
Mensajes: 472
Antigüedad: 17 años, 3 meses
Puntos: 25
If en AutoSuggest

Hola chicos, yo de nuevo xD

Veran, tengo un AutoSuggest basado en PHP - MYSQL y Ajax, al escribir algo en el textield de un buscador, dependiendo la letra o numero que se teclee, hace una consulta a MYSQL y va dando resultados parecidos a los ke el usuario va tecleando.

Hasta aqui todo bien, aki esta el codigo del sistema:
Código PHP:
<?php    
    
// PHP5 Implementation - uses MySQLi.
    // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
    
$db = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
    
    if(!
$db) {
        
// Show error if we cannot connect.
        
echo 'ERROR: Could not connect to the database.';
    } else {
        
// Is there a posted query string?
        
if(isset($_POST['queryString'])) {
            
$queryString $db->real_escape_string($_POST['queryString']);
            
            
// Is the string length greater than 0?
            
            
if(strlen($queryString) >0) {
                
// Run the query: We use LIKE '$queryString%'
                // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
                // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
                
                
$query $db->query("SELECT * FROM pro WHERE pn LIKE '$queryString%' LIMIT 15");
                if(
$query) {
                    
// While there are results loop through them - fetching an Object (i like PHP5 btw!).
                    
while ($result $query ->fetch_object()) {
                        
// Format the results, im using <li> for the list, you can change it.
                        // The onClick function fills the textbox with the result.
                        
                        // YOU MUST CHANGE: $result->value to $result->your_column
                        // AutoFill Search Field:
                         
echo '<li><a href="search.php?Search='.$result->part.'">'.$result->part.'</a></li>';
                     }
                } else {
                    echo 
'ERROR: There was a problem with the query.';
                }
            } else {
                
// Dont do anything.
            
// There is a queryString.
        
} else {
            echo 
'There should be no direct access to this script!';
        }
    }
?>
Ahora mi pregunta, lo ke me gustaria hacer, es ke si no hay resultados respecto a lo ke el usuario busca, regrese un mensaje que diga:

Código:
No encontrado, por favor contactanos
Se ke tengo que usar un IF y un ELSE, pero, mi pregunta es, donde en todo el codigo coloco esto? Ya hice varias pruebas y solo logro trabar el codigo xD

Podrian plis ayudarme en esto?

De corazon les agradecere mucho su ayuda.
  #2 (permalink)  
Antiguo 06/10/2009, 13:44
Avatar de gjx2  
Fecha de Ingreso: agosto-2008
Ubicación: R.D
Mensajes: 1.153
Antigüedad: 15 años, 8 meses
Puntos: 139
Respuesta: If en AutoSuggest

Bueno pues seria algo haci
ubica la parte donde tienes el if($query)


Código PHP:
Ver original
  1. if($query) { // BUSCA ESTA PARTE EN TU CODIGO
  2.              
  3.                 if(mysql_num_rows() > 0){
  4.                     // While there are results loop through them - fetching an Object (i like PHP5 btw!).
  5.                     while ($result = $query ->fetch_object()) {
  6.                         // Format the results, im using <li> for the list, you can change it.
  7.                         // The onClick function fills the textbox with the result.
  8.                          
  9.                         // YOU MUST CHANGE: $result->value to $result->your_column
  10.                         // AutoFill Search Field:
  11.                          echo '<li><a href="search.php?Search='.$result->part.'">'.$result->part.'</a></li>';
  12.                      }
  13.                  
  14.                  }else{
  15.                  
  16.                     echo "<script>alert('No encontrado, por favor contactanos');</script>";
  17.                  
  18.                  }
  19.                  
  20.                  
  21.                  
  22.                 } else {
  23.                     echo 'ERROR: There was a problem with the query.';
  24.                 }
  #3 (permalink)  
Antiguo 06/10/2009, 13:44
 
Fecha de Ingreso: abril-2008
Mensajes: 453
Antigüedad: 16 años
Puntos: 16
Respuesta: If en AutoSuggest

proba con algo asi
Código php:
Ver original
  1. <?php    
  2.     // PHP5 Implementation - uses MySQLi.
  3.     // mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');
  4.     $db = new mysqli(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
  5.    
  6.     if(!$db) {
  7.         // Show error if we cannot connect.
  8.         echo 'ERROR: Could not connect to the database.';
  9.     } else {
  10.         // Is there a posted query string?
  11.         if(isset($_POST['queryString'])) {
  12.             $queryString = $db->real_escape_string($_POST['queryString']);
  13.            
  14.             // Is the string length greater than 0?
  15.            
  16.             if(strlen($queryString) >0) {
  17.                 // Run the query: We use LIKE '$queryString%'
  18.                 // YOU NEED TO ALTER THE QUERY TO MATCH YOUR DATABASE.
  19.                 // eg: SELECT yourColumnName FROM yourTable WHERE yourColumnName LIKE '$queryString%' LIMIT 10
  20.                
  21.                 $query = $db->query("SELECT * FROM pro WHERE pn LIKE '$queryString%' LIMIT 15");
  22.                 if($query) {
  23.                     // While there are results loop through them - fetching an Object (i like PHP5 btw!).
  24.                     $num = $query->num_rows;
  25.                     if($num>0){
  26.                         while ($result = $query ->fetch_object()) {
  27.                             // Format the results, im using <li> for the list, you can change it.
  28.                             // The onClick function fills the textbox with the result.
  29.                            
  30.                             // YOU MUST CHANGE: $result->value to $result->your_column
  31.                             // AutoFill Search Field:
  32.                              echo '<li><a href="search.php?Search='.$result->part.'">'.$result->part.'</a></li>';
  33.                          }
  34.                     }else{
  35.                         echo 'No encontrado, por favor contactanos';
  36.                 } else {
  37.                     echo 'ERROR: There was a problem with the query.';
  38.                 }
  39.             } else {
  40.                 // Dont do anything.
  41.             } // There is a queryString.
  42.         } else {
  43.             echo 'There should be no direct access to this script!';
  44.         }
  45.     }
  46. ?>

le agregue un num_rows para saber si la consulta devuelve un numero mayor a 0 en el caso contrario mandara el mensaje
  #4 (permalink)  
Antiguo 06/10/2009, 13:59
Avatar de JessicaTJ  
Fecha de Ingreso: enero-2007
Ubicación: 127.0.0.1
Mensajes: 472
Antigüedad: 17 años, 3 meses
Puntos: 25
Respuesta: If en AutoSuggest

Hola Samu22, de nuevo salvandome

Nada mas falto cerrar el echo:

Código PHP:
}else{
echo 
'No encontrado, por favor contactanos'
Le falto un } al final, ya estoy aprendiendo xD

Mil gracias de nuevo !!!!
  #5 (permalink)  
Antiguo 06/10/2009, 14:01
 
Fecha de Ingreso: abril-2008
Mensajes: 453
Antigüedad: 16 años
Puntos: 16
Respuesta: If en AutoSuggest

Cita:
Iniciado por JessicaTJ Ver Mensaje
Hola Samu22, de nuevo salvandome

Nada mas falto cerrar el echo:

Código PHP:
}else{
echo 
'No encontrado, por favor contactanos'
Le falto un } al final, ya estoy aprendiendo xD

Mil gracias de nuevo !!!!
si si jajaja me colgue
  #6 (permalink)  
Antiguo 06/10/2009, 14:08
Avatar de JessicaTJ  
Fecha de Ingreso: enero-2007
Ubicación: 127.0.0.1
Mensajes: 472
Antigüedad: 17 años, 3 meses
Puntos: 25
Respuesta: If en AutoSuggest

xD lo bueno es ke descubri el problema si no ya estuviera de enfadosa otra vez xD

Mil gracias de nuevo !!!
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:10.