Advertisement
Guest User

Untitled

a guest
Nov 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. <?php
  2.  
  3. class DBConnection
  4. {
  5. const USERNAME = "root";
  6. const PASSWORD = "";
  7. const HOST = "localhost";
  8. const DB = "instituto";
  9.  
  10. private function getConnection()
  11. {
  12. $username = self::USERNAME;
  13. $password = self::PASSWORD;
  14. $host = self::HOST;
  15. $db = self::DB;
  16. $connection = new PDO("mysql:dbname=$db;host=$host", $username, $password);
  17. return $connection;
  18. }
  19.  
  20. public function queryList($sql, $args = null)
  21. {
  22. $connection = $this->getConnection();
  23. $stmt = $connection->prepare($sql);
  24. if (!empty($args))
  25. $stmt->execute($args);
  26. else
  27. $stmt->execute();
  28. return $stmt;
  29. }
  30.  
  31. public function createTables()
  32. {
  33. $estudiante = "CREATE TABLE IF NOT EXISTS Estudiante (
  34. id INT(11) NOT NULL AUTO_INCREMENT,
  35. nombre VARCHAR(255),
  36. apellidos VARCHAR(255),
  37. email VARCHAR(255),
  38. telefono INT(9),
  39. tutor VARCHAR(255),
  40. grupo VARCHAR(255),
  41. fechaInicio VARCHAR(255)
  42. )";
  43. $this->queryList($estudiante);
  44.  
  45. $comentario = "CREATE TABLE IF NOT EXISTS Comentario (
  46. id INT(11) NOT NULL AUTO_INCREMENT,
  47. idEstudiante INT(11),
  48. mensaje VARCHAR(255)
  49. )";
  50. $this->queryList($comentario);
  51. }
  52. }
  53.  
  54. /*
  55. * MODELO
  56. */
  57.  
  58. class Estudiante
  59. {
  60. private $id;
  61. private $nombre;
  62. private $apellidos;
  63. private $email;
  64. private $telefono;
  65. private $tutor;
  66. private $grupo;
  67. private $fechaInicio;
  68.  
  69. public function __construct($id, $nombre, $apellidos, $email, $telefono, $tutor, $grupo, $fechaInicio)
  70. {
  71. $this->id = $id;
  72. $this->nombre = $nombre;
  73. $this->apellidos = $apellidos;
  74. $this->email = $email;
  75. $this->telefono = $telefono;
  76. $this->tutor = $tutor;
  77. $this->grupo = $grupo;
  78. $this->fechaInicio = $fechaInicio;
  79. }
  80.  
  81. public function guardar()
  82. {
  83. global $DBCon;
  84. $query = "INSERT INTO Estudiante VALUES (:id, :nombre, :apellidos, :email, :telefono, :tutor, :grupo, :fechaInicio)
  85. ON DUPLICATE KEY UPDATE nombre = :nombre, apellidos = :apellidos, email = :email, telefono = :telefono, grupo = :grupo";
  86. $args = array(":id" => $this->id, ":nombre" => $this->nombre, ":apellidos" => $this->apellidos, ":email" => $this->email,
  87. ":telefono" => $this->telefono, ":tutor" => $this->tutor, ":grupo" => $this->grupo, ":fechaInicio" => $this->fechaInicio);
  88. $stm = $DBCon->queryList($query, $args);
  89. return $stm;
  90. }
  91. }
  92.  
  93. class Comentario
  94. {
  95. private $id;
  96. private $idEstudiante;
  97. private $mensaje;
  98.  
  99. public function __construct($id = null, $idEstudiante, $mensaje)
  100. {
  101. $this->id = $id;
  102. $this->idEstudiante = $idEstudiante;
  103. $this->mensaje = $mensaje;
  104. }
  105.  
  106. public function guardar()
  107. {
  108. global $DBCon;
  109. $query = "INSERT INTO Comentario VALUES (:id, :idEstudiante, :mensaje)
  110. ON DUPLICATE KEY UPDATE idEstudiante = :idEstudiante, mensaje = :mensaje";
  111. $args = array(":id" => $this->id, ":idEstudiante" => $this->idEstudiante, ":mensaje" => $this->mensaje);
  112. $stm = $DBCon->queryList($query, $args);
  113. return $stm;
  114. }
  115. }
  116.  
  117. /*
  118. * VISTA
  119. */
  120.  
  121. class EstudiantesView
  122. {
  123. private $estudiantes;
  124.  
  125. public function __construct()
  126. {
  127. global $DBCon;
  128. $this->estudiantes = array();
  129. foreach ($DBCon->queryList('SELECT * FROM Estudiante ORDER BY apellidos')->fetchAll() as $row) {
  130. $this->estudiantes[] = new Estudiante($row['id'], $row['nombre'], $row['apellidos'], $row['email'], $row['telefono'], $row['tutor'], $row['grupo'], $row['fechaInicio']);
  131. }
  132. }
  133.  
  134. public function display()
  135. {
  136. echo "<table>";
  137. echo '<tr id="estudiantes">
  138. <td>Nombre</td>
  139. <td>Apellidos</td>
  140. <td>Email</td>
  141. <td>Telefono</td>
  142. <td>Tutor</td>
  143. <td>Grupo</td>
  144. <td>FechaDeInicio</td>
  145. <td>IdEstudiante</td>
  146. </tr>';
  147. foreach ($this->estudiantes as $estudiante) {
  148. echo '<tr class="estudiante">
  149. <td>' . $estudiante->nombre . '</td>
  150. <td>' . $estudiante->apellidos . '</td>
  151. <td>' . $estudiante->email . '</td>
  152. <td>' . $estudiante->telefono . '</td>
  153. <td>' . $estudiante->tutor . '</td>
  154. <td>' . $estudiante->grupo . '</td>
  155. <td>' . $estudiante->fechaInicio . '</td>
  156. <td>' . $estudiante->id . '</td>
  157. </tr>';
  158. }
  159. echo '</table>';
  160. }
  161. }
  162.  
  163. class ComentariosView
  164. {
  165. private $comentarios;
  166.  
  167. public function __construct()
  168. {
  169. global $DBCon;
  170. $this->comentarios = array();
  171. foreach ($DBCon->queryList('SELECT * FROM Comentario ORDER BY id')->fetchAll() as $row) {
  172. $this->comentarios[] = new Comentario($row['id'], $row['idEstudiante'], $row['mensaje']);
  173. }
  174. }
  175.  
  176. public function display()
  177. {
  178. echo "<table>";
  179. echo '<tr id="comentarios">
  180. <td>id</td>
  181. <td>idEstudiante</td>
  182. <td>Mensaje</td>
  183. </tr>';
  184. foreach ($this->comentarios as $comentario) {
  185. echo '<tr class="comentario">
  186. <td>' . $comentario->id . '</td>
  187. <td>' . $comentario->idEstudiante . '</td>
  188. <td>' . $comentario->mensaje . '</td>
  189. </tr>';
  190. }
  191. echo '</table>';
  192. }
  193. }
  194.  
  195. $DBCon = new DBConnection();
  196. $DBCon->createTables();
  197.  
  198.  
  199. if (!empty($_POST['idest'])) {
  200. $estudiante = new Estudiante($_POST['idest'], $_POST['nombre'], $_POST['apellidos'], $_POST['email'], $_POST['telefono'], $_POST['tutor'], $_POST['grupo'], $_POST['fecha']);
  201. $estudiante->guardar();
  202. }
  203.  
  204. if(!empty($_POST['idcom'])) {
  205. $comentario = new Comentario($_POST['idcom'], $_POST['idestudiante'], $_POST['mensaje']);
  206. $comentario->guardar();
  207. }
  208.  
  209. $estudiantes = new EstudiantesView();
  210. $comentarios = new ComentariosView();
  211. ?>
  212. <!doctype html>
  213. <html>
  214. <head>
  215. <meta charset="UTF-8">
  216. <title>Ejercicio1</title>
  217. <style>
  218. #botonazo {
  219. margin-left: 200px;
  220. }
  221.  
  222. #estudiantes,
  223. #comentarios {
  224. background-color: rgb(255, 60, 212);
  225. }
  226.  
  227. .estudiante,
  228. .comentario {
  229. background-color: rgb(227, 209, 138);
  230. padding: 2%;
  231. }
  232.  
  233. .bloque {
  234. background-color: rgb(255, 247, 139);
  235. margin-bottom: 100px;
  236. }
  237.  
  238.  
  239. </style>
  240.  
  241. </head>
  242. <body>
  243.  
  244. <div class="bloque">
  245. <h1>Formulario de Estudiantes</h1>
  246. <form action="" method="post">
  247. <table>
  248. <tr>
  249. <td>Nombre</td>
  250. <td><input type="text" name="nombre" value=""></td>
  251. </tr>
  252. <tr>
  253. <td>Apellidos</td>
  254. <td><input type="text" name="apellidos" value=""></td>
  255. </tr>
  256. <tr>
  257. <td>Email</td>
  258. <td><input type="text" name="email" value=""></td>
  259. </tr>
  260. <tr>
  261. <td>Telefono</td>
  262. <td><input type="text" name="telefono" value=""></td>
  263. </tr>
  264. <tr>
  265. <td>Tutor</td>
  266. <td><input type="text" name="tutor" value=""></td>
  267. </tr>
  268. <tr>
  269. <td>Grupo</td>
  270. <td><input type="text" name="grupo" value=""></td>
  271. </tr>
  272. <tr>
  273. <td>FechaDeInicio</td>
  274. <td><input type="text" name="fecha" value=""></td>
  275. </tr>
  276. <tr>
  277. <td>IdEstudiante</td>
  278. <td><input type="text" name="idest" value=""></td>
  279. </tr>
  280. </table>
  281. <br>
  282. <input id="botonazo" type="submit" value="Enviar">
  283. </form>
  284.  
  285. <?php $estudiantes->display(); ?>
  286.  
  287. <h1>Formulario de Comentarios</h1>
  288. <form action="" method="post">
  289. <table>
  290. <tr>
  291. <td>ID Estudiante</td>
  292. <td><input type="text" name="idestudiante" value=""></td>
  293. </tr>
  294. <tr>
  295. <td>Mensaje</td>
  296. <td><input type="text" name="mensaje" value=""></td>
  297. </tr>
  298. <tr>
  299. <td>ID Comentario</td>
  300. <td><input type="text" name="idcom" value=""></td>
  301. </tr>
  302. </table>
  303. <br>
  304. <input id="botonazo" type="submit" value="Enviar">
  305. </form>
  306.  
  307. <?php $comentarios->display(); ?>
  308. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement