Jump to content

Search Form - Basic.


oblivion2010

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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/

 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

   }  

}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

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.