The thing is that I'm creating a table where I want the data already inserted can be modified or deleted. The problem is that I have no idea how to do it on my page model. the model is "Model View Controller". Any help?
There are some examples of my code.
Category.php
class Categoria {
private $id;
private $nombre;
private $db;
//CONEXION DB
public function __construct() {
$this->db = DataBase::connect();
}
function getId() {
return $this->id;
}
function getNombre() {
return $this->nombre;
}
function setId($id) {
$this->id = $id;
}
function setNombre($nombre) {
$this->nombre = $this->db->real_escape_string($nombre);
}
public function getAll(){
$categorias = $this->db->query("SELECT * FROM categorias ORDER BY id DESC;");
return $categorias;
}
public function save(){
$sql = "INSERT INTO categorias VALUES(NULL,'{$this->getNombre()}');";
$save = $this->db->query($sql);
$result = false;
if($save){
$result = true;
}
return $result;
}
}
CategoryController.php
require_once 'models / category.php';
class CategoryController {
public function index(){
Utils::isAdmin();
$categoria = new Categoria();
$categorias = $categoria->getAll();
require_once 'views/categoria/index.php';
}
public function crear(){
Utils::isAdmin();
require_once 'views/categoria/crear.php';
}
public function modificar(){
Utils::isAdmin();
require_once 'views/categoria/modificar.php';
}
public function save(){
Utils::isAdmin();
if(isset($_POST) && isset($_POST['nombre'])){
//GUARDAR LA CATEGORIA EN BD
$categoria = new Categoria();
$categoria->setNombre($_POST['nombre']);
$save = $categoria->save();
}
header('location:'.base_url.'categoria/index');
}
}
index.php
DIRTER | REGISTRY
<div id="presentacion">
<!--BARRA DE NAVEGACION-->
<nav class="navbar navbar-light bg-light navbar-expand-md py-2 shadow">
<!--CONTAINER-->
<div class="container">
<!--LOGO-->
<a href="<?=base_url?>" class="navbar-brand border-bottom">DIRTER</a>
</div><!--FIN CONTAINER-->
</nav><!--FIN BARRA NAVEGACION-->
<!--CONTAINER-->
<section class="container">
<a href="<?=base_url?>categoria/crear" class="btn btn-secondary rounded mt-3 btn-block">Crear categoria</a>
<table class="table table-hover border mb-5" id="tabla-categoria">
<thead class="thead-light">
<tr>
<th>ID</th>
<th class="col-md-7">NOMBRE</th>
<th colspan="2">OPERACIONES</th>
</tr>
</thead>
<?php while($cat = $categorias->fetch_object()): ?>
<tbody>
<tr>
<td><?= $cat->id; ?></td>
<td><?= $cat->nombre; ?></td>
<td><a href="<?=base_url?>categoria/modificar" class="px-3 py-1"><i class="fas fa-edit text-dark"></i></a></td>
<td><a href="<?=base_url?>categoria/eliminar" class="px-3 py-1"><i class="fas fa-trash-alt text-dark"></i></a></td>
</tr>
</tbody>
<?php endwhile; ?>
</table>
</section>
</div>
Thank you for your attention.