oblivion2010 Posted February 7, 2009 Share Posted February 7, 2009 Hi guys, I'm just getting to grips with PHP. I'm attempting to create a search engine for a Pet Rehoming company. A user selects which shelters they'd like to pick the pet up from along with selecting a preferred breed and sex, the script then shows the user a list of dogs which match the users selections. This site shows exactly what I need to create. http://hermes.hud.ac.uk/u0754700/assign2/ However, like I stated before, I'm still getting to grips and am unsure about the best way to create this. I don't even know where to start, any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/ Share on other sites More sharing options...
ratcateme Posted February 7, 2009 Share Posted February 7, 2009 your going to need PHP/Mysql. you need a mysql database to store all the information about the dogs then a php form to search the database and print out the results. you will also need some kind of admin area for the staff to add dogs and take dogs off the find a home. i recommend going through the php/Mysql tutorials here http://tizag.com/ Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-757026 Share on other sites More sharing options...
oblivion2010 Posted February 9, 2009 Author Share Posted February 9, 2009 Thanks Scott, I've been working on my Database via MySQL. I have made 3 tables; 'Shelters', 'Dogs, and 'Breeds'. I have made the XHTML form, for a user to submit their preferred dog details into. When they click submit the next page should show all of the dogs within my database that match their choices. How would I go about showing these details? 'IF Checkbox 1 and Checkbox 2, Male Radio Button' are all selected, show dog ID 2, 4, and 6. ^^ How would this be coded in PHP? What I've created so far: http://hermes.hud.ac.uk/u0656983/Dog%20Website/ Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-758304 Share on other sites More sharing options...
skiingguru1611 Posted February 9, 2009 Share Posted February 9, 2009 From my understanding you only need 1 table, then have some fields, such as Breed, Shelter, Gender, Friendly(for living with kids), Dogs (living with other dogs). As for the actual query to the DB, I'm not positive, I'll let someone else help you on that. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-758556 Share on other sites More sharing options...
ratcateme Posted February 10, 2009 Share Posted February 10, 2009 well im not sure exactly how you have made your database. but you could make it like the following: Shelters( `ID` INT(11) `name` VARCHAR(50) ) Breeds( `ID` INT(11) `name` VARCHAR(50) ) Dogs( `ID` INT(11) `breed_id` INT(11) `shelters_id` INT(11) `male` INT(1) `child` INT(1) `other_dogs` INT(1) ) (dont think that is valid mysql) then your form could be generated like <?php include "dbConnect.php"; $result = mysql_query("SELECT * FROM `Shelters`"); while($row = mysql_fetch_assoc($result)){ echo "<input type=\"checkbox\" name=\"shelters[]\" id=\"shelter_{$row['ID']}\" value=\"{$row['ID']}\" /><label for=\"shelter_{$row['ID']}\">{$row['name']}</label><br/>"; } $result = mysql_query("SELECT * FROM `Breeds`"); while($row = mysql_fetch_assoc($result)){ echo "<input type=\"checkbox\" name=\"breeds[]\" id=\"breed_{$row['ID']}\" value=\"{$row['ID']}\" /><label for=\"breed_{$row['ID']}\">{$row['name']}</label><br/>"; } ?> <h2 class="title">Gender</h2> <p class="sub">Indicate if you would prefer a male of female</p> <label for="male">Male</label> <label for="female"> <input type="radio" name="sex" id="male" value="Male" /> Female <input type="radio" name="sex" id="female" value="Female" /> </label> <label for="both">Don't Mind</label> <input name="sex" type="radio" id="both" value="both" checked="checked" /> <h2 class="title">Child Friendly?</h2> <p class="sub">Will your dog be living around young children?</p> <input name="child" type="radio" id="yes" value="Yes" checked="checked" /> <label for="yes">Yes</label> <input type="radio" name="child" id="no" value="No" /> <label for="no">No</label> <h2 class="title">Living with other dogs?</h2> <p class="sub">Will your dog be living with any other dogs?</p> <input name="dogs" type="radio" id="yes" value="Yes" checked="checked" /> <label for="yes">Yes</label> <input type="radio" name="dogs" id="no" value="No" /> <label for="no">No</label> <p> <input type="submit" name="submit" id="submit" value="Submit" /> <?php if(isset($_POST['shelters'])){ $breeds_where = ""; if(isset($_POST['breeds'])){ foreach($_POST['breeds'] as $breed){ $breed = mysql_real_escape_string($breed); if($breeds_where != ""){ $breeds_where .= " OR "; } $breeds_where .= "`breed_id` = $breed"; } } $common_where = ""; if($_POST['sex'] == "male"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 1"; }elseif($_POST['sex'] == "female"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 0"; } if($_POST['child'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`child` = 1"; } if($_POST['dogs'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 1"; }elseif($_POST['dogs'] == "no"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 0"; } if($breeds_where != ""){ if($common_where != ""){ $common_where .= " AND "; } $common_where .= "($breeds_where)"; } foreach($_POST['shelters'] as $shelter){ echo "<h2>$shelter</h2><br />"; $shelter = mysql_real_escape_string($shelter); $query = "SELECT * FROM `Dogs` WHERE `shelter_id` = $shelter AND $common_where"; $result = mysql_query($query) or die(mysql_error() . "<br>" . $query); if(mysql_num_rows($result) == 0){ echo "No matching dogs<br />"; continue; } while($row = mysql_fetch_assoc($result)){ $breed = mysql_result(mydsql_query("SELECT `name` FROM `Breeds` WHERE `ID` = {$row['ID']}")or die(mysql_error()),0); echo "<h3>$breed </h3><br/>"; echo ($row['male'] == 1?"Male":"Female"). "<br />"; echo ($row['child'] == 1?"Good For Children":"Not so good with Children"). "<br />"; echo ($row['other_dogs'] == 1?"Good with other dogs":"Not so good with other dogs"). "<br />"; } } } ?> i was board lol think it should work but you need to make some admin scripts and stuff Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-758644 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 Cheers ratcateme for your help. I've been playing with your code for a while, to no avail. When I click submit, it takes me to the results page with no error, but it's just a blank page. I've editted all of your names to match the names in my database, any idea where I just get a blank white screen? <?php $host="localhost"; $username="******"; $pass="******"; mysql_connect($host,$username,$pass); mysql_select_db($username); if(isset($_POST['shelters'])){ $breeds_where = ""; if(isset($_POST['breeds'])){ foreach($_POST['breeds'] as $breed){ $breed = mysql_real_escape_string($breed); if($breeds_where != ""){ $breeds_where .= " OR "; } $breeds_where .= "`breed_id` = $breed"; } } $common_where = ""; if($_POST['sex'] == "male"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 1"; }elseif($_POST['sex'] == "female"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 0"; } if($_POST['child'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`child` = 1"; } if($_POST['dogs'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 1"; }elseif($_POST['dogs'] == "no"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 0"; } if($breeds_where != ""){ if($common_where != ""){ $common_where .= " AND "; } $common_where .= "($breeds_where)"; } foreach($_POST['shelters'] as $shelter){ echo "<h2>$shelter</h2><br />"; $shelter = mysql_real_escape_string($shelter); $query = "SELECT * FROM `dogs` WHERE `shelters_id` = $shelter AND $common_where"; $result = mysql_query($query) or die(mysql_error() . "<br>" . $query); if(mysql_num_rows($result) == 0){ echo "No matching dogs<br />"; continue; } while($row = mysql_fetch_assoc($result)){ $breed = mysql_result(mydsql_query("SELECT `breedname` FROM `breeds` WHERE `id` = {$row['id']}")or die(mysql_error()),0); echo "<h3>$breed </h3><br/>"; echo ($row['male'] == 1?"Male":"Female"). "<br />"; echo ($row['child'] == 1?"Good For Children":"Not so good with Children"). "<br />"; echo ($row['other_dogs'] == 1?"Good with other dogs":"Not so good with other dogs"). "<br />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762367 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 i modified the main code a little bit but i don't see why it shouldn't be working. and also i changed the {$row['id']} in the last query to {$row['breed_id']} <?php if(isset($_POST['shelters'])){ $breeds_where = ""; if(isset($_POST['breeds'])){ foreach($_POST['breeds'] as $breed){ $breed = mysql_real_escape_string($breed); if($breeds_where != ""){ $breeds_where .= " OR "; } $breeds_where .= "`breed_id` = $breed"; } } $common_where = ""; if(isset($_POST['sex'])){ if($_POST['sex'] == "male"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 1"; }elseif($_POST['sex'] == "female"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 0"; } } if(isset($_POST['child'])){ if($_POST['child'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`child` = 1"; } } if(isset($_POST['dogs'])){ if($_POST['dogs'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 1"; }elseif($_POST['dogs'] == "no"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 0"; } } if($breeds_where != ""){ if($common_where != ""){ $common_where .= " AND "; } $common_where .= "($breeds_where)"; } foreach($_POST['shelters'] as $shelter){ echo "<h2>$shelter</h2><br />"; $shelter = mysql_real_escape_string($shelter); $query = "SELECT * FROM `dogs` WHERE `shelters_id` = $shelter AND $common_where"; $result = mysql_query($query) or die(mysql_error() . "<br>" . $query); if(mysql_num_rows($result) == 0){ echo "No matching dogs<br />"; continue; } while($row = mysql_fetch_assoc($result)){ $breed = mysql_result(mydsql_query("SELECT `breedname` FROM `breeds` WHERE `id` = {$row['breed_id']}")or die(mysql_error()),0); echo "<h3>$breed </h3><br/>"; echo ($row['male'] == 1?"Male":"Female"). "<br />"; echo ($row['child'] == 1?"Good For Children":"Not so good with Children"). "<br />"; echo ($row['other_dogs'] == 1?"Good with other dogs":"Not so good with other dogs"). "<br />"; } } } ?> if it is still not working try adding some debug like echo "at stage..." so you know what the script is doing and can see where it is failing also try print_r($_POST); to see if that is all good Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762383 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 I used the code, and still the same blank page appeared. I then entered 'print_r($_POST);' at the top of the code, and it displayed. Array ( [sex] => Male [child] => Yes [dogs] => Yes [submit] => Submit ) Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762391 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 can you post the html code generated for the form please Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762397 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 Sure mate. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $host="localhost"; $username="******"; $pass="******"; mysql_connect($host,$username,$pass); mysql_select_db($username); $result = mysql_query("SELECT * FROM `shelters`"); while($row = mysql_fetch_assoc($result)){ echo "<input type=\"checkbox\" name=\"shelters[]\" id=\"shelters_{$row['id']}\" value=\"{$row['id']}\" /><label for=\"shelters_{$row['id']}\">{$row['sheltername']}</label><br/>"; } $result = mysql_query("SELECT * FROM `breeds`"); while($row = mysql_fetch_assoc($result)){ echo "<input type=\"checkbox\" name=\"breeds[]\" id=\"breeds_{$row['id']}\" value=\"{$row['id']}\" /><label for=\"breeds_{$row['id']}\">{$row['breedname']}</label><br/>"; } ?> <h2 class="title">Gender</h2> <p class="sub">Indicate if you would prefer a male of female</p> <form method="post" action="results.php"> <label for="male">Male</label> <input type="radio" name="sex" id="male" value="Male" /> <label for="female"> Female <input type="radio" name="sex" id="female" value="Female" /> </label> <label for="both">Don't Mind</label> <input name="sex" type="radio" id="both" value="both" checked="checked" /> <h2 class="title">Child Friendly?</h2> <p class="sub">Will your dog be living around young children?</p> <input name="child" type="radio" id="yes" value="Yes" checked="checked" /> <label for="yes">Yes</label> <input type="radio" name="child" id="no" value="No" /> <label for="no">No</label> <h2 class="title">Living with other dogs?</h2> <p class="sub">Will your dog be living with any other dogs?</p> <input name="dogs" type="radio" id="yes" value="Yes" checked="checked" /> <label for="yes">Yes</label> <input type="radio" name="dogs" id="no" value="No" /> <label for="no">No</label> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762399 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 when you tested it did you select some breads/shelters? i can't really see anything wrong but can you post the html that that php generates for the form. Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762401 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 I only have two pages, an Index.php (2nd post) and a results.php page (1st post) I've attached both. (Had to call the Index page a different name due to restrictions on the forum) [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762408 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 worked it out shift this line <form method="post" action="results.php"> to above where it prints out all the checkboxes and it should be fine Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762416 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 Ahh, it is now giving me results, but it's only giving me the ID for the Breed and the Shelter. Check it out: http://hermes.hud.ac.uk/u0656983/Dog%20Website/ Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762420 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 i am a bit confused by the results it is outputting but try this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php mysql_connect($host,$username,$pass); mysql_select_db($username); print_r($_POST); if(isset($_POST['shelters'])){ $breeds_where = ""; if(isset($_POST['breeds'])){ foreach($_POST['breeds'] as $breed){ $breed = mysql_real_escape_string($breed); if($breeds_where != ""){ $breeds_where .= " OR "; } $breeds_where .= "`breed_id` = $breed"; } echo "<h3>$breed </h3><br/>"; } $common_where = ""; if(isset($_POST['sex'])){ if($_POST['sex'] == "male"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 1"; }elseif($_POST['sex'] == "female"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`male` = 0"; } } if(isset($_POST['child'])){ if($_POST['child'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`child` = 1"; } } if(isset($_POST['dogs'])){ if($_POST['dogs'] == "yes"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 1"; }elseif($_POST['dogs'] == "no"){ if($common_where != ""){ $common_where .= " AND "; } $common_where = "`other_dogs` = 0"; } } if($breeds_where != ""){ if($common_where != ""){ $common_where .= " AND "; } $common_where .= "($breeds_where)"; } foreach($_POST['shelters'] as $shelter){ $shelter = mysql_fetch_assoc(mydsql_query("SELECT `sheltername` FROM `shelters` WHERE `id` = {$shelter}")or die(mysql_error()),0); $shelter = $shelter['sheltername']; echo "<h2>$shelter</h2><br />"; $shelter = mysql_real_escape_string($shelter); $query = "SELECT * FROM `dogs` WHERE `shelters_id` = $shelter AND $common_where"; $result = mysql_query($query) or die(mysql_error() . "<br>" . $query); if(mysql_num_rows($result) == 0){ echo "No matching dogs<br />"; continue; } while($row = mysql_fetch_assoc($result)){ $breed = mysql_fetch_assoc(mydsql_query("SELECT `breedname` FROM `breeds` WHERE `id` = {$row['breed_id']}")or die(mysql_error()),0); $breed = $breed['breedname']; echo "<h3>$breed </h3><br/>"; echo ($row['male'] == 1?"Male":"Female"). "<br />"; echo ($row['child'] == 1?"Good For Children":"Not so good with Children"). "<br />"; echo ($row['other_dogs'] == 1?"Good with other dogs":"Not so good with other dogs"). "<br />"; } } } ?> </body> </html> Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762422 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 Afraid not, more of the same. Why is it displaying Array ( [shelters] => Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 5 [4] => 6 [5] => 7 ) [breeds] => Array ( [0] => 5 [1] => 6 ) [sex] => Male [child] => No [dogs] => No [submit] => Submit ) At the top of the results page? Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762426 Share on other sites More sharing options...
ratcateme Posted February 14, 2009 Share Posted February 14, 2009 that is the print_r($_POST) it is all the data being sent to the page are you sure you are updating the results page because the output i get just could not come from the code i see Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762427 Share on other sites More sharing options...
oblivion2010 Posted February 14, 2009 Author Share Posted February 14, 2009 Yeah, I'm updating the code with every update you're posting. The only output that is being displayed is the ID of the last breed that is checked. Strange. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762430 Share on other sites More sharing options...
ratcateme Posted February 15, 2009 Share Posted February 15, 2009 i have no clue what is going wrong because if you number this code lines starting from 1 foreach($_POST['shelters'] as $shelter){ $shelter = mysql_fetch_assoc(mydsql_query("SELECT `sheltername` FROM `shelters` WHERE `id` = {$shelter}")or die(mysql_error()),0); $shelter = $shelter['sheltername']; echo "<h2>$shelter</h2><br />"; $shelter = mysql_real_escape_string($shelter); $query = "SELECT * FROM `dogs` WHERE `shelters_id` = $shelter AND $common_where"; $result = mysql_query($query) or die(mysql_error() . "<br>" . $query); if(mysql_num_rows($result) == 0){ echo "No matching dogs<br />"; continue; } while($row = mysql_fetch_assoc($result)){ $breed = mysql_fetch_assoc(mydsql_query("SELECT `breedname` FROM `breeds` WHERE `id` = {$row['breed_id']}")or die(mysql_error()),0); $breed = $breed['breedname']; echo "<h3>$breed </h3><br/>"; echo ($row['male'] == 1?"Male":"Female"). "<br />"; echo ($row['child'] == 1?"Good For Children":"Not so good with Children"). "<br />"; echo ($row['other_dogs'] == 1?"Good with other dogs":"Not so good with other dogs"). "<br />"; } } you should get <h2>shelter</h2><br /><h3>breed name</h3><br/> at the very least it should have information about the dong on the end i am totally confused as to how you could get the <h3></h3> part without the <h2> part and how you could get a number not a breed name Scott. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762440 Share on other sites More sharing options...
oblivion2010 Posted February 15, 2009 Author Share Posted February 15, 2009 Doesn't make much sense, maybe I should just strat from scratch, new database, new PHP. Getting late on this side of the world, so I'll be back tommorow, thanks for all your help. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762445 Share on other sites More sharing options...
ratcateme Posted February 15, 2009 Share Posted February 15, 2009 sorry i can't be more help i just can't work out what is missing Quote Link to comment https://forums.phpfreaks.com/topic/144250-search-form-basic/#findComment-762446 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.