php - Searching multiple values in sql table, with a range -
i have table contains ingredients. example:
id | title | ingredients 1 | ex1 | ketchup, chicken, salt, honey, mountain dew 2 | ex2 | beef, pepper, chili, honey, salt
and when user searchs ingredients like:
ketchup, salt, honey
i generate sql-query:
select * recipes where( (ingredients '%ketchup%') , (ingredients '%salt%') , (ingredients '%honey%')
and returns recipes containing these specific ingredients, , works grey.
now. i've added range-slider, pick how many of entered ingredients should match search return anything. lets chose 2 ingredients should match @ least, want make php function outputs sql string pairs of entered ingredients, don't have mindset it. example:
(ingredients '%ketchup%') , (ingredients '%salt%') or (ingredients '%ketchup%') , (ingredients '%honey%') or on. ketchup & salt pair, ketchup & honey pair, salt & honey pair.
and of course variable theres no limit ingredients inputted. i've tried hours no success. hope i've explained self & able or teach me :-)
my php function current string looks this:
$c_soeg = "ketchup, whatever, whatever. etc"; $c_ing_arr = explode(",", $c_soeg); $c_count_arr = count($c_ing_arr); $i = 0; $c_sql = "select * recipes ("; while($i < $c_count_arr){ $c_sql .= "(ingredients '%".trim($c_ing_arr[$i])."%')"; if($i != $c_count_arr-1){ $c_sql .= " , "; } $i++; } $c_sql .= ")";
and variable contains value of range named
$c_range;
instead of , and or conditions count met criteria. example gives records @ least 2 ingredients match:
select * recipes case when ingredients '%ketchup%' 1 else 0 end + case when ingredients '%salt%' 1 else 0 end + case when ingredients '%honey%' 1 else 0 end > = 2;
Comments
Post a Comment