Noviembre 8 de 2017

 Ejercicios de los manuales 33 y 34


Se realizó una evaluación
Entrega de notas

EJERCICIOS:

1. Ejercicio con imágenes:

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<?php
$mensaje = isset($_GET['mensaje'])?$_GET['mensaje']:"";
if ($mensaje != ""){
?>
<script type="text/javascript">alert("<?=$mensaje;?>");</script>
<?php }?>
<body>
<form id="form1" name="form1" method="post" action="guardar.php">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td width="16%">&nbsp;</td>
      <td width="74%">&nbsp;</td>
      <td width="10%">&nbsp;</td>
    </tr>
    <tr>
      <td>Nombre : </td>
      <td><input type="text" name="nombre" id="nombre" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>Apellido : </td>
      <td><input type="text" name="apellido" id="apellido" /></td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="button" id="button" value="Guardar" /></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><hr /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><a href="ver_pantalla.php" target="_blank">Ver Resultados en Pantalla</a></td>
    <td>&nbsp;</td>
    <td><a href="ver_pdf.php" target="_blank">Ver Resultados en Pdf</a></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3"><hr /></td>
  </tr>
</table>
</body>
</html>


guardar.php

<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = '';
$base_datos = 'ejemplo_001';

$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
$nombre  = isset($_POST['nombre'])?$_POST['nombre']:"";
$apellido  = isset($_POST['apellido'])?$_POST['apellido']:"";
//-- ------------------------------------------------------------------------------------------------
$mensaje = 'Registro Incluido con Exito.';
if (strlen($nombre)==0) $mensaje = "El nombre no puede ser vacio. Intentelo de nuevo.";
elseif (strlen($apellido)==0) $mensaje = "El apellido no puede ser vacio. Intentelo de nuevo.";
else{
//-- ------------------------------------------------------------------------------------------------
$sql  = "INSERT INTO ".$base_datos.".usuarios (id, nombre, apellido) "."VALUES(NULL, '"
.$nombre."', '".$apellido."')";
echo $sql;

$sql  = mysqli_query($conexion,$sql);
//mysqli_free_result($sql);
//-- ------------------------------------------------------------------------------------------------
}
header("Location: index.php?mensaje=".$mensaje);
exit();
?>

conexion_db.php

<?php  $servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = '';
$base_datos = 'ejemplo_001';

?>


ver_pantalla.php

<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = '';
$base_datos = 'ejemplo_001';
$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
// -- Cambia dependiendo del ejercicio
$sql= "SELECT id, apellido, nombre, activo "
  ."FROM ".$base_datos.".usuarios  "
  ."ORDER by apellido, nombre";   
$sql = mysqli_query($conexion,$sql);  // No cambia
//-- ------------------------------------------------------------------------------------------------
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body><table width="100%" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle"><strong>I.d.</strong></td>
    <td align="center" valign="middle"><strong>Apellido</strong></td>
    <td align="center" valign="middle"><strong>Nombre</strong></td>
    <td align="center" valign="middle"><strong>Activo</strong></td>
  </tr>
<?php
while($rs = mysqli_fetch_array($sql)) { 
$id = $rs[0];
$apellido = $rs[1];
$nombre  = $rs[2];
$activo  = $rs[3];
?>   
  <tr>
    <td align="center" valign="middle"><?=$id;?></td>
    <td align="center" valign="middle"><?=$apellido;?></td>
    <td align="center" valign="middle"><?=$nombre;?></td>
    <td align="center" valign="middle"><?=$activo;?></td>
  </tr>
<?php }?>  
  </table>
</body>

</html>


ver_pdf.php

<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = '';
$base_datos = 'ejemplo_001';
$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
require_once("librerias/fpdf.php"); //-- Llama la Libreria
class PDF extends FPDF{} //-- Define una clase de la Libreria
$texto = isset($_POST['texto'])?$_POST['texto']:""; //-- Captura la variable del index.php
$pdf = new PDF(); //-- Crea una variable para trabajar con la Libreria
$pdf->AddPage(); //-- Crea una nueva pagina en el archivo Pdf
$pdf->SetFont('Arial','',8); //-- Define el tipo de fuente y el tamaño
//-- ------------------------------------------------------------------------------------------------
$sql= "SELECT id, apellido, nombre, activo "
  ."FROM ".$base_datos.".usuarios  "
  ."ORDER by apellido, nombre";   
$sql = mysqli_query($conexion,$sql);
//-- ------------------------------------------------------------------------------------------------
while($rs = mysqli_fetch_array($sql)) { 
$id = $rs[0];
$apellido = $rs[1];
$nombre  = $rs[2];
$activo  = $rs[3];
$linea = "[ " . $id . " ]   -   [ " . $apellido . " ]   -   [ " . $nombre . " ]   -   [ " . $activo . " ]";
            
$imagen = 'img/'.$id.'.jpg';
$Yy = 8.8; //Posicion de Y, donde se ubicará la imagen.
    if (file_exists($imagen) == false){
        // No encontro la imagen
    }
    else{
        //$pdf->Image($imagen,11,$Yy,10,10,'jpeg','');//($file,$x,$y,$w=0,$h=0,$type='',$link='')
        $pdf->Image($imagen, 5, $pdf->GetY() + 5, 33.78);


        $Yy = $Yy + 100;
        $pdf->Ln();
    }


$pdf->MultiCell(0, 3, utf8_decode($linea) ); //-- Imprime en el Pdf la Variable que viene del index.php
$pdf->Ln();
}
//-- utf8_decode: Funcion PHP para convertir caracteres especiales, que se vean bien los acentos y otras letras del alfabeto latino.
$pdf->Output();  //-- Cierra la variable del Pdf y visualiza en pantalla el resultado en un Pdf.

?>


crear-base-datos.sql


-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Servidor: localhost
-- Tiempo de generación: 07-05-2017 a las 01:52:17
-- Versión del servidor: 10.1.21-MariaDB
-- Versión de PHP: 7.1.1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Base de datos: `ejemplo_001`
--
CREATE DATABASE IF NOT EXISTS `ejemplo_001` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `ejemplo_001`;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `usuarios`
--

CREATE TABLE `usuarios` (
  `id` int(11) NOT NULL,
  `nombre` varchar(30) NOT NULL,
  `apellido` varchar(30) NOT NULL,
  `activo` varchar(1) NOT NULL DEFAULT 'S'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Volcado de datos para la tabla `usuarios`
--

INSERT INTO `usuarios` (`id`, `nombre`, `apellido`, `activo`) VALUES
(1, 'jaime', 'Lopez', 'S'),
(2, 'andres', 'jimenez', 'S'),
(3, 'toña', 'colmenares', 'S'),
(4, 'lucia', 'castro', 'S'),
(5, 'tomas', 'albarracin', 'S');

--
-- Índices para tablas volcadas
--

--
-- Indices de la tabla `usuarios`
--
ALTER TABLE `usuarios`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT de las tablas volcadas
--

--
-- AUTO_INCREMENT de la tabla `usuarios`
--
ALTER TABLE `usuarios`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



2. Ejercicio sin imágenes:

TODO QUEDA IGUAL SOLO CAMBIA EL VER_PDF.PHP

ver_pdf.php


<?php 
$servidor = 'localhost';
$usr_sistema = 'root';
$pass_sistema = '';
$base_datos = 'ejemplo_001';
$conexion=mysqli_connect($servidor, $usr_sistema, $pass_sistema);
if (!$conexion){
header ("Location: index.php?mensaje=error de conexion.");
exit();    
}
//-- ------------------------------------------------------------------------------------------------
require_once("librerias/fpdf.php"); //-- Llama la Libreria
class PDF extends FPDF{} //-- Define una clase de la Libreria
$texto = isset($_POST['texto'])?$_POST['texto']:""; //-- Captura la variable del index.php
$pdf = new PDF(); //-- Crea una variable para trabajar con la Libreria
$pdf->AddPage(); //-- Crea una nueva pagina en el archivo Pdf
$pdf->SetFont('Arial','',8); //-- Define el tipo de fuente y el tamaño
//-- ------------------------------------------------------------------------------------------------
$sql= "SELECT id, apellido, nombre, activo "
  ."FROM ".$base_datos.".usuarios  "
  ."ORDER by apellido, nombre";   
$sql = mysqli_query($conexion,$sql);
//-- ------------------------------------------------------------------------------------------------
while($rs = mysqli_fetch_array($sql)) { 
$id = $rs[0];
$apellido = $rs[1];
$nombre  = $rs[2];
$activo  = $rs[3];
$linea = "[ " . $id . " ]   -   [ " . $apellido . " ]   -   [ " . $nombre . " ]   -   [ " . $activo . " ]";
$pdf->MultiCell(0, 3, utf8_decode($linea) ); //-- Imprime en el Pdf la Variable que viene del index.php
$pdf->Ln();
}
//-- utf8_decode: Funcion PHP para convertir caracteres especiales, que se vean bien los acentos y otras letras del alfabeto latino.
$pdf->Output();  //-- Cierra la variable del Pdf y visualiza en pantalla el resultado en un Pdf.
?>

Realizado por: Valeria Rojas Orduz 10-A  
 Colegio Integrado Nuestra Señora del Divino Amor

Comentarios

Entradas más populares de este blog

Septiembre 27 de 2016

Febrero 23 de 2016