I have a course management system where the teacher registers a class and the student accesses a page with a table with all the classes and subscribes to the one that he thinks best.
The problem is that many students are enrolling in a class that should behave much less then what I wanted to do:
The teacher stipulates the number of students that can participate, then the time a student enrolls in that class, compares the number of students allowed by the teacher with the number of students already registered, if there is still a vacancy, the enrollment is saved, if not, an alert appears and returns to the previous page.
This is the page that the student accesses with the table that shows the existing classes:
<?php
//Conexão e consulta ao Mysql
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_qualquer') or die(mysql_error());
$qry = mysql_query("select * from turmas");
//Pegando os nomes dos campos
$num_fields = mysql_num_fields($qry);//Obtém o número de campos do resultado
for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
$fields[] = mysql_field_name($qry,$i);
}
//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #37444a; color:lightgrey;"> <tr>';
for($i = 0;$i < $num_fields; $i++){
$table .= '<th>'.$fields[$i].'</th>';
}
//Montando o corpo da tabela
$table .= '<tbody style="
background-color: #86979e;
color: #37444a;
">';
while($r = mysql_fetch_array($qry)){
$table .= '<tr>';
for($i = 0;$i < $num_fields; $i++){
$table .= '<td>'.$r[$fields[$i]].'</td>';
}
$table .= '<td><form action="inscricao.php" method="post">';
$table .= '<input type="hidden" name="ID" value="'.$r['ID'].'">';
$table .= '<input type="hidden" name="NOME" value="' . $r["NOME"] . '">';
$table .= '<button class="btn btn-primary"><i class="glyphicon glyphicon-pencil"> Inscreva-se</i></button>';
$table .= '</form></td>';
}
//Finalizando a tabela
$table .= '</tbody></table>';
//Imprimindo a tabela
echo $table;
?>
This is the registration page that appears when the student clicks the button to sign up:
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 style="
margin-top:100px;">Inscrição</h1>
<p> </p>
<p class="lead"></p>
<ul class="list-unstyled">
<form id="cadastro" method="post" action="banco/updateP.php" style="
text-align: left;
margin-top:50px;">
<div class="col-lg-12">
<div class="form-group" style="
text-align: left;">
<label for="FORMACAO">Formação: </label>
<input type="text" required class="form-control" id="FORMACAO" name="FORMACAO" value="<?php echo $formacao; ?>">
</div>
</div>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="TURMA">Turma: </label>
<input type="text" required class="form-control" id="TURMA" name="TURMA" value="<?php echo $turma; ?>">
</div>
</div>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="COLABORADOR">Colaborador: </label>
<select class="form-control" id="COLABORADOR" name="COLABORADOR">
<option>Selecione...</option>
<?php while($colab = mysqli_fetch_array($queryColaboradores)) { ?>
<option value="<?php echo $colab['NOME']; ?>"><?php echo $colab['NOME']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="AREA">Área: </label>
<select class="form-control" id="AREA" name="AREA">
<option> MITV </option>
<option> CTCA </option>
<option> VSPA </option>
</select>
</div>
</div>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="DATA">Data: </label>
<select class="form-control" id="DATA" name="MES">
<option> jan </option>
<option> fev </option>
<option> mar </option>
<option> abr </option>
<option> mai </option>
<option> jun </option>
<option> jul </option>
<option> ago </option>
<option> set </option>
<option> out </option>
<option> nov </option>
<option> dez </option>
</select>
</div>
</div>
<div class="col-lg-12">
<div class="form-group" method="post" style="
text-align: left;">
<label for="PREVISTO">Status: </label>
<input type="text" required class="form-control" id="PREVISTO" name="PREVISTO" value="Previsto">
</div>
<div class="form-check form-check-inline disabled">
<label class="form-check-label">
<input class="form-check-input" type="radio" name="REALIZADO" id="REALIZADO" value="Realizado" disabled> Realizado
</label>
</div>
<div class="">
<button type="submit" class="btn btn-primary btn-lg btn-block"> <i class="glyphicon glyphicon-floppy-disk"> Salvar </i></button>
</div>
<div class="alert alert-info" role="alert">
<strong>Hey! </strong> Antes de realizar o cadastro, certifique-se de que não se esqueceu de nada! :)
</div>
<?php
//ESSE É O TESTE QUE EU TENHO FEITO E QUE NÃO TEM FUNCIONADO
$limite = "SELECT * FROM turmas WHERE ID = 'TURMA' and NOME = 'FORMACAO'";
$contador = "SELECT COUNT(ID) AS ComparaLIMITE FROM participantes WHERE TURMA = 'turma' and FORMACAO = 'formacao'";
if ($contador <= $limite) { header("Location: inscricao.php"); }
else {
echo '<script type="text/javascript">
alert("Essa turma estava completa");
window.history.go(-1);
</script>';
}
}
?>
</div>
</form>
</ul>
</div>
</div>
</div>
What I have done so far was to add one more column in my database table and one field in the teacher form to stipulate the number of students, the field name is LIMIT.
What I want to do is compare the limit with the amount of written on this page with the registration form and the registration is only validated if the limit is less than the number of subscribers.
How do I make this comparison of the number of students already enrolled in that class with the amount stipulated by the teacher when I click the button?