+: pacientes.class.php, pacientes.php

edited: conexion.php convertirUFT8  si dato es null
This commit is contained in:
devfzn 2023-03-24 17:25:43 -03:00
parent 83d7fd1bad
commit 47a202cbf0
Signed by: devfzn
GPG Key ID: E070ECF4A754FDB1
5 changed files with 79 additions and 28 deletions

View File

@ -105,4 +105,5 @@ Combinación de 2 funciones de *php* para generar un *token* único en `<proyect
un `string` *hexadecimal*.
- Función [openssl_random_pseudo_bytes](https://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php)
> [Metodos HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)

View File

@ -6,11 +6,13 @@ $_auth = new auth;
$_respuestas = new respuestas;
if($_SERVER['REQUEST_METHOD'] == "POST"){
// recibir datos
$postBody = file_get_contents("php://input");
// envío de datos al manejador
$datosArray = $_auth->login($postBody);
// respuesta
header('Content-Type: application/json');
if(isset($datosArray["result"]["error_id"])){

View File

@ -2,8 +2,6 @@
class conexion {
#private $debug=true;
private $debug=false;
private $server;
private $user;
private $password;
@ -12,8 +10,6 @@ class conexion {
private $conector;
function __construct(){
if ($this->debug){ echo '</br>'."En constructor clase conexion".'</br>'; }
$listadatos = $this->datosConexion();
foreach ($listadatos as $key => $value) {
$this->server = $value['server'];
@ -22,62 +18,50 @@ class conexion {
$this->database = $value['database'];
$this->port = $value['port'];
}
if ($this->debug){ echo "datos constructor: SERVER: $this->server, USER: $this->user,
PASS: $this->password, DB: $this->database, PORT: $this->port".'</br>';}
if ($this->debug){ echo '</br>'."pre conector".'</br>'; }
try {
$conn = new mysqli("$this->server","$this->user","$this->password","$this->database","$this->port");
$this->conector = $conn;
if ($this->debug){ echo "todo BIEN con la conexion"; }
} catch (Exception $e) {
echo '</br>'."Error al intentar conectar con base de datos!".'</br>';
echo "connect_errno: [$conn->connect_errno]".'</br>';
echo "connect_error: [$conn->connect_error]".'</br>';
echo "Exception: $e".'</br>';
die();
}
if ($this->debug){ echo '</br>'."post conector".'</br>'; }
}
private function datosConexion(){
if ($this->debug){ echo '</br>'."En funcion datosConexion".'</br>'; }
$ruta = dirname(__FILE__);
$jsondata = file_get_contents($ruta . "/" . "config");
if ($this->debug){ echo '</br>'."Ruta: $ruta".'</br>'.'<pre>'; print_r($jsondata); echo '</pre>'.'</br>'; }
return json_decode($jsondata, true);
}
private function convertirUTF8($array){
if ($this->debug){ echo '</br>'."En funcion convertirUTF8".'</br>'; }
private function convertirUTF8($array){
array_walk_recursive($array,function(&$item,$key){
if(!mb_detect_encoding($item,'utf-8',true)){
if(is_null($item)){
$item = utf8_encode("null");
} else if(!mb_detect_encoding($item,'utf-8',true)){
echo "no detectado ?";
$item = utf8_encode($item);
}
});
return $array;
}
// otra forma
#array_walk_recursive($array,function (&$item) {
# $item = mb_convert_encoding($item,'UTF-8');
#});
// otra mas
#array = array_map("utf8_encode", $array );
}
public function obtenerDatos($query){
if ($this->debug){ echo '</br>'."En funcion obtenerDatos".'</br>'; }
$results = $this->conector->query($query);
$resultsArray = array();
foreach ($results as $value) {
$resultsArray[] = $value;
}
if ($this->debug){ echo '<pre>'; print_r($resultsArray); echo '</pre>'.'</br>'; }
return $this->convertirUTF8($resultsArray);
}
public function test_conector(){
echo '</br>'."Funcion test_conector".'</br>';
}
public function nonQuery($sqlstr){
$results = $this->conector->query($sqlstr);
#return $results->affected_rows;

View File

@ -0,0 +1,30 @@
<?php
require_once 'conexion/conexion.php';
require_once 'clases/respuestas.class.php';
class pacientes extends conexion{
private $table = "pacientes";
public function listaPacientes($pagina = 1){
// paginador
$inicio = 0;
$cantidad = 100;
if ($pagina > 1){
$inicio = ($cantidad * ($pagina - 1)) + 1;
$cantidad = $cantidad * $pagina;
}
$query = "SELECT PacienteId, Nombre, DNI, Telefono, Correo
FROM " . $this->table . " limit $inicio, $cantidad";
$datos = parent::obtenerDatos($query);
return ($datos);
}
public function obtenerPaciente($id){
$query = "SELECT * FROM " . $this->table . " WHERE PacienteId = '$id'";
return parent::obtenerDatos($query);
}
}
?>

34
apirest_yt/pacientes.php Normal file
View File

@ -0,0 +1,34 @@
<?php
require_once 'clases/respuestas.class.php';
require_once 'clases/pacientes.class.php';
$_respuestas = new respuestas;
$_pacientes = new pacientes;
#header('Content-Type: text/plain; charset=utf-8');
header('Content-Type: application/json; charset=utf-8');
if ($_SERVER['REQUEST_METHOD'] == "GET"){
if (isset($_GET["page"])){
$pagina = $_GET["page"];
$lista_pacientes = $_pacientes->listaPacientes($pagina);
echo json_encode($lista_pacientes);
} else if (isset($_GET["id"])){
$pacienteid = $_GET["id"];
$datos_paciente = $_pacientes->obtenerPaciente($pacienteid);
echo json_encode($datos_paciente);
}
} else if ($_SERVER['REQUEST_METHOD'] == "POST"){
echo "hola POST";
} else if ($_SERVER['REQUEST_METHOD'] == "PUT"){
echo "hola PUT";
} else if ($_SERVER['REQUEST_METHOD'] == "DELETE"){
echo "hola DELETE";
} else {
$datosArray = $_respuestas->error_405();
echo json_encode($datosArray);
}
?>