I have my table and it shows me the results I want but I can not make it delete the id from my db. they recommend that you add an 'onClick' event with VanillaJS or any JS framework where, when you click on it, it sends you to a page where you delete / edit the record. Peron I have no idea how it is done or can be. Thank you very much for your input.
<?php
/**
* Maneja la conexión a la base de datos
*/
class Connect
{
/** @var string */
protected $error;
/** @var string */
protected $user;
/** @var string */
protected $pass;
/** @var string */
protected $base;
/** @var string */
protected $server;
/** @var mysqli */
protected $connection;
/**
* Connect constructor.
*
* @param string $user
* @param string $pass
* @param string $base
* @param string $server
*/
public function __construct($user, $pass, $base, $server)
{
$this->user = $user;
$this->pass = $pass;
$this->base = $base;
$this->server = $server;
}
/**
* Conecta a la base de datos
*
* @return mysqli
*/
public function connect()
{
$this->connection = new mysqli($this->server, $this->user, $this->pass, $this->base);
if ($this->connection->connect_errno) {
throw new \UnexpectedValueException("Fallo al conectar a MySQL: ({$this->connection->connect_errno}) " .
$this->connection->connect_error);
}
return $this->connection;
}
}
/**
* Maneja las acciones de Trabajo
*/
class Trabajo
{
/** @var mysqli */
protected $connection;
/**
* Trabajo constructor.
*
* @param mysqli $conexion
*/
public function __construct(mysqli $conexion)
{
$this->connection = $conexion;
}
/**
* Obtiene los registros a desplegar
*
* @return array
*/
public function getData()
{
$data = [];
$result = $this->connection->query('SELECT * FROM pagos_pagina');
while ($row = $result->fetch_object()) {
$valida = $this->hasReferences($row->referencia);
$row->valida = $valida;
$data[] = $row;
}
return $data;
}
/**
* Valida si existen referencias
*
* @param string $reference
*
* @return bool
*/
protected function hasReferences($reference)
{
$statement = $this->connection->prepare('SELECT COUNT(*) AS cant FROM archivos_pagos WHERE referencia = ?');
$statement->bind_param('s', $reference);
$statement->execute();
$hayResultados = $statement->get_result()->fetch_object();
$statement->close();
return (bool)$hayResultados;
}
}
$connect = new Connect('root', '', 'radius', 'localhost');
$conexion = $connect->connect();
$trabajo = new Trabajo($conexion);
$datos = $trabajo->getData();
?>
<div class="panel-body">
<form>
<table width="70%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Ticket</th>
<th>Usuario</th>
<th>Cedula</th>
<th>Referencia</th>
<th>Monto</th>
<th>Estatus</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
<?php
if (!\count($datos)) {
echo <<< HTML
<tr>
<th colspan="8">No hay registros</th>
</tr>
HTML;
} else {
$contador = 1;
foreach ($datos as $registro) {
$validTag = $registro->valida ? 'Valido' : 'Invalido';
echo <<< HTML
<tr>
<td>{$contador}</td>
<td>{$registro->nombre} {$registro->apellido}</td>
<td>{$registro->cedula}</td>
<td>{$registro->referencia}</td>
<td>{$registro->monto}</td>
<td>{$validTag}</td>
<td>
<button class='btn btn-warning btn-sm' id='btn-edit-{$registro->referencia}'>Editar</button>
<button class="btn btn-sm btn-info" class='btn btn-warning btn-sm' id='btn-delete-{$registro->referencia}'>Eliminar</button>
</td>
</tr>
HTML;
$contador++;
}
}
?>
</tbody>
</table>
</form>
</div>
Thank you for your contribution