martes, 21 de enero de 2014

DEBER 27

FRANCISCO MUNOZ
CATEDRATICO Ing. Juan Espinoza.

RESULTADO FINAL





PRIMERO NOS CONECTAMOS A LA BASE DE DATOS
<?php
$conexion=mysql_connect("localhost","CONEXIONBDD","admin");
if(!$conexion){
die('NO SE PUDO ESTABLECER LA CONEXION'.mysql_error());
}
else{
echo("CONEXION SATISFACTORIA");
}

AHORA SELECCIONAMOS LA BASE DE DATOS
mysql_select_db("BDDAGENDA", $conexion);
?>

DENTRO DE UN HTML REALIZAMOS LA CODIFICACION PHP
<html>
<head>
<title>DATOS</title>
</head>
<body bgcolor="celeste">
<section id="top">
<h1><CENTER>LISTAR DATOS</CENTER></h1>
</section>
<?php
$var="";
$var1="";
$var2="";
$var3="";
$var4="";
$var5="";

EXTRAEMOS LOS DATOS CON LA SENTENCIA IF Y ISSET
if(isset($_POST["btn1"])){
$btn=$_POST["btn1"];
$bus=$_POST["txtbus"];
$bus1=$_POST["txtapel"];
$bus2=$_POST["txtedad"];
$bus3=$_POST["txtdire"];

AQUI PREGUNTAMOS SI PRESIONA NOMBRE QUE REALICE LA SIGUIENTE CONSULTA



if($btn=="Nombre"){
$sql="select * from agenda where Nombre='$bus'";
$cs=mysql_query($sql,$conexion);
?>
<table width="1166" border="1" id="tab">
   <tr>
     <td width="19">Id </td>
     <td width="61">Nombre</td>
     <td width="157">Apellido</td>
     <td width="221">Edad</td>
     <td width="176">Direccion</td>
     <td width="107">Telefono</td>
   </tr>
   <?php
while($resul=mysql_fetch_array($cs)){
echo '<tr>';
echo '<td width="19">'.$resul['idagenda'].'</td>';
echo '<td width="61">'.$resul['Nombre'].'</td>';
echo '<td width="157">'.$resul['Apellido'].'</td>';
echo '<td width="221">'.$resul['Edad'].'</td>';
echo '<td width="176">'.$resul['Direccion'].'</td>';
echo '<td width="107">'.$resul['Telefono'].'</td>';
echo '</tr>';
}
}

AQUI CONSULTA PARA EL NOMBRE
if($btn=="Apellido"){

$sql="select * from agenda where Apellido='$bus1'";
$cs1=mysql_query($sql,$conexion);
?>
<table width="1166" border="1" id="tab">
   <tr>
     <td width="19">Id </td>
     <td width="61">Nombre</td>
     <td width="157">Apellido</td>
     <td width="221">Edad</td>
     <td width="176">Direccion</td>
     <td width="107">Telefono</td>
   </tr>
   <?php
while($resul=mysql_fetch_array($cs1)){
echo '<tr>';
echo '<td width="19">'.$resul['idagenda'].'</td>';
echo '<td width="61">'.$resul['Nombre'].'</td>';
echo '<td width="157">'.$resul['Apellido'].'</td>';
echo '<td width="221">'.$resul['Edad'].'</td>';
echo '<td width="176">'.$resul['Direccion'].'</td>';
echo '<td width="107">'.$resul['Telefono'].'</td>';
echo '</tr>';
}
}

AQUI CONSUTA PARA LA EDAD
if($btn=="Edad"){

$sql="select * from agenda where Edad='$bus2'";
$cs2=mysql_query($sql,$conexion);
?>
<table width="1166" border="1" id="tab">
   <tr>
     <td width="19">Id </td>
     <td width="61">Nombre</td>
     <td width="157">Apellido</td>
     <td width="221">Edad</td>
     <td width="176">Direccion</td>
     <td width="107">Telefono</td>
   </tr>
   <?php
while($resul=mysql_fetch_array($cs2)){
echo '<tr>';
echo '<td width="19">'.$resul['idagenda'].'</td>';
echo '<td width="61">'.$resul['Nombre'].'</td>';
echo '<td width="157">'.$resul['Apellido'].'</td>';
echo '<td width="221">'.$resul['Edad'].'</td>';
echo '<td width="176">'.$resul['Direccion'].'</td>';
echo '<td width="107">'.$resul['Telefono'].'</td>';
echo '</tr>';
}
}

AQUI CONSULTA PARA QUE MUESTRE DIRECCIONES IGUALES
if($btn=="Direccion"){

$sql="select * from agenda where Direccion='$bus3'";
$cs3=mysql_query($sql,$conexion);
?>
<table width="1166" border="1" id="tab">
   <tr>
     <td width="19">Id </td>
     <td width="61">Nombre</td>
     <td width="157">Apellido</td>
     <td width="221">Edad</td>
     <td width="176">Direccion</td>
     <td width="107">Telefono</td>
   </tr>
   <?php
while($resul=mysql_fetch_array($cs3)){
echo '<tr>';
echo '<td width="19">'.$resul['idagenda'].'</td>';
echo '<td width="61">'.$resul['Nombre'].'</td>';
echo '<td width="157">'.$resul['Apellido'].'</td>';
echo '<td width="221">'.$resul['Edad'].'</td>';
echo '<td width="176">'.$resul['Direccion'].'</td>';
echo '<td width="107">'.$resul['Telefono'].'</td>';
echo '</tr>';
}

}
?>

</table>
AHORA AGREGAMOS LAS CONSULTAS PARA LOS BOTONES 

NUEVO
<?php
if($btn=="Nuevo"){
$sql="select count(idagenda),Max(idagenda) from agenda";
$cs=mysql_query($sql,$conexion);
while($resul=mysql_fetch_array($cs)){
$count=$resul[0];
$max=$resul[1];
}
if($count==0){
$var="A0001";
}
else{
$var='A'.substr((substr($max,1)+10001),1);
}
}

AGREGAR
if($btn=="Agregar"){
$cod=$_POST["txtcod"];
$nom=$_POST["txtnom"];
$ape=$_POST["txtape"];
$eda=$_POST["txteda"];
$tel=$_POST["txttel"];
$dir=$_POST["cbosex"];
$sql="insert into agenda values ('$cod','$nom','$ape','$eda','$tel','$dir')";
$conexion=mysql_query($sql,$conexion);
echo "<script> alert('Se Inserto; correctamente');</script>";
}
ELIMINAR
if($btn=="Eliminar"){
$cod=$_POST["txtcod"];
$sql="delete from agenda where idagenda='$cod'";
$cs=mysql_query($sql,$conexion);
echo "<script> alert('Se elimnino correctamente');</script>";
}
}

?>
AQUI CON CODIGO HTML REALIZAMOS LOS CAMPOS PARA INGRESAR DATOS:
<form name="fe" action="" method="post">
<center>
CODIGO
<table border="2">
<tr>
<td>Codigo</td>
<td><input type="text" name="txtcod" value="<?php echo $var?>" /></td>
</tr>
NOMBRE
<tr>
<td>Nombre</td>
<td><input type="text" name="txtnom"  value="<?php echo $var1?>"/></td>
</tr>
APELLIDO
<tr>
<td>Apellido</td>
<td><input type="text" name="txtape"  value="<?php echo $var2?>"/></td>
</tr>
EDAD
<tr>
<td>Edad</td>
<td><input type="text" name="txteda"  value="<?php echo $var3?>"/></td>
</tr>
TELEFONO
<tr>
<td>Telefono;</td>
<td><input type="text" name="txttel"  value="<?php echo $var4?>"/></td>
</tr>
DIRECCION
<tr>
<td>Direccion</td>
<td><input type="text" name="cbosex"  value="<?php echo $var5?>"/></td>
</tr>

Y AQUI PONEMOS LOS BOTONES:
<tr align="center">
<td colspan="2">
NUEVO
<input type="submit" name="btn1" value="Nuevo"/>
LISTAR
<input type="submit" name="btn1" value="Listar"/>
ELIMINAR
<input type="submit" name="btn1"value="Eliminar"/>
AGREGAR
<input type="submit" name="btn1"value="Agregar"/></td></tr>
</table >

AQUI CREAMOS LOS CAMPOS Y BOTONES PARA FILTRAR CON CODIGO HTML
<table color="green" border="2">
<tr>
POR NOMBRE:
<td>Filtrar</td>
<td><input type="text" name="txtbus"/></td>
<td><input type="submit" name="btn1"  value="Nombre"  /></td>
POR APELLIDO:
<td>Filtrar</td>
<td><input type="text" name="txtapel"/></td>
<td><input type="submit" name="btn1"  value="Apellido"  /></td>
POR EDAD:
<td>Filtrar</td>
<td><input type="text" name="txtedad"/></td>
<td><input type="submit" name="btn1"  value="Edad"  /></td>
POR DIRECCION:
<td>Filtrar</td>
<td><input type="text" name="txtdire"/></td>
<td><input type="submit" name="btn1"  value="Direccion"  /></td>

</center>
<br />
<hr>
</form>
<br />


AQUI PROGRAMAMOS PARA QUE NO MUESTRE EN UNA TABLA LA LISTA CON TODOS SUS CAMPOS OSEA DESPUES DE PRESIONAR LA TECLA LISTAR.




<?php
if(isset($_POST["btn1"])){
$btn=$_POST["btn1"];

if($btn=="Listar"){
$sql="select * from agenda";
$cs=mysql_query($sql,$conexion);
echo"<center>
<table border='3'>
<tr>
<td>Codigo</td>
<td>Nombre</td>
<td>Apellido</td>
<td>Edad</td>
<td>Telefono</td>
<td>Direccion</td>
</tr>";
while($resul=mysql_fetch_array($cs)){
$var=$resul[0];
$var1=$resul[1];
$var2=$resul[2];
$var3=$resul[3];
$var4=$resul[4];
$var5=$resul[5];
echo "<tr>

<td>$var</td>
<td>$var1</td>
<td>$var2</td>
<td>$var3</td>
<td>$var4</td>
<td>$var5</td>

</tr>";
}
ECHO "<H1><CENTER>LISTA</CENTER></H1>";
echo "</table>

</center>";

}
}
?>
</body>
</html>

No hay comentarios:

Publicar un comentario