Jump to content


Photo

Hiding specific results from a query


  • Please log in to reply
5 replies to this topic

#1 idy

idy
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 12 September 2006 - 12:17 PM

Hello to you all !

I would need your advice and suggestions on how to build the following code. Please note that I am not specifically asking for the full code (!) but I am struggling to get the concepts all perfectly right.

1. The user enters keywords in a search bar. That's easy ;
2. The corresponding data is retrieved from a database through a standard PHP query. That's OK for me ;
3. Now imagine there is a checkbox next to the search bar, allowing the user to filter the results in step 2 according to the checkbox's criteria. How can I automatically hide or highlight the filtered results without making a new query and without reloading the whole page ? I guess there is some javascript / ajax trick there...

In detail :

[1] Table contents :
Column A | Column B
car | blue
car | red
car | blue
truck | blue
truck | red
truck | green

[2] Search bar : looks into column A, and displays results in the following format :
if "car" is sought for :
car | blue
car | red
car | blue

[3] Now imagine there is a checkbox beneath the search bar, allowing the user to select a colour between red, blue and green (or a combination if more than one checked). I have tried to give the same ID (=colour) for each row returned in [1], and then put nothing in the div when a checkbox was unchecked. However, this does not work out (only the first match is hidden) because IDs are unique. Please remember I am trying to automatically update the results table without refreshing the whole page.

Thanks a lot for your help in step [3].

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 12 September 2006 - 12:39 PM

you could use the checkbox to form the query string. Using an if statement you could add...

$qry = "SELECT * FROM `table` WHERE `cala` LIKE '%".$term1."%'";
if ($_post['chkbx1'] === true)
{
$qry .= "AND `colb` = 'ur criteria';
}

//.... and so on.
?>


follow me on twitter @PHPsycho

#3 idy

idy
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 12 September 2006 - 12:54 PM

Thanks for your quick answer, but I am not sure your solution prevents me from running the query over again.

My point was actually that once the query is run the first time, any further filter by the user (through a checkbox for instance) should select a subset of the initial results, and non-relevant results should be hidden dynamically (without refreshing the whole page). Is this clear ?

Thanks a lot.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 12 September 2006 - 02:10 PM

No..

You run the query once but use the check buttons to formulate the string before you run it......

eg.

<?php
$searchname = $_GET['sname'];

$qry = "SELECT * FROM `people` WHERE `surname` LIKE '%" . $searchname . "%'";

if (isset($_GET['chksex'])
{
 if ($_GET['chksex'] == 'male')
 {
  $qry . = " AND `sex` = 'm'";
 }
 else
 {
  $qry . = " AND `sex` = 'f'";
 }
}

$search = mysql_query($qry);
?>

The above code would search for the name you entered and if you checked one (or both) the boxes for sex it would add the criteria to the query BEFORE the query is executed.
follow me on twitter @PHPsycho

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 12 September 2006 - 02:25 PM

It can be jone in Ajax, but it doesn't stop the query being resent, it just stops the page from being refreshed.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 idy

idy
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 12 September 2006 - 02:54 PM

OK cool, thanks to you both.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users