-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
70 lines (63 loc) · 2.25 KB
/
functions.php
File metadata and controls
70 lines (63 loc) · 2.25 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
<?php
//establece conexion con la base de datos
function conexion($bd_config){
try {
$conexion = new PDO('mysql:host=localhost;dbname=' . $bd_config['basededatos'], $bd_config['usuario'], $bd_config['pass']);
return $conexion;
} catch (PDOException $e) {
return false;
}
}
//limpia datos de caracteres especiales
function limpiar_datos($datos){
$datos = trim($datos);
$datos = stripslashes($datos);
$datos = htmlspecialchars($datos);
return $datos;
}
//obtiene numero de pagina a travez de la variable p
function pagina_actual(){
return isset($_GET['p']) ? (int)$_GET['p'] : 1;
}
//obtiene posts disponibles para la pagina actual
function obtener_posts($post_por_pagina, $conexion){
$inicio = (pagina_actual() > 1 ? pagina_actual() * $post_por_pagina - $post_por_pagina : 0); //obtiene el primer post de la pagina
$sentencia = $conexion -> prepare("SELECT SQL_CALC_FOUND_ROWS * FROM articulos LIMIT $inicio, $post_por_pagina");
$sentencia -> execute();
return $sentencia -> fetchAll();
}
//obtiene numero de páginas para la paginación
function numero_paginas($post_por_pagina, $conexion){
$total_post = $conexion -> prepare('SELECT FOUND_ROWS() as total');
$total_post -> execute();
$total_post = $total_post -> fetch()['total'];
$numero_paginas = ceil($total_post / $post_por_pagina);
return $numero_paginas;
}
//retorna solo valores enteros
function id_articulo($id){
return (int)limpiar_datos($id);
}
//obtiene un post a partir de un id
function obtener_post_por_id($conexion, $id){
$resultado = $conexion -> query("SELECT * FROM articulos WHERE id = $id LIMIT 1");
$resultado = $resultado->fetchAll();
return ($resultado) ? $resultado : false;
}
//transforma fecha de tipo TIMESTAMP a string
function fecha($fecha){
$timestamp = strtotime($fecha);
$meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
$day = date('d', $timestamp);
$month = date('m', $timestamp) - 1;
$year = date('Y', $timestamp);
$fecha = $day . ' de ' . $meses[$month] . " del " . $year;
return $fecha;
}
//comprueba si la sesión de admin se encuentra iniciada
function comprobar_sesion(){
if (!isset($_SESSION['admin'])) {
header('Location: ' . RUTA);
}
}
?>