· 8 years ago · Nov 27, 2017, 08:22 AM
1<?php
2
3class 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 TIMESTAMP
42 )";
43 $this->queryList($estudiante);
44 }
45}
46
47/*
48 * MODELO
49 */
50
51class Estudiante
52{
53 private $id;
54 private $nombre;
55 private $apellidos;
56 private $email;
57 private $telefono;
58 private $tutor;
59 private $grupo;
60 private $fechaInicio;
61
62 public function __construct($id, $nombre, $apellidos, $email, $telefono, $tutor, $grupo, $fechaInicio)
63 {
64 $this->id = $id;
65 $this->nombre = $nombre;
66 $this->apellidos = $apellidos;
67 $this->email = $email;
68 $this->telefono = $telefono;
69 $this->tutor = $tutor;
70 $this->grupo = $grupo;
71 $this->fechaInicio = $fechaInicio;
72 }
73
74 public function guardar()
75 {
76 global $DBCon;
77 $query = "INSERT INTO Estudiante VALUES (:id, :nombre, :apellidos, :email, :telefono, :tutor, :grupo, :fechaInicio)
78 ON DUPLICATE KEY UPDATE nombre = :nombre, apellidos = :apellidos, email = :email, telefono = :telefono, grupo = :grupo";
79 $args = array(":id" => $this->id, ":nombre" => $this->nombre, ":apellidos" => $this->apellidos, ":email" => $this->email,
80 ":telefono" => $this->telefono, ":tutor" => $this->tutor, ":grupo" => $this->grupo, ":fechaInicio" => $this->fechaInicio);
81 $stm = $DBCon->queryList($query, $args);
82 return $stm;
83 }
84}
85
86/*
87 * VISTA
88 */
89
90class EstudiantesView
91{
92 private $estudiantes;
93
94 public function __construct()
95 {
96 global $DBCon;
97 $this->estudiantes = array();
98 foreach ($DBCon->queryList('SELECT * FROM Estudiante ORDER BY apellidos') as $row) {
99 $this->estudiantes[] = new Estudiante($row['id'], $row['nombre'], $row['apellidos'], $row['email'], $row['telefono'], $row['tutor'], $row['grupo'], $row['fechaInicio']);
100 }
101 }
102
103 public function display()
104 {
105 echo "<table>";
106 echo '<tr id="titulos">
107 <td>Nombre</td>
108 <td>Apellidos</td>
109 <td>Email</td>
110 <td>Telefono</td>
111 <td>Tutor</td>
112 <td>Grupo</td>
113 <td>FechaDeInicio</td>
114 <td>IdEstudiante</td>
115 </tr>';
116 foreach ($this->estudiantes as $estudiante) {
117 echo '<tr class="partecicas">
118 <td>' . $estudiante->nombre . '</td>
119 <td>' . $estudiante->apellidos . '</td>
120 <td>' . $estudiante->email . '</td>
121 <td>' . $estudiante->telefono . '</td>
122 <td>' . $estudiante->tutor . '</td>
123 <td>' . $estudiante->grupo . '</td>
124 <td>' . $estudiante->fechaInicio . '</td>
125 <td>' . $estudiante->id . '</td>
126 </tr>';
127 }
128 echo '</table>';
129 }
130}
131
132$DBCon = new DBConnection();
133$DBCon->createTables();
134?>
135<!doctype html>
136<html>
137<head>
138 <meta charset="UTF-8">
139 <title>Ejercicio1</title>
140 <style>
141 #botonazo {
142 margin-left: 200px;
143 }
144
145 #titulos {
146 background-color: rgb(255, 60, 212);
147 }
148
149 .partecicas {
150 background-color: rgb(227, 209, 138);
151 padding: 2%;
152 }
153
154 .bloque {
155 background-color: rgb(255, 247, 139);
156 margin-bottom: 100px;
157 }
158
159
160 </style>
161
162</head>
163<body>
164
165<div class="bloque">
166 <form action="" method="post">
167 <table>
168 <tr>
169 <td>Nombre</td>
170 <td><input type="text" name="nombre" value=""></td>
171 </tr>
172 <tr>
173 <td>Apellidos</td>
174 <td><input type="text" name="apellidos" value=""></td>
175 </tr>
176 <tr>
177 <td>Email</td>
178 <td><input type="text" name="email" value=""></td>
179 </tr>
180 <tr>
181 <td>Telefono</td>
182 <td><input type="text" name="telefono" value=""></td>
183 </tr>
184 <tr>
185 <td>Tutor</td>
186 <td><input type="text" name="tutor" value=""></td>
187 </tr>
188 <tr>
189 <td>Grupo</td>
190 <td><input type="text" name="grupo" value=""></td>
191 </tr>
192 <tr>
193 <td>FechaDeInicio</td>
194 <td><input type="text" name="fecha" value=""></td>
195 </tr>
196 <tr>
197 <td>IdEstudiante</td>
198 <td><input type="text" name="id" value=""></td>
199 </tr>
200 </table>
201 <br>
202 <input id="botonazo" type="submit" value="Enviar">
203 </form>
204</div>