how to create conditioned search syntax in select

1

It turns out that I want to do a search through ajax where I have three fields one is idpaciente another is cedula and the other is name.

What I want to do is to search first by idpaciente if it does not find search by cedula and if it does not find a cedula search like ´%$nombre%´ to launch the results more sercanos of the name sent but it turns out that in my select only responds to the last Search condition by name and use separator or below I leave the code so you have an idea.

<?php 
include('../conexion.php');
$idp=$_POST['idp']; #idpaciente
$cd=$_POST['cd'];   #cedula
$n=$_POST['n'];     #nombre
?>
<table class="tblmicentro">
<tr>
<th>idp</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Nacionalidad</th>
<th>Cedula</th>
<th>acciones</th>
</tr>
<?php 
$n2="0";
$sql2="SELECT * FROM perfil_paciente where (idpaciente='$idp' or cedula='$cd') or nombres like '%$n%'";
$result2=mysqli_query($conexion, $sql2);
while ($m2=mysqli_fetch_assoc($result2)) {
$n2++;
?>
<tr>
<td><span class="numeracion"><?php echo $n2 ?></span></td>
<td><span class="numeracion"><?php echo $m2['idpaciente'] ?></span></td>
<td><?php echo $m2['nombres'] ?></td>
<td><?php echo $m2['apellidos'] ?></td>
<td><?php echo $m2['nacionalidad'] ?></td>
<td></td>
</tr>
<?php 
} 
?>
</table>

thanks.

    
asked by Starking Elyos 13.04.2018 в 14:21
source

1 answer

0

You can do so, I have commented on the code that I added:

    <?php 
    include('../conexion.php');
    $idp=$_POST['idp']; #idpaciente
    $cd=$_POST['cd'];   #cedula
    $n=$_POST['n'];     #nombre
    ?>
    <table class="tblmicentro">
    <tr>
    <th>idp</th>
    <th>Nombre</th>
    <th>Apellidos</th>
    <th>Nacionalidad</th>
    <th>Cedula</th>
    <th>acciones</th>
    </tr>
    <?php 
    $n2="0";
    $sql2="SELECT * FROM perfil_paciente where idpaciente='$idp'";
    $result2=mysqli_query($conexion, $sql2);
if(mysqli_fetch_assoc($result2)){//si obtenemos datos adelante
while ($m2=mysqli_fetch_assoc($result2)) {
    $n2++;
    ?>
    <tr>
    <td><span class="numeracion"><?php echo $n2 ?></span></td>
    <td><span class="numeracion"><?php echo $m2['idpaciente'] ?></span></td>
    <td><?php echo $m2['nombres'] ?></td>
    <td><?php echo $m2['apellidos'] ?></td>
    <td><?php echo $m2['nacionalidad'] ?></td>
    <td></td>
    </tr>
    <?php 
    } 
}else{//sino probamos la segunda opción
$n2="0";
    $sql2="SELECT * FROM perfil_paciente where cedula='$cd'";
    $result2=mysqli_query($conexion, $sql2);
if(mysqli_fetch_assoc($result2)){//si obtenemos datos perfecto
while ($m2=mysqli_fetch_assoc($result2)) {
    $n2++;
    ?>
    <tr>
    <td><span class="numeracion"><?php echo $n2 ?></span></td>
    <td><span class="numeracion"><?php echo $m2['idpaciente'] ?></span></td>
    <td><?php echo $m2['nombres'] ?></td>
    <td><?php echo $m2['apellidos'] ?></td>
    <td><?php echo $m2['nacionalidad'] ?></td>
    <td></td>
    </tr>
    <?php 
    } 
}else{//si no obtenemos datos, probamos la tercera y ultima opcion
$n2="0";
    $sql2="SELECT * FROM perfil_paciente where nombres like '%$n%'";
    $result2=mysqli_query($conexion, $sql2);
while ($m2=mysqli_fetch_assoc($result2)) {
    $n2++;
    ?>
    <tr>
    <td><span class="numeracion"><?php echo $n2 ?></span></td>
    <td><span class="numeracion"><?php echo $m2['idpaciente'] ?></span></td>
    <td><?php echo $m2['nombres'] ?></td>
    <td><?php echo $m2['apellidos'] ?></td>
    <td><?php echo $m2['nacionalidad'] ?></td>
    <td></td>
    </tr>
    <?php 
    } 
}
}

    ?>
    </table>
    
answered by 13.04.2018 в 21:14