mysql - PHP Query multiple search -
for php sql search query, have 5 different search criteria (from important least).
for example:
- (most important): name
- area
- color
- shape
- (least important): time
for query, have following:
$ajax_data = $_post['data']; $name = $ajax_data [0]; $area = $ajax_data [1]; $color = $ajax_data [2]; $shape = $ajax_data [3]; $time = $ajax_data [4]; $option = "where name = '$name' , area = '$area' , color = '$color' , shape = '$shape' , time = '$time'"; //not complete $query = "select * $table $option limit 10;
here trying do.
using 5 criteria (name, area, color, shape , time), want query search find related data, meaning should match search criteria best results.
for example, following search criteria sent via ajax:
ajax search criteria: earth, canada, white, square, today
then, want query search identical criteria.
however, there occasions identical data not exist.
in scenario doesn't, want remove least important criteria , search , on till have 10 results.
any suggestions how can write query this?
could try approach build numeric field in table keeps track of number of matches, order descending on field , apply limit @ results?
apologies pseudocode, should enough give idea.
$option = "where name = '$name' , area = '$area' , color = '$color' , shape = '$shape' , time = '$time'"; //not complete $order = "order matchquality desc"; $query = "select *, if(name = '$name', 1, 0) + if(area = '$area', 1, 0) + if(color = '$color', 1, 0) + if(shape = '$shape', 1, 0) + if(time = '$time', 1, 0) matchquality $table $option $order limit 10;";
let me know if have further requirements.
regards,
james
Comments
Post a Comment