Ir para conteúdo

Arquivado

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

Rafael Augusto_173176

Select não consulta corretamente

Recommended Posts

Boa tarde pessoal,

 

Fiz um Select dinamico, porem com algumas buscas ele não encontra o resultado, como se passasse o valor nulo.

 

Index.php

	<div class="busca" style="background: #ffc928; padding: 10px;">
		<form action="" method="post" enctype="multipart/form-data">
			<label>
				
				<select name="seguimento">
					<option value="" selected>Seguimento...</option>
					<option value="4 rodas">4 rodas</option>
					<option value="2 rodas">2 rodas</option>
					<option value="Diesel">Diesel</option>
				</select>
			</label>

			<label>
				
				<select name="fabricante">
					<option value="" selected>Fabricante...</option>
				</select>
			</label>

			<label>
				
				<select name="veiculo">
					<option value="" selected>Veículo...</option>
				</select>
			</label>

			<label>
				
				<select name="motor">
					<option value="" selected>Motor...</option>
				</select>
			</label>
		</form>

		<table border="1">
			<thead>
				<tr>
					<td>Veículo</td>
					<td>Motor</td>
					<td>Combustível</td>

					<td>Ano</td>
					<td>NGK</td>
					<td>NGK Green</td>

					<td>MM</td>

					<td>Ano Cabo</td>
					<td>Cabo</td>.

					<td>Ano Igniçãoi</td>
					<td>Bobina</td>
				</tr>
			</thead>
			<div id="status"></div>
			<tbody id="conteudo">
			</tbody>
			</thead>
		</table>
		</div>

functions.js

$(function(){
	$('select[name=seguimento]').change(function(){
		var seguimento = $(this).val();
		$('select[name=fabricante]').html('<option value="" selected>Selecione o fabricante</option>');
		$('select[name=veiculo]').html('<option value="" selected>Selecione o veículo</option>');
		$('select[name=motor]').html('<option value="" selected>Selecione o Motor</option>');

		$.ajax({
			method: 'post',
			url: 'combo.php',
			data:{opcao: 'seguimento', seguimento: seguimento},
			dataType: 'json',
			beforeSend: function(){
				$('#status').html('Aguarde, buscando...');
			},
			success: function(retorno){
				if(retorno.status == 0){
					$('#status').html('Não encontramos fabricantes!');
				}else{
					$('#status').html('');
					$('select[name=fabricante]').html(retorno.conteudo);
				}
			}
		});
	});

	$('select[name=fabricante]').change(function(){
		var fabricante = $(this).val();
		var seguimento = $('select[name=seguimento]').val();

		$.ajax({
			method: 'post',
			url: 'combo.php',
			data:{opcao: 'fabricante', fabricante: fabricante, seguimento: seguimento},
			dataType: 'json',
			beforeSend: function(){
				$('#status').html('Aguarde, buscando...');
			},
			success: function(retorno){
				if(retorno.status == 0){
					$('#status').html('Não encontramos os veículos!');
				}else{
					$('#status').html('');
					$('select[name=veiculo]').html(retorno.conteudo);
				}
			}
		});
	});

	$('select[name=veiculo]').change(function(){
		var fabricante = $('select[name=fabricante]').val();
		var seguimento = $('select[name=seguimento]').val();
		var veiculo = $(this).val();
		$.ajax({
			method: 'post',
			url: 'combo.php',
			data:{opcao: 'veiculo', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo},
			dataType: 'json',
			beforeSend: function(){
				$('#status').html('Aguarde, buscando...');
			},
			success: function(retorno){
				if(retorno.status == 0){
					$('#status').html('Não encontramos os Motores!');
				}else{
					$('#status').html('');
					$('select[name=motor]').html(retorno.conteudo);
				}
			}
		});
	});

	$('select[name=motor]').change(function(){
		var fabricante = $('select[name=fabricante]').val();
		var seguimento = $('select[name=seguimento]').val();
		var veiculo = $('select[name=veiculo]').val();
		var motor = $(this).val();

		$.ajax({
			method: 'post',
			url: 'combo.php',
			data:{opcao: 'motor', fabricante: fabricante, seguimento: seguimento, veiculo: veiculo, motor: motor},
			dataType: 'json',
			beforeSend: function(){
				$('#status').html('Aguarde, buscando...');
			},
			success: function(retorno){
				$('#status').html('');
				$('tbody#conteudo').html(retorno.conteudo);
			}
		});
	});
});

combo.php

<?php
	if(isset($_POST)){
		include_once "config.php";
		$retorno = array();
		$retorno['status'] = 0;
		$retorno['conteudo'] = '';

		switch($_POST['opcao']){
			case 'seguimento':
			//pego o fabricante
				$seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
				$pegaFabricante = $pdo->prepare("SELECT * FROM `busca_rapida` WHERE `seguimento` = ? GROUP BY `fabricante`");
				$pegaFabricante->execute(array($seguimento));
				if($pegaFabricante->rowCount() == 0){
					$retorno['status'] = 0;
				}else{
					$retorno['status'] = 1;
					$retorno['conteudo'] .= '<option value="" selected>Selecione o fabricante</option>';
					while($fabricante = $pegaFabricante->fetchObject()){
						$retorno['conteudo'] .= '<option value="'.$fabricante->fabricante.'">'.$fabricante->fabricante.'</option>';
					}
				}
			break;

			case 'fabricante':
			//pego o veiculo
				$seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
				$fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));

				$pegaVeiculo = $pdo->prepare("SELECT * FROM `busca_rapida` WHERE `seguimento` = ? AND `fabricante` = ? GROUP BY `modelo`");
				$pegaVeiculo->execute(array($seguimento, $fabricante));
				if($pegaVeiculo->rowCount() == 0){
					$retorno['status'] = 0;
				}else{
					$retorno['status'] = 1;
					$retorno['conteudo'] .= '<option value="" selected>Selecione o veículo</option>';
					while($veiculo = $pegaVeiculo->fetchObject()){
						$retorno['conteudo'] .= '<option value="'.$veiculo->modelo.'">'.$veiculo->modelo.'</option>';
					}
				}
			break;

			case 'veiculo':
			//pego o motor
				$seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
				$fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));
				$veiculo = strip_tags(trim(filter_input(INPUT_POST, 'veiculo', FILTER_SANITIZE_STRING)));

				$pegaMotor = $pdo->prepare("SELECT * FROM `busca_rapida` WHERE `seguimento` = ? AND `fabricante` = ? AND `modelo` = ? GROUP BY `motor`");
				$pegaMotor->execute(array($seguimento, $fabricante, $veiculo));
				if($pegaMotor->rowCount() == 0){
					$retorno['status'] = 0;
				}else{
					$retorno['status'] = 1;
					$retorno['conteudo'] .= '<option value="" selected>Selecione o motor</option>';
					while($motor = $pegaMotor->fetchObject()){
						$retorno['conteudo'] .= '<option value="'.$motor->motor.'">'.$motor->motor.'</option>';
					}
				}
			break;

			case 'motor':
			//retorna o que selecinou
				$seguimento = strip_tags(trim(filter_input(INPUT_POST, 'seguimento', FILTER_SANITIZE_STRING)));
				$fabricante = strip_tags(trim(filter_input(INPUT_POST, 'fabricante', FILTER_SANITIZE_STRING)));
				$veiculo = strip_tags(trim(filter_input(INPUT_POST, 'veiculo', FILTER_SANITIZE_STRING)));
				$motor = strip_tags(trim(filter_input(INPUT_POST, 'motor', FILTER_SANITIZE_STRING)));

				$pegaAutomovel = $pdo->prepare("SELECT * FROM `busca_rapida` WHERE `seguimento` = ? AND `fabricante` = ? AND `modelo` = ? AND `motor` = ?");
				$pegaAutomovel->execute(array($seguimento, $fabricante, $veiculo, $motor));
				if($pegaAutomovel->rowCount() == 0){
					$retorno['status'] = 0;
				}else{
					$retorno['status'] = 1;
					while($automovel = $pegaAutomovel->fetchObject()){
						$retorno['conteudo'] .=  '<tr><td>'.$automovel->modelo.'</td><td>'.$automovel->motor.'</td>';
						$retorno['conteudo'] .= '<td>'.$automovel->combustivel.'</td>';

						$retorno['conteudo'] .= '<td>'.$automovel->ano_vela.'</td><td>'.$automovel->ngk.'</td><td>'.$automovel->ngk_green.'</td>';

						$retorno['conteudo'] .= '<td>'.$automovel->gap.'</td>';

						$retorno['conteudo'] .= '<td>'.$automovel->ano_cabo.'</td><td>'.$automovel->cabos_ngk.'</td><td></td><td></td></tr>';

					}
				}
			break;
		}

		echo json_encode($retorno);
	}
?>

a maioria dos resultados são exibidos corretamente, porem alguns fazem a busca de dois selects e o terceiro fica em branco, sendo que tem o valor para ele.

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.