Jump to content

Multi inputs search php doesnt works


carlitoway

Recommended Posts

Hello guys, Im new in php and im trying to creat a multi inputs search...

here is my problem I want to search on three inputs select (tipo,ciudad and precio) searching on the web i have this code, but it doesnt work, just show me "Not results" so here is my code, any help will be appreciated


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
	<p>Tipo</p>
	<select name="opt1">
		<option value="" selected>all</option>
		<option value="nave">Naves</option>
		<option value="penthouse">Penthouse</option>
		<option value="residencia">Residencias</option>
		<option value="villa">Villas</option>
	</select>
	<p>Ciudad</p>
	<select name="opt2">
		<option value="santo domingo">Santo Domingo</option>				<option value="samana">Samana</option>
	</select>
	<p>Precios</p>	
	<select name="opt3">
		<option value="terrenas">terrenas</option>
	</select>
	<input type='submit'>
</form>

<?php 					
	include ("conexion.php");
	
	$opt1 = $_POST['opt1'];
	$opt2 = $_POST['opt2'];
	$opt3 = $_POST['opt3'];
	
	$result = mysql_query("SELECT * FROM `inmuebles` WHERE `tipo` = '".$opt1."' AND `ciudad` = '".$opt2."' AND `sector` = '".$opt3."'"); 
								
	if ($row = mysql_fetch_array($result)){ 
	   echo "
			<table>
				<tr>
					<td>" .$row["tipo"]. "</td>
					<td>" .$row["ciudad"]. "</td>
					<td>" .$row["sector"]. "</td>
				</tr>
			</table>	  
			"; 
			}
		else { 
		echo "<div class='container' style='text-align:center;'>No disponible</div>"; 
	}
?>

</div>
	
	
	
	
	

Any help will be appreciated

Link to comment
Share on other sites

Thank you Barad... my code was wrong in the select SQL Now its works but just show me one record and the same code on my PhpmyAdmin works perfectly and show me all the Records, and now i dont know what im doing wrong.... this is what i have change and I also did what you said.. Thank you very much bro!

 

$result = mysql_query(" SELECT * FROM `inmuebles` WHERE tipo = '".$opt1."' OR ciudad = '".$opt2."' OR sector = '".$opt3."'  "); 
Link to comment
Share on other sites

 

	if ($row = mysql_fetch_array($result)){ 
	   echo "
			<table>
				<tr>
					<td>" .$row["tipo"]. "</td>
					<td>" .$row["ciudad"]. "</td>
					<td>" .$row["sector"]. "</td>
				</tr>
			</table>	  
			"; 
			}

 

You are only fetching and echo'ing ONE row, so that is all you see. You will need to loop through the returned rows:

 

	if (mysql_num_rows($result)) {
		echo "<TABLE>";
		while ($row = mysql_fetch_array($result)) {
		echo "	<tr>
					<td>" .$row["tipo"]. "</td>
					<td>" .$row["ciudad"]. "</td>
					<td>" .$row["sector"]. "</td>
				</tr>";
		}
		echo "</TABLE>";
	}
Link to comment
Share on other sites

WOW :o  THIS REALLY WORKS NOW!!!! MANY MANY THANKS GUYS I DO APPRECIATE YOUR HELP!  ;D

 

and now I will leave the entre code to all those guys who will search for the same thing! this is really important and all thanks to you guyz DavidAM and Barand and Filecipher&  Adrian Hendrich from google+


<div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='POST'>
	<p>Tipo</p>
	<select name="opt1">
		<option selected>all</option>
		<option value="apartamentos">Apartamentos</option>
		<option value="nave">Naves</option>
		<option value="penthouse">Penthouse</option>
		<option value="residencia">Residencias</option>
		<option value="villa">Villas</option>
	</select>
	<p>Ciudad</p>
	<select name="opt2">
		<option selected>all</option>
		<option value="santo domingo">Santo Domingo</option>
		<option value="santiago">Santiago</option>
		<option value="samana">Samana</option>
		<option value="punta cana">punta cana</option>
	</select>
	<p>Precios</p>	
	<select name="opt3">
		<option selected>all</option>
		<option value="terrenas">terrenas</option>
		<option value="bavaro">bavaro</option>
		<option value="naco">naco</option>
	</select>
	<input type='submit'>
</form>

		<?php 					
			include ("conexion.php");
			
			$opt1 = $_POST['opt1'];
			$opt2 = $_POST['opt2'];
			$opt3 = $_POST['opt3'];
			
			$result = mysql_query(" SELECT * FROM `inmuebles` WHERE tipo = '".$opt1."' OR ciudad = '".$opt2."' OR sector = '".$opt3."'  "); 
											
			if (mysql_num_rows($result)) {
				echo "<TABLE>";
				while ($row = mysql_fetch_array($result)) {
				echo "	<tr>
							<td>" .$row["tipo"]. "</td>
							<td>" .$row["ciudad"]. "</td>
							<td>" .$row["sector"]. "</td>
						</tr>";
				}
				echo "</TABLE>";
			}
		?>

</div>

thank you very much for your help guyz! God bless you all

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.