Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pellegrini2106

Deletando registro direto da consulta no padrão MVC

Recommended Posts

Ola galera do Fórum php !!!

Tenho um problema com relação a uma exclusão direta da consulta realizada no padrão MVC onde queria excluir diretamente os registros de uma consulta e depois de ser excluída mandar uma mensagem informando que o registro foi deletado com sucesso como faço!!!!

Obrigado!!!

Modelo:

class Setor {

private $id_setor;
private $desc;
private $cnx;

function getId_setor() {
return $this->id_setor;
}

function getDesc() {
return $this->desc;
}

function setId_setor($id_setor) {
$this->id_setor = $id_setor;
}

function setDesc($desc) {
$this->desc = $desc;
}

public function excluirSt() {

try {
$sql = "DELETE FROM tbsetor WHERE id_setor = '$this->id_setor'";
$campo = $this->cnx->prepare($sql);
$campo->bindValue(1, $this->id_setor, PDO::PARAM_INT);
$campo->execute();
echo '<div class="alert-danger">Dado Excluido com Sucesso!!</div>';
} catch (PDOException $ex) {
echo '<div class="alert-danger">' . $ex->getMessage() . '</div>';
}
}

}

Controle:

class ControleSetor {

private $cts;

function __construct() {

$this->cts = new Setor();
}

public function ctlExcluirSt() {

if (isset($_POST['btnexcluir'])) {
$this->cts->setId_setor($_GET['id_setor']);
return $this->cts->excluirSt();
}
}

}

Visao:

<?php
include '../controle/controleSetor.php';
$vs = new ControleSetor();
$array = $vs->ctlListarSetor();
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Consultar Setor:</title>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="../css/Estilo.css"/>
<link rel="stylesheet" type="text/css" href="../css/Formulario.css"/>
<link rel="stylesheet" type="text/css" href="../css/EstilizarInputs.css"/>
<link rel="stylesheet" type="text/css" href="../css/alerta.css"/>
<link rel="stylesheet" type="text/css" href="../css/material.css"/>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>

<!-- Java Script-->
<script language="javascript" type="application/javascript" src="../js/jquery.min.js"></script>
<script language="javascript" type="application/javascript" src="../js/Fechar.js"></script>
<script language="javascript" type="application/javascript" src="../js/material.js"></script>
<style type="text/css">
body,td,th {
font-family: "Times New Roman";
}
</style>
</head>
<body>
<!DOCTYPE HTML>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Editar Setor:</title>
<!--CSS-->
<link rel="stylesheet" type="text/css" href="../css/Estilo.css"/>
<link rel="stylesheet" type="text/css" href="../css/Formulario.css"/>
<link rel="stylesheet" type="text/css" href="../css/EstilizarInputs.css"/>
<link rel="stylesheet" type="text/css" href="../css/alerta.css"/>
<link rel="stylesheet" type="text/css" href="../css/material.css"/>
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
<!-- Java Script-->
<script language="javascript" type="application/javascript" src="../js/jquery.min.js"></script>
<script language="javascript" type="application/javascript" src="../js/Fechar.js"></script>
<script language="javascript" type="application/javascript" src="../js/material.js"></script>
<body>

<div class="panel panel-body">
<form action="" method="post">
<table align="center" width="574" border="0">
<tr>
<td width="331" align="center"><input size="50" name="txtpesquisar" type="text"></td>
<td width="162" align="center"><input class="botaoCadEdit" name="btnpesq" type="submit" value="Consultar:"></td>
<td width="67" align="center"><a href="../index.php"><img id="tt2" class="icon material-icons" align="right" height="35" width="35" src="../imagens/icones/left-arrow-6.png"></a></td>
</tr>
</table>
</form>
</div>
<div class="panel panel-default">
<form action="controleSetor.php" method="post">
<table align="center" id="letrasC" class="table table-condensed" width="434" border="0">
<tr>
<td width="144" align="center">Setor:</td>
<td width="116" align="center">Editar:</td>
<td width="160" align="center">Excluir:</td>
</tr>
<?php foreach ($array as $key => $valor) { ?>
<tr>
<td align="center"><?= $valor['descricao']; ?></td>
<td align="center"><a href="VisaoEditarSetor.php?id_setor=<?= $valor['id_setor']; ?>"> <img src="../imagens/icones/edit.png" width="35" height="35"></a></td>
<td align="center"><a href="VisaoExcluirSetor.php?id_setor=<?= $valor['id_setor']; ?>">
<img src="../imagens/icones/delete-1.png" width="35" height="35"></a></td>
<?php } ?>
</table>
</form>
</div>
<a href="../index.php">
<div class="mdl-tooltip mdl-tooltip--large" for="tt2">VOLTAR:</div>
</a>
</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Só pegar o Status da Query e retornar, basicamente Getters e Setters, bem como você já fez para outro atributos.

    public $status;

    /**
     * @return mixed
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }
$sql = "DELETE FROM tbsetor WHERE id_setor = '$this->id_setor'";
$campo = $this->cnx->prepare($sql);
$campo->bindValue(1, $this->id_setor, PDO::PARAM_INT);
$campo->execute();

//Conta a quantidade de linhas exluidas 
if ($campo->rowCount() > 0) {

   //Informa a função que os dados formam excluidos
    $this->setStatus('Dado Excluido com Sucesso!!!');
    
    //ou 
    $this->status = 'Dado Excluido com Sucesso!!';

    //ou 
    $this->status = true;

}

Para pegar fora tu sabe né, Depende da forma como foi setada!

if(!empty($var->getStatus() === true) ){
     echo 'Dado Excluido com Sucesso!!';
}

Outro detalhe é que seu Controller pode herdar a Model

class ControleSetor extends Setor {


}

oque deixaria sua classe mais limpa

class ControleSetor extends Setor {

    public function ctlExcluirSt() {

        if (isset($_POST['btnexcluir'])) {
            parent::setId_setor($_GET['id_setor']);
            return parent::excluirSt();
        }

    }

}


// ou


class ControleSetor extends Setor {

    public function ctlExcluirSt() {

        if (isset($_POST['btnexcluir'])) {
            $this->setId_setor($_GET['id_setor']);
            return $this->excluirSt();
        }

    }

}

Conceitos de Herança no PHP

http://www.htmlstaff.org/ver.php?id=23368


Pois o construct é totalmente desnecessário e não esqueça de tratar os dados no controller.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.