twsowerby Posted April 14, 2008 Share Posted April 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 14, 2008 Share Posted April 14, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
twsowerby Posted April 14, 2008 Author Share Posted April 14, 2008 Awesome, that worked a treat, thanks! Regards, Tom Quote Link to comment Share on other sites More sharing options...
twsowerby Posted April 14, 2008 Author Share Posted April 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 14, 2008 Share Posted April 14, 2008 (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: + - * / % Quote Link to comment Share on other sites More sharing options...
twsowerby Posted April 14, 2008 Author Share Posted April 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 14, 2008 Share Posted April 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
twsowerby Posted April 14, 2008 Author Share Posted April 14, 2008 Awesome, thank you. You've been a great help. Regards, Tom Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.