Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced Search Engine

Status
Not open for further replies.

Mairiiv

Programmer
Apr 18, 2002
2
ZA
Hi I need to write a pretty advanced search(Engine) using PHP.
I will need to query a database with a few checkboxes where the result with the most selected should be showed first and then in a diminishing order.
i.e.
I have five checkboxes, the user needn't check all, only the apropriate ones, say he selects 3 of the 5 then I needto show him the results that match those three the other two doesn't play a part in the result(they can be true or false in the result. then after I showed the results that match exactly I must show the next relevant results i.e. results with two of the three.
I don't of yet have a final DB design.

Please any help would be highly appreciated.
Thanx
Mairiiv
 
To do that you must do several queries to the database.

1st you must select data with all 3, then another with all the combinations of 2 out of 3

SELECT ... WHERE (chk1=$chk1 and chk2=$chk2) or (chk1=$chk1 and chk3=$chk3) OR (chk2=$chk2 and chk3=$chk3)

and then 1 out of 3...

and so on.

For this case, is not that hard ... but if he checks 6 ... it's going to be hard to write the queries.

what you can do is, select ALL first and then AT LEAST ONE that is not in the first.

then put everything in an array, check how many hits you got in each line, and add this to the array.

$array[]=array("name"=$name,
"chk1"=$chk1,...
"hits"=0
);

$array[$cod][hits]+=($array[$cod]==$search_chk1) ? 1 : 0;

then build a function that sorts arrays according to this field

function mysort($a,$b){
if ($a[hits]>$b[hits]) return 1;
elseif($a[hits]<$b[hits]) return -1;
else return 0;
}

then you can use usort function

usort($array,&quot;mysort&quot;);

try this. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Hi

I have Surmised that I would need to use multiple queries since my first post. Thanx U gave me a few ideas for further down the strugle and defined better the way I'm going to structure my queries.

Mairiiv
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top