0
Well, I’m working on my computer CBT, and its purpose is to control the output of students. I managed to make registration system, deletion, I am already working on the edition, however I would like to implement a button that when clicking, will show more information of the student within a modal. I even managed to do this, but only appears the first record, example:
The table contains Ana and Wilian. If I click to see Ana’s data, everything will appear cute, in case I click on Wilian, will appear Ana’s data. If I reverse and put Wilian and then Ana, only Wilian’s data will appear.
PS. I researched a lot to try a solution, but I am at the beginning of php and I learned only the PDO class, so I easily get confused with php "pure" (if I can call it that).
<head>
<title>MODAL</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--ARQUIVOS PADRÕES BOOTSTRAP-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<?php include("action_usuario.php")?>
<div class="main register-form col-md-12">
<ul class="list-group">
<li class="list-group-item">
<fieldset>
<form action="" method="get" id='form-contato' class="form-horizontal col-md-12 col-lg-12">
<div class='col-lg-6 col-md-7 col-sm-9 col-lg-offset-2 col-xs-12'>
<input type="text" class="form-control" id="termo" name="termo" placeholder="Infome o Nome/Matrícula">
</div>
<button type="submit" class="btn btn-primary col-lg-offset-0 col-xs-offset-2">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span>
</button>
<a href='index.php' class="btn btn-primary">Ver Todos</a>
</form>
</fieldset>
</li>
<li class="list-group-item">
<?php if(!empty($usuarios)):?>
<!-- Tabela de Clientes -->
<div class="table-responsive">
<table class="table table-bordered table-hover ">
<tr class='active'>
<th>Nome</th>
<th>Matrícula</th>
<th>Turma</th>
<th>Nível</th>
<th>Opções</th>
</tr>
<?php foreach($usuarios as $usuario):?>
<tr>
<td><?=$usuario->nome?></td>
<td><?=$usuario->matricula?></td>
<td><?=$usuario->turma?></td>
<td><?=$usuario->nivel?></td>
<td style="width:16%">
<a rel="<?=$usuario->matricula?>" id="view" name="view" class="btn btn-default text-center" role="button" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-eye-open"></span></a>
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"><?php echo $usuario->nome;?></h3>
</div>
<div class="modal-body">
<p>Matrícula: <?php echo $usuario->matricula;?></p>
<p>Turma: <?php echo $usuario->turma;?></p>
<p>Email: <?php echo $usuario->email;?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<a href="#editar.php?matricula=<?=$usuario->matricula?>" class="btn btn-primary text-center" role="button" disabled><span class="glyphicon glyphicon-pencil"></span></a>
<a href="#index.php?delete=<?=$usuario->matricula?>" class="btn btn-danger text-center" role="button" disabled><span class="glyphicon glyphicon-remove"></span></a>
</tr>
<?php endforeach;?>
</table>
</div>
<?php else: ?>
<!-- Mensagem caso não exista clientes ou não encontrado -->
<h5 class="text-center text-default">Não há registros cadastrados ou não correspondem à pesquisa!</h5>
<?php endif; ?>
</li>
</ul>
</div>
</body>
Inclusion, exclusion and search codes (in this order) for users.
<?php
$conexao = conexao::getInstance();
if(isset($_POST['inserir'])){
$matricula = $_POST['matricula'];
$nome = trim(strip_tags($_POST['nome']));
$turma = trim(strip_tags($_POST['turma']));
$email = trim(strip_tags($_POST['email']));
$login = trim(strip_tags($_POST['login']));
$senha = trim(strip_tags($_POST['senha']));
$nivel = trim(strip_tags($_POST['nivel']));
$insert = "INSERT into tab_usuarios (matricula, nome, turma, email, login, senha, nivel) VALUES (:matricula, :nome, :turma, :email, :login, :senha, :nivel)";
try{
$result = $conexao->prepare($insert);
$result->bindParam(':matricula', $matricula, PDO::PARAM_STR);
$result->bindParam(':nome', $nome, PDO::PARAM_STR);
$result->bindParam(':turma', $turma, PDO::PARAM_STR);
$result->bindParam(':email', $email, PDO::PARAM_STR);
$result->bindParam(':login', $login, PDO::PARAM_STR);
$result->bindParam(':senha', $senha, PDO::PARAM_STR);
$result->bindParam(':nivel', $nivel, PDO::PARAM_STR);
$result->execute();
$count = $result->rowCount();
if($count>0){
echo '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<span class="glyphicon glyphicon-ok"></span> <strong>Usuário cadastrado com sucesso!</strong>
</div>';
echo "<meta http-equiv=refresh content='1;URL=index.php'>";//refresh da pagina
}else{
echo '<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<span class="glyphicon glyphicon-remove"></span> <strong>Não foi possivel cadastrar o usuário</strong>
</div>';
echo "<meta http-equiv=refresh content='1;URL=index.php'>";//refresh da pagina
}
}catch(PDOException $e){
echo $e;
}
}?>
<?php
//exclusão de usuário
$conexao = conexao::getInstance();
if(isset($_GET['delete'])){
$matricula = $_GET['delete'];
$del = "DELETE from tab_usuarios WHERE matricula=:matricula";
try{
$result = $conexao->prepare($del);
$result->bindParam(':matricula',$matricula, PDO::PARAM_INT);
$result->execute();
$count = $result->rowCount();
if($count>0){
echo '<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<span class="glyphicon glyphicon-ok"></span> <strong>Usuário deletado com sucesso!</strong>
</div>';
echo "<meta http-equiv=refresh content='1;URL=index.php'>";//refresh da pagina
}else{
echo '<div class="alert alert-msgstatus alert-danger alert-dismissable col-md-6 col-xs-6 col-6 col-md-offset-3 col-xs-offset-3 col-offset-3">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<strong>Houve um erro ao deletar o cadastro.</strong>
</div>';
echo "<meta http-equiv=refresh content='1;URL=index.php'>";//refresh da pagina
}
}catch (PDOWException $erro){ echo $erro;}
}?>
<?php
// Recebe o termo de pesquisa se existir
$termo = (isset($_GET['termo'])) ? $_GET['termo'] : '';
// Verifica se o termo de pesquisa está vazio, se estiver executa uma consulta completa
if (empty($termo)):
$conexao = conexao::getInstance();
$sql = "SELECT * FROM tab_usuarios ORDER BY nome ASC "; //LIMIT $inicio, $quantidade
$stm = $conexao->prepare($sql);
$stm->execute();
$usuarios = $stm->fetchAll(PDO::FETCH_OBJ);
else:
// Executa uma consulta baseada no termo de pesquisa passado como parâmetro
$conexao = conexao::getInstance();
$sql = 'SELECT * FROM tab_usuarios WHERE nome LIKE :nome OR matricula LIKE :matricula OR turma LIKE :turma ORDER BY nome ASC ';
$stm = $conexao->prepare($sql);
$stm->bindValue(':nome', $termo.'%');
$stm->bindValue(':matricula', $termo.'%');
$stm->bindValue(':turma', $termo.'%');
$stm->execute();
$usuarios = $stm->fetchAll(PDO::FETCH_OBJ);
endif; ?>
In case you need, I can be providing an archive with the work files for ease. Thank you in advance and forgive me the lack of skill.
F12 and see the content of your HTML, but already I note that you are creating several modals in your foreach when the ideal would be to create only one ai when opening the data.
– Nelson Aguiar
@Nelsonaguide thanks for warning. Here is the print with HTML content. http://prntscr.com/eds484
– Wilian Silva
As said you are creating more of a modal and all with the same id, this is the problem, when you call the id it will open the first one you find with that id, you would have to put a different id for each modal which is not recommended, because you would create a modal for each record, it would be a lot of code generated without need, ideal would create only one ai when opening load the data either with ajax what I find more convenient or play the own user that comes from PHP by parameter.
– Nelson Aguiar
Got it @Nelsonaguide. Thanks! :)
– Wilian Silva