Jump to content

Archived

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

usman07

PHP Search Form

Recommended Posts

Just to make sure u know that currently i have a index.html and have another file named insert.php that executes the code in the index.html page.

Share this post


Link to post
Share on other sites

ok, make a copy of index.html and name it something along the lines of idx.php, this will give us a safe area to work in.

 

Once that's done have a look at these code snipits

<?php
$roomLimit = 5;
for($minRooms = 0; $minRooms <= $roomLimit; $minRooms++){
  if ($minRooms == 0){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minRooms\">$minRooms</option>";
  }
}
?>
</select>

?>


<select name="min price" ...>
<?php
$priceLimit = 350;
for($minPrice = 40; $minPrice <= $priceLimit; $minPrice+10){
  if ($minPrice == 40){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minPrice\">£$minRooms</option>";
  }
}
?>
</select>

They are going to generate your dropdowns for price and bedrooms.  I've included the start of each <select> so you can see exactly where they will go.

you'll need to add in copy's for the max versions as well, just make sure and change the min to max, you don't need to re-declare the $xxxLimit as it's going to be the same and isn't changed in the loops.  Doing it this way makes the source of the page you see is shorter and easier to maintain. so if in the future you want to add a property that's 600k, or a house with 8 bedrooms, you don't need to add all those lines manualy to keep continuity, just change a number and your done.

Share this post


Link to post
Share on other sites

Sorry where abouts in the html file am i pasting that code?

 

heres the HTML for number of bedrooms:

 


<div id="table2">
	<table id="NBtable">
	<tr>
	<td><p class="NBS">Number of bedrooms:</p></td>
	<td><div id="NB">

	<select name="min bedrooms">
	<option value="none" selected="selected">No Min</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select> to

	<select name="max bedrooms">
	<option value="none" selected="selected">No Max</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select>	
	</div>
</td>

Share this post


Link to post
Share on other sites

ok, revised code, buggered the loop on the price select.

<select name="min bedrooms">
<?php
$roomLimit = 5;
for($minRooms = 0; $minRooms <= $roomLimit; $minRooms++){
  if ($minRooms == 0){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minRooms\">$minRooms</option>";
  }
}
?>
</select>




<select name="min price">
<?php
$priceLimit = 350;
for($minPrice = 40; $minPrice <= $priceLimit; $minPrice =( $minPrice+ 10)){
  if ($minPrice == 40){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minPrice\">£$minPrice</option>";
  }
}
?>
</select>

The code replaces all the html between the opening and closing <select> so:

<div id="table2">
	<table id="NBtable">
	<tr>
	<td><p class="NBS">Number of bedrooms:</p></td>
	<td><div id="NB">

	<select name="min bedrooms">
	<?php
// ***IN HERE***
      ?>
	</select> to

Share this post


Link to post
Share on other sites

Is this right for the minimum and maximum bedrooms:

 


<select name="min bedrooms">
<?php
$roomLimit = 5;
for($minRooms = 0; $minRooms <= $roomLimit; $minRooms++){
  if ($minRooms == 0){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minRooms\">$minRooms</option>";
  }
}
?>
	</select> to

	<select name="max bedrooms">
	<?php
$roomLimit = 5;
for($maxRooms = 0; $maxRooms <= $roomLimit; $maxRooms++){
  if ($maxRooms == 0){
    echo"<option value=\"0\" selected=\"selected\">No Max</option>";
  }
  else{
    echo "<option value\"$maxRooms\">$maxRooms</option>";
  }
}
?>
	</select>	

Share this post


Link to post
Share on other sites

and done for the minimum and maximum price range:

 


<td><p class="PR">Price range:</p></td>
	<td><div id="PR">

	<select name="min price">
	<?php
$priceLimit = 350;
for($minPrice = 40; $minPrice <= $priceLimit; $minPrice+10){
  if ($minPrice == 40){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minPrice\">£$minRooms</option>";
  }
}
?>
	</select> to

	<select name="max price">
	<?php
$priceLimit = 350;
for($maxPrice = 40; $maxPrice <= $priceLimit; $maxPrice+10){
  if ($maxPrice == 40){
    echo"<option value=\"0\" selected=\"selected\">No Max</option>";
  }
  else{
    echo "<option value\"$maxPrice\">£$maxRooms</option>";
  }
}
?>

	</select>

	</div>
</td>
</tr>

	</table>

Share this post


Link to post
Share on other sites

yeah, sorry, that's the screwed up loop version of the code you'll need to change to this:

<select name="min price">
<?php
$priceLimit = 350;
for($minPrice = 40; $minPrice <= $priceLimit; $minPrice =( $minPrice+ 10)){
  if ($minPrice == 40){
    echo"<option value=\"0\" selected=\"selected\">No Min</option>";
  }
  else{
    echo "<option value\"$minPrice\">£$minPrice</option>";
  }
}
?>
</select>

The rest of the page should render after that, the problem is that the screwed up version runns an infinate loop, so the page gets jammed there.

Share this post


Link to post
Share on other sites

Well I'm off for the night, but I'll leave you some stuff to have a play with:

 

first up, make a new file and call it "mstr_ref.inc". in it have the following code (changing the credentials to your own):

<?php
define('__HOST', 'localhost'); //localhost = database host name (normaly this won't need changing)
define('__USER', 'xxx');    //xxx = database username
define('__PASS', 'xxx');    //xxx = database password
define('__DEF_DB', 'xxx'); //xxx = database name

$con = mysql_connect(__HOST, __USER, __PASS) or die(mysql_error());
$db = mysql_select_db(__DEF_DB, $con) or die (mysql_error());
?>

 

then add this code to the top of the idx.php:

<?php
require_once 'mstr_ref.inc';
?>

 

this now meens that the idx.php can connect to the database. which is good, because next we want to use the following piece of code to create the locations element for the form.  This way each time a new location gets added to the database it will show in the list without any other action needed.

 

using what you've picked up from the bedrooms and prices you should be able to insert this bit of code to get the locations displaying out of the database (remember you will need to have them in the database first of all)

 

Note: the <select> is being echoed from within the code this time, so watch out for that.

<?php
$locationSql = "SELECT id, area_name FROM Locations GROUP by area_name";
$locQry = mysql_query($locationSql) or die (mysql_error());
echo "<select name=\"areas\" multiple=\"multiple\" size\"=5\" style=\"width:150px;\">";
while ($location = mysql_fetch_assoc($locQry)){
echo "<option value=\"{$location['id']}\">{$location['area_name']}</option>";
}
echo "</select>";
?>

 

also, we'll use the catagories table for storing the types "house" and "flat" in, so you can use the type column for semi, bungalow etc. 

have a shot scripting the Property Type <select> box to read from the catagories table and display the results.  Everything you need is in the code here, so it should be easy enough.

 

I'll check back in in the morning and work on scripting up some more stuff.  Good Luck

 

Share this post


Link to post
Share on other sites

thanks so much, uv helped me a tremendous lot! no words can thank you enough!

Share this post


Link to post
Share on other sites

Alryt mate, i actually got stuck. for some reason it won't connect to the database? I followed what u said about create a new file called "mstr_ref.inc" and pasted that code that u provided and inserted my own credentials.

Share this post


Link to post
Share on other sites

ok thanks.

 

heres the code in the mstr_ref.inc file

<?php
define('__HOST', ''); //localhost = database host name (normaly this won't need changing)
define('__USER', '');    //xxx = database username
define('__PASS', '');    //xxx = database password
define('__DEF_DB', ''); //xxx = database name

$con = mysql_connect(__HOST, __USER, __PASS) or die(mysql_error());
$db = mysql_select_db(__DEF_DB, $con) or die (mysql_error());
?>

 

and Iv put the code as you said at the top in my index.php file

<!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">
<?php
require_once 'mstr_ref.inc';
?>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Mumtaz Properties</title>
	<link rel="stylesheet" href="cutouts/style.css"/>

</head>

<body>

 

And then when i view this and press the submit button on the form it displays everything that it written in the mstr_ref.inc file like this:

<?php
define('__HOST', ''); //localhost = database host name (normaly this won't need changing)
define('__USER', '');    //xxx = database username
define('__PASS', '');    //xxx = database password
define('__DEF_DB', ''); //xxx = database name

$con = mysql_connect(__HOST, __USER, __PASS) or die(mysql_error());
$db = mysql_select_db(__DEF_DB, $con) or die (mysql_error());
?>

 

Share this post


Link to post
Share on other sites

then the most likely probem is your hosing isn't configured to parse .inc files as php files. change the extension to . php and see what happens.

Share this post


Link to post
Share on other sites

Where should I place the code in my index.php to link the mstr_ref.php file? heres where it is at the moment


<!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>Mumtaz Properties</title>
	<link rel="stylesheet" href="cutouts/style.css"/>
	<?php
require_once 'mstr_ref.php';
?>
</head>

<body>

Share this post


Link to post
Share on other sites

Ok it was my fault, i needed to change the form action like this:

<form action="mstr_ref.php" method="get">

Share this post


Link to post
Share on other sites

Do i place this code in the mstr_ref.php file or in my index.php file where my form is? thanks

<?php
$locationSql = "SELECT id, area_name FROM Locations GROUP by area_name";
$locQry = mysql_query($locationSql) or die (mysql_error());
echo "<select name=\"areas\" multiple=\"multiple\" size\"=5\" style=\"width:150px;\">";
while ($location = mysql_fetch_assoc($locQry)){
echo "<option value=\"{$location['id']}\">{$location['area_name']}</option>";
}
echo "</select>";
?>

Share this post


Link to post
Share on other sites

your form action should still point to where it always did, you're not posting the form to the mstr_ref.php, it's just a file that we are going to reqire_once on each page (and as PHP code as all parsed befor the page reaches the browser, it doesn't really matter where on the page it goes (normaly, there are some exceptions). mstr_ref.php holds the info in it that is of common use, such as database connection.

Do i place this code in the mstr_ref.php file or in my index.php file where my form is? thanks

<?php
$locationSql = "SELECT id, area_name FROM Locations GROUP by area_name";
$locQry = mysql_query($locationSql) or die (mysql_error());
echo "<select name=\"areas\" multiple=\"multiple\" size\"=5\" style=\"width:150px;\">";
while ($location = mysql_fetch_assoc($locQry)){
echo "<option value=\"{$location['id']}\">{$location['area_name']}</option>";
}
echo "</select>";
?>

You put that code in the index.php file, to replace the html code that is making the current <select name="areas">.  None of the code is going off page yet, so clicking the submit button isn't going to do anything new (at least it shouldn't) We still want to have the form point to the page that will be displaying the results.

 

so just now you should have 3 pages : index.php has a form that links to insert.php and mstr_ref.php that is currently used in index.php vie a require_once call.  the form still points -> to insert.php, but as we havn't looked at that page yet, there isn't anything much going to happen when you submit the form.  although I on't think that's really the best name for the page that we want the form to go to as we're not doing anthing that reales to "insert", so once we have the index page sorted out well look at making a new page calling it results.php

Share this post


Link to post
Share on other sites

ahh right ok, so il change it back to what it was cuz it was pointing towards my insert.php file which is the php i previously had that displayed my 'properties' table

<form action="insert.php" method="get">

Share this post


Link to post
Share on other sites

do i place the PHP code for the location like this in the index.php file? thanks.


<td><p class="LOC">Location:</p></td>
<td><div id="LC">

<select multiple="multiple" name="area" size="5" style="width: 150px;" >
<option>Armley</option>
<option>Chapel Allerton</option>
<option>Harehills</option>
<option>Headingley</option>
<option>Hyde Park</option>
<option>Moortown</option>
<option>Roundhay</option>
<?php
$locationSql = "SELECT id, area_name FROM Locations GROUP by area_name";
$locQry = mysql_query($locationSql) or die (mysql_error());
echo "<select name=\"areas\" multiple=\"multiple\" size\"=5\" style=\"width:150px;\">";
while ($location = mysql_fetch_assoc($locQry)){
echo "<option value=\"{$location['id']}\">{$location['area_name']}</option>";
}
echo "</select>";
?>
</select>
</div>

Share this post


Link to post
Share on other sites

×

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.