Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/01/2020, 16:10
madison_sg
 
Fecha de Ingreso: noviembre-2015
Mensajes: 77
Antigüedad: 8 años, 5 meses
Puntos: 2
Buscador hecho en php con peticion ajax

Hola, tengo un buscador que funciona perfectamente bien para buscar registros en la base de datos, este lo tengo hecho con html, javascript, php y la peticion Ajax para devolver resultados asincronos, sin embargo; al buscar un registro que no encuentra y al borrar del input los datos de busqueda, me devuelve todos los resultados que se encuentra en la base de datos. No seria ningun problema si nada mas contara con 100 o 200 registros en la base de datos, el problema viene cuando recupera 1,000,000 que no quiero de regeso porque al hacer el cambio con la peticion de ajax en el div, es evidente que se queda el nuevo div que trajo la vista del ajax. No se si me explique bien, pero en resumen, quiero que si no encuentra ninguna coincidencia o ningun resultado, simplemente se quede puesto el colspan que indica que no hay registros.
Este es mi codigo html:
Código HTML:
<section>
          <table border="0" align="center">
              <tr>
                  <td width="335"><input type="text" placeholder="Search by company name" id="bs-prod" /></td>
              </tr>
          </table>
       </section> 
A traves del input realizo la peticion que en el codigo de php se llama 'dato'
Código PHP:
include('conexion.php');

$dato $_POST['dato'];

//EJECUTAMOS LA CONSULTA DE BUSQUEDA
if ($dato == $_POST['dato']) {
  

$registro pg_query($con"SELECT cm.*, pgc.* FROM company_main cm, parent_group_catalog pgc where cm.parentgroup = pgc.groupID and cm.companyName like '%$dato%'");

//CREAMOS NUESTRA VISTA Y LA DEVOLVEMOS AL AJAX

echo '<table class="table table-striped table-condensed table-hover" id="ordCons">
            <tr>
                   <th width="50">ID</th>
                <th width="100">Company Name</th>
                <th width="150">Activity Description</th>
                <th width="150">Public or Private</th>
                <th width="150">Address</th>
                <th width="100">Parent Group</th>
                <th width="50">Is Client</th>
                <th width="50">Country</th>
                <th width="50">GRP TaxID:</th>
                <th width="50">Activities:</th>
                <th width="50">Tax ID:</th>
            </tr>'
;
if(
pg_num_rows($registro)>0){
      while(
$registro2 pg_fetch_array($registro)){
            echo 
'<tr>
                        <td>'
.$registro2['0'].'</td>
                        <td>'
.$registro2['1'].'</td>
                        <td>'
.$registro2['6'].'</td>
                        <td>'
.$registro2['8'].'</td>
                        <td>'
.$registro2['9'].'</td>
                        <td>'
.$registro2['14'].'</td>';
                        if (
$registro2['10'] == 'true') {
                          echo 
"<td>Yes</td>";
                        }else{
                          echo 
"<td>No</td>";
                        }
echo 
'                  <td>'.$registro2['5'].'</td>
                        <td>'
.$registro2['16'].'</td>
                        <td>'
.$registro2['18'].'</td>
                        <td>'
.$registro2['3'].'</td>
                        
                        
                        </tr>'
;
      }
}else{
      echo 
'<tr>
                        <td colspan="6">There is not more records</td>
                  </tr>'
;
}
}elseif(
$dato == '') {
  echo 
'<tr>
                        <td colspan="6">There is not more records</td>
                  </tr>'
;
}
echo 
'</table>'
En php le puse una condicion if, elseif que no funciona, sigue trayendo lo que no quiero
y este es mi codigo javascript de la peticion ajax
Código:
$(function() {
    
    $('#bs-prod').on('keyup', function() {
        var dato = $('#bs-prod').val();
        var url = '../php/busca_registro.php';
        $.ajax({
            type: 'POST',
            url: url,
            data: 'dato=' + dato,
            success: function(datos) {
                $('#agrega-registros').html(datos);
            }
        });
        return false;
    });
});
}
Alguien que me pueda ayudar por favor.
__________________
paco alonso