Jump to content

PHP filtering results


usman07

Recommended Posts

I want to create a feature as you see nn this image, where it says 'filter your results' if a user clicks 'Detached Houses' then only detached houses will be displayed. if a user clicks 'Semi-detached' then only semi detached houses will be shown.

Any help is really appreciated, thank you.


<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>Houses</u></b>
					<br />
					<span class="dh"><b><u>Detached Houses</u></b></span><?php
// Make a MySQL Connection
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM example
WHERE name='Sandy Smith'") or die(mysql_error());  

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table 
echo $row['name']." - ".$row['age'];
?>
					<br />
					<span class="dh"><b><u>Semi-detached houses</u></b></span>
					<br />
					<span class="dh"><b><u>Terraced houses</u></b></span>
					<br />
					<br />
					<b><u>Flats / Apartments</u></b>

</p></div></td>

Link to comment
Share on other sites

How many topics have you created about this now?

 

Make the text a link, and in the link add a parameter of what you want to search on.

It should look like: http://mysite.com/show_houses.php?type=semi-detached

 

Then on your page you'll check if $_GET['type'] is set and has value, and then create your MySQL query to use that.

 

You have to try it before we can help you fix it.

Link to comment
Share on other sites

Is this looking ok so far?


<div id="main">

<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>Houses</u></b>
					<br />
	<a href="www.mumtazproperties.hostei.com/show_houses.php?type=semi-detached"><span class="dh"><b><u>Detached Houses</u></b></span></a>
<?php
// Make a MySQL Connection
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM example
WHERE name='Sandy Smith'") or die(mysql_error());  

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table 
echo $row['name']." - ".$row['age'];
?>
					<br />
					<span class="dh"><b><u>Semi-detached houses</u></b></span>
					<br />
					<span class="dh"><b><u>Terraced houses</u></b></span>
					<br />
					<br />
					<b><u>Flats / Apartments</u></b>

 

One thing u mentioned that im not sure about is 'Then on your page you'll check if $_GET['type'] is set and has value, and then create your MySQL query to use that.'

I have limited knowledge with PHP code i'm afraid, but do i need to set a GET type in the php code?

 

Thank You

 

Link to comment
Share on other sites

One thing u mentioned that im not sure about is 'Then on your page you'll check if $_GET['type'] is set and has value, and then create your MySQL query to use that.'

I have limited knowledge with PHP code i'm afraid, but do i need to set a GET type in the php code?

 

Values passed on the URL are available in the global $_GET array. So, if the user access a page using

http://mysite.com/page.php?foo=bar&this=that

 

Then when that script runs the $_GET array will look like this

array(
    'foo' => 'bar',
    'this' = 'that'
)

 

So, to expand on what jesirose suggested, you would do something like this:

$query = "SELECT * FROM example";
if(isset($_GET['type']))
{
    $query .= " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "'";
}

Link to comment
Share on other sites

Is this the correct way to do this?

<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div id="filter"><p class="houses" style="font-family:helvetica;color:#0155a1;font-size:14px;background:url(cutouts/forsale/filter.jpg) no-repeat;"><b><u>Houses</u></b>
					<br />
	<a href="www.mumtazproperties.hostei.com/show_houses.php?type=detached"><span class="dh"><b><u>Detached Houses</u></b></span></a>
<?php
// Make a MySQL Connection
mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());

// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM example
WHERE name='Sandy Smith'") or die(mysql_error());

$query = "SELECT * FROM example";
if(isset($_GET['type']))
{
    $query .= " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "'";
}  

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
// Print out the contents of each row into a table 
echo $row['name']." - ".$row['age'];
?>
					<br />
					<span class="dh"><b><u>Semi-detached houses</u></b></span>						
					<br />
					<span class="dh"><b><u>Terraced houses</u></b></span>
					<br />
					<br />
					<b><u>Flats / Apartments</u></b>

</p></div></td>

 

where it says 'example' in the php code, is that where I put the name of my table in MYSQL?

Basically what i want it to do is when a user clicks on "detached houses", all the detached house will be displayed from the database.

 

Thank you for your help, much appreciated.

Link to comment
Share on other sites

<?php
  $query = mysql_query("SELECT * FROM something WHERE name='someone'");
  $result = mysql_fetch_array($query);

  var_dump($result);
?>

 

Get the result of the query as an array with mysql_fetch_array(); Then access the items with $result['id'] or $result['surname']

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.