Jump to content

[SOLVED] Checkbox search


twsowerby

Recommended Posts

Hi all,

 

Had a quick look through the archives for the answer to this query but not had any luck so far...

 

I have a database full of villa details. some of the fields are 'pool', 'hottub' and 'tv'. Each villa either has a 1 or a 0 in the pool, tv, hottubs fields depending on whether they have one or not.

 

I need to be able to search villas by checking a checkbox. So if a user wanted to search for villas with a pool and a tv they could check the pool and tv box and click search.

 

I have made a start but it doesnt seem to be working, so far I have this:

 

<form method="post" action="scripts/search_script.php">
	<input type="checkbox" name="pool" value="1" />Pool <br />
	<input type="checkbox" name="hot_tub" value="1" />Hot Tub <br />
	<input type="checkbox" name="tv" value="1" />TV <br />
	<input type="submit" value="Go">
</form> 

 

$query = mysql_query("SELECT * FROM villas WHERE pool = '%$pool%' and hottub = '%$hot_tub%' and tv = '%$tv%'");
while ($row = mysql_fetch_array($query))
{
$Name=$row["Name"];
$Country=$row["country"];
$Price=$row["price"];
echo $Name, $Country, $Price;
}

 

I'm pretty new to PHP so there's bound to be something wrong with my query or syntax. Any help would be greatly appreciated!

 

Regards,

 

Tom

 

 

Link to comment
Share on other sites

Try this for your PHP:

 

<?php
$where = array();
if($_POST['pool'])
  $where[] = "pool = '1'";
if($_POST['hot_tub'])
  $where[] = "hottub = '1'";
if($_POST['tv'])
  $where[] = "tv = '1'";
$sql = "SELECT * FROM villas";
if(count($where))
  $sql .= " WHERE ".implode(' AND ',$where);
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query))
{
$Name=$row["Name"];
$Country=$row["country"];
$Price=$row["price"];
echo $Name, $Country, $Price;
}
?>

Link to comment
Share on other sites

Actually just a quick question, would you mind explaining to me what the '.' does in the second $sql variable? I know what a dot does normally but I don't fully understand its use here.  How does the PHP differentiate between the two $sql's?

 

Would like to know so I can learn from the help you gave me, will prevent me from asking similar questions in the future.

 

Regards,

 

Tom

Link to comment
Share on other sites

(I think you know this already) A period in PHP concatenates two strings:

$var = 'ABC' . 'DEF'; // ABCDEF

 

.= is simply appending to the string:

$var = 'ABC';
$var .= 'DEF'; // ABCDEF

 

it's the short version of this:

$var = 'ABC';
$var = $var . 'DEF'; // ABCDEF

 

Edit: This applies to number operators too: + - * / %

Link to comment
Share on other sites

Thanks Aaron,

 

I did know that it was a concatenation but the use of it there confused me a little bit, haven't quite got my head around how (using your example) PHP can have a variable that looks like it contains itself, ie $var = $var...Am I right in thinking that anything that calls $var in that script would be calling "ABCDEF" and not just "ABC"?

 

Tom

 

 

Link to comment
Share on other sites

Right, it's just adding on to itself.

 

In the solution I originally posted, $sql starts as a simple query string, and then we add stuff to it if checkboxes are checked. Here is a good tuturial for Beginners with PHP:

http://devzone.zend.com/node/view/id/627

And the part about concatenation:

http://devzone.zend.com/node/view/id/625#Heading9

 

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.