0
How could improve the script below, it would be possible to do something with the queries or yes thing, because it does everything I need, more became a bit messy and big, what could do?
<?php
include "conexao.php";
if (!isset($_SESSION)) session_start();
if (!isset($_SESSION['usu_login'])) {
session_destroy();
header("Location: ../../logout.php"); exit;
}
ini_set( 'display_errors', 0 );
session_start();
$var_usu = $_SESSION['usu_id'];
$var_login = $_SESSION['usu_login'];
$var_nome = $_SESSION['usu_nome'];
$var_nivel = $_SESSION['usu_nivel_checklist'];
$var_loja = $_SESSION['usu_loja_id'];
$var_quest = $_SESSION['quest'];
$var_quest_id = $_SESSION['quest_id'];
?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo $emp_desc ; ?></title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<script src="bootstrap/js/jquery.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<center>
<div class="panel panel-danger">
<div class="panel-heading"><?php echo $emp_desc ; ?>
</div>
</div>
</center>
<?php
//CONSULTA PARA LISTAR AS SUB_AREAS
try {
$stmt = $conn->prepare('SELECT
id_area AS AREA,
area_desc AS DESC_AREA,
id_sub_area AS SUB_AREA,
sub_area_nome AS DESC_SUB_AREA
FROM relacaoset AS rs
INNER JOIN area AS ar ON rs.id_area = ar.area_id
INNER JOIN sub_area AS sb ON rs.id_sub_area = sb.sub_area_cod
WHERE id_area = :var_nivel');
$stmt->execute(array('var_nivel' => $var_nivel));
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
$var_area = $row["AREA"];
$var_desc_area = $row["DESC_AREA"];
$var_sub_area = $row["SUB_AREA"];
$var_desc_sub_area = $row["DESC_SUB_AREA"];
//CONSULTA PARA CONTAR AS RESPOSTAS
$q_respostas = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area <>'$var_area'
AND resp_questionario ='$var_quest_id'");
while ($row = $q_respostas->fetch(PDO::FETCH_ASSOC)) {
$var_total_resposta = $row["TOTAL"];
}
//CONSULTA PARA CONTAR AS PERGUNTAS
$q_count_geral = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id > '1'");
while ($row = $q_count_geral->fetch(PDO::FETCH_ASSOC)) {
$var_totalgeral = $row["TOTAL_PERG"];
}
}
$total_perg_area2 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id = '2'");
while ($row = $total_perg_area2->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area2 = $row["TOTAL_PERG"];
}
$total_perg_area3 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id = '3'");
while ($row = $total_perg_area3->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area3 = $row["TOTAL_PERG"];
}
$total_perg_area4 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id = '4'");
while ($row = $total_perg_area4->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area4 = $row["TOTAL_PERG"];
}
$total_perg_area5 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id ='5'");
while ($row = $total_perg_area5->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area5 = $row["TOTAL_PERG"];
}
$total_perg_area6 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id = '6'");
while ($row = $total_perg_area6->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area6 = $row["TOTAL_PERG"];
}
$total_perg_area7 = $conn->query("SELECT
COUNT(rp.id) AS TOTAL_PERG
FROM
relacaoperg AS rp
INNER JOIN perguntas AS p ON rp.id_pergunta = p.perg_id
INNER JOIN area AS a ON rp.id_area = a.area_id
INNER JOIN sub_area AS sb ON rp.id_subgrupo = sb.sub_area_cod
WHERE
a.area_id = '7'");
while ($row = $total_perg_area7->fetch(PDO::FETCH_ASSOC)) {
$var_total_perg_area7 = $row["TOTAL_PERG"];
}
//CONSULTA PARA CONTAR AS RESPOSTAS POR SETORES
$count_area2 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='2'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area2->fetch(PDO::FETCH_ASSOC)) {
$var_area2 = $row["TOTAL"];
}
$count_area3 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='3'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area3->fetch(PDO::FETCH_ASSOC)) {
$var_area3 = $row["TOTAL"];
}
$count_area4 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='4'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area4->fetch(PDO::FETCH_ASSOC)) {
$var_area4 = $row["TOTAL"];
}
$count_area5 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='5'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area5->fetch(PDO::FETCH_ASSOC)) {
$var_area5 = $row["TOTAL"];
}
$count_area6 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='6'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area6->fetch(PDO::FETCH_ASSOC)) {
$var_area6 = $row["TOTAL"];
}
$count_area7 = $conn->query("SELECT
COUNT(resp_pergunta_id) AS TOTAL
FROM respostas
WHERE
resp_loja ='$var_loja'
AND resp_area ='7'
AND resp_questionario ='$var_quest_id'");
while ($row = $count_area7->fetch(PDO::FETCH_ASSOC)) {
$var_area7 = $row["TOTAL"];
}
$total_respostas = $var_area2 +
$var_area3 +
$var_area4 +
$var_area5 +
$var_area6 +
$var_area7;
$diferença = $var_totalgeral - $total_respostas;
if ($var_total_resposta >= $var_totalgeral){
echo"<div class='jumbotron'>
<h4>OLÁ,</h4>
<p>TODOS OS SETORES RESPONDERAM SUAS PERGUNTAS, POR FAVOR CLICAR NO BOTÃO ABAIXO PARA RESPONDER AS PERGUNTAS.
<p><a href='principal.php?sub_area_id=$var_sub_area&area_id=$var_area&desc_sub_area=$var_desc_sub_area&desc_area=$var_desc_area' class='btn btn-primary btn-lg'>RESPONDER</a></p>
</div>";
}else{
echo"<table class='table table-striped table-hover'>
<thead>
<tr>
<th bgcolor='#00FF00'><font color='000000'>TOTAL PERGUNTAS: $var_totalgeral </font></th>
<th bgcolor='#00FF00'><font color='000000'>TOTAL RESPOSTAS: $total_respostas </font></th>
<th bgcolor='#FF0000'><font color='000000'>DIFERENÇA: $diferença </font></th>
</tr>
<tr>
<th>SETOR</th>
<th>TOTAL PERGUNTAS</th>
<th>TOTAL RESPOSTAS</th>
</tr>
</thead>
<tbody>
<tr>
<td>area2</td>
<td>$var_total_perg_area2</td>
<td>$var_area2</td>
</tr>
<tr>
<td>area3</td>
<td>$var_total_perg_area3</td>
<td>$var_area3</td>
</tr>
<tr>
<td>area4</td>
<td>$var_total_perg_area4</td>
<td>$var_area4</td>
</tr>
<tr>
<td>area5</td>
<td>$var_total_perg_area5</td>
<td>$var_area5</td>
</tr>
<tr>
<td>area6</td>
<td>$var_total_perg_area6</td>
<td>$var_area6</td>
</tr>
<tr>
<td>area7</td>
<td>$var_total_perg_area7</td>
<td>$var_area7</td>
</tr>
</tbody>
</table>";
}
} else {
echo"<center><b>NENHUM SETOR SELECIONADO</b><center>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo"<a href='logout.php' class='btn btn-danger btn-lg btn-block'><span class='glyphicon glyphicon-remove' aria-hidden='true'></span> SAIR</a><br>";
?>
</div>
</body>
</html>
did not understand the -2, in my conception the question is clear and I am in need of a hint, it escapes from the rules of the community?
– Chefe Druida
Most of these queries look the same except for the
a.area_id
change, give agroup by
wouldn’t solve this? Another tip when the same process repeats vc play it within a function, and ask as argument the elements that vary.– rray
Thanks @rray I’m already doing, finishing post the finish.
– Chefe Druida