-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbd.php
More file actions
114 lines (114 loc) · 3.48 KB
/
Copy pathbd.php
File metadata and controls
114 lines (114 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
function leer_config($nombre, $esquema){
$config = new DOMDocument();
$config->load($nombre);
$res = $config->schemaValidate($esquema);
if ($res===FALSE){
throw new InvalidArgumentException("Revise fichero de configuración");
}
$datos = simplexml_load_file($nombre);
$ip = $datos->xpath("//ip");
$nombre = $datos->xpath("//nombre");
$usu = $datos->xpath("//usuario");
$clave = $datos->xpath("//clave");
$cad = sprintf("mysql:dbname=%s;host=%s", $nombre[0], $ip[0]);
$resul = [];
$resul[] = $cad;
$resul[] = $usu[0];
$resul[] = $clave[0];
return $resul;
}
function comprobar_usuario($nombre, $clave){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$ins = "select codRes, correo from restaurantes where correo = '$nombre'
and clave = '$clave'";
$resul = $bd->query($ins);
if($resul->rowCount() === 1){
return $resul->fetch();
}else{
return FALSE;
}
}
function cargar_categorias(){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$ins = "select codCat, nombre from categorias";
$resul = $bd->query($ins);
if (!$resul) {
return FALSE;
}
if ($resul->rowCount() === 0) {
return FALSE;
}
//si hay 1 o más
return $resul;
}
function cargar_categoria($codCat){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$ins = "select nombre, descripcion from categorias where codcat = $codCat";
$resul = $bd->query($ins);
if (!$resul) {
return FALSE;
}
if ($resul->rowCount() === 0) {
return FALSE;
}
//si hay 1 o más
return $resul->fetch();
}
function cargar_productos_categoria($codCat){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$sql = "select * from productos where categoria = $codCat";
$resul = $bd->query($sql);
if (!$resul) {
return FALSE;
}
if ($resul->rowCount() === 0) {
return FALSE;
}
//si hay 1 o más
return $resul;
}
// recibe un array de códigos de productos
// devuelve un cursor con los datos de esos productos
function cargar_productos($codigosProductos){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$texto_in = implode(",", $codigosProductos);
$ins = "select * from productos where codProd in($texto_in)";
$resul = $bd->query($ins);
if (!$resul) {
return FALSE;
}
return $resul;
}
function insertar_pedido($carrito, $codRes){
$res = leer_config(dirname(__FILE__)."/configuracion.xml", dirname(__FILE__)."/configuracion.xsd");
$bd = new PDO($res[0], $res[1], $res[2]);
$bd->beginTransaction();
$hora = date("Y-m-d H:i:s", time());
// insertar el pedido
$sql = "insert into pedidos(fecha, enviado, restaurante)
values('$hora',0, $codRes)";
$resul = $bd->query($sql);
if (!$resul) {
return FALSE;
}
// coger el id del nuevo pedido para las filas detalle
$pedido = $bd->lastInsertId();
// insertar las filas en pedidoproductos
foreach($carrito as $codProd=>$unidades){
$sql = "insert into pedidosproductos(Pedido, Producto, Unidades)
values( $pedido, $codProd, $unidades)";
$resul = $bd->query($sql);
if (!$resul) {
$bd->rollback();
return FALSE;
}
}
$bd->commit();
return $pedido;
}