Jump to content

Creating a search engine with a form


oblivion2010

Recommended Posts

Hey guys, pretty new to PHP, and I'm trying to create a dog search engine.

 

I'm trying to use checkboxes, and radio buttons on a form, with the results showing a list of dogs that match the users critera.

 

The code I'm using on the results.php page are below.

 

<?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";
      }
  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){
      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 />";
      }


   }

}
?>

 

This code is only showing me the ID numbers of the last selected breed / shelter's.

 

See here:

http://hermes.hud.ac.uk/u0656983/Dog%20Website/

 

Any idea's how I can rectify this code, or is there a better way to go about doing a search engine using a form?

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/145338-creating-a-search-engine-with-a-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.