0
I have a system with interactive map for registering companies. By clicking on a state it opens a <select>
with the segments of companies of that state, but when selecting a segment it shows me the companies of all states that have that segment instead of showing the companies only of the selected state. That is, if there is the same segment in more than one state it lists all the companies in that segment ignoring the selected state, as shown in the image. Could someone help me solve this? I realized that the problem may be in the database query, because it is not killing one query to start the other. When you click on another state it just adds that state to what was previously selected.
Ajax code
function abre_estado(codigo){
$.ajax({
type:"POST",
data:"codigo="+codigo,
url:"altera_estado.php",
success: function(msg){
$('#segmentos').hide(msg);
$('#segmentos').html(msg);
$('#segmentos').fadeIn(msg);
}
});
}
function abre_empresa(codigo){
$.ajax(
{
url: 'consulta_empresa.php',
type: 'POST',
data: "codigo="+codigo,
success: function(data)
{
var data = data;
$('#centro-direita').html(data).fadeIn();
}
});
Code altera_status.php
$id_estado= $_POST['codigo'];
$consultaSegmentos = $conexao->prepare("SELECT * FROM `empresas` AS e INNER JOIN `segmentos` as s ON (e.segempresas = s.idseg) WHERE `ufempresas` = '$id_estado' GROUP BY `segempresas`");
$consultaSegmentos->execute();
$fetchAll = $consultaSegmentos->fetchAll();
foreach ($fetchAll as $segmentos)
{
<option value="<?php echo $segmentos['idseg'];?>"><?php echo $segmentos['segmento'];?></option>
Code consult_company.php
$id_segment= $_POST['code'];
$consultaEmpresas = $conexao->prepare("SELECT * FROM `empresas` AS e
INNER JOIN `estados` AS t ON (e.ufempresas = t.codigo) WHERE
`ufempresas` = '1' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '2' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '3' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '4' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '5' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '6' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '7' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '8' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '9' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '10' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '11' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '12' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '13' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '14' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '15' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '16' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '17' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '18' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '19' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '20' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '21' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '22' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '23' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '24' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '25' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '26' AND `segempresas` = '$id_segmento' OR
`ufempresas` = '27' AND `segempresas` = '$id_segmento'");
$consultaEmpresas->execute();
echo "
<table>
<thead>
<td>NOME DA EMPRESA</td>
<td>ESTADO</td>
<td>LINK RH</td>
</thead>
";
while ($empresas = $consultaEmpresas->fetch(PDO::FETCH_ASSOC))
{
<div id="centro-direita-select">
<tbody width="100%">
<tr>
<td width="67%"><?php echo $empresas['nome']; ?></td>
<td width="10%"><?php echo $empresas['sigla']; ?></td>
<td><a href ="<?php echo $empresas['url']; ?>"target=_blank"</a>Clique para acessar</td>
</tr>
</tbody>
</div>