php - Looped SQL queries with 2 variables -
i have table (tags) 3 fields, activityid, placeid, tagid (and 4th, id pkey). want search through table using 2 arrays, 1 of places , 1 of tags. each match want return activityid. want used activityid list in table (activity) each of same array of placeids. started put loops see lots of things saying not that. thinking need use temp table might not required. i'm struggling loops anyway rather struggle make poor practice anyway thought i'd post general idea see if point me in right direction... code not working shows general idea..edit... i'm looking resolve loop in first section, second section need leave loop
$places = array("london","madrid","paris","rome"); $tags = array("shopping","sight","bar","club"); $num_places = count($places); $num_tags = count($tags); /* want remove loop section */ $counterp = 0; while($counterp <= ($num_places)) { $countert = 0; while($countert <= ($num_tags)) { $conn->query('insert temp (activityid) select activityid, placeid tags placeid = "'.$place[$counterp].'" , tagid = "'.$tag[$countert].'"'); $countert++; } $counterp++; } /* section stay in loop */ $counterp = 0; while($counterp <= ($num_places)) { $sql_interests = 'select a.summary, a.image, a.link activity left join temp t on t.activityid = a.activityid a.placeid = "'.$place[$counterp].'"'; $interests = array(); $interests_result = $conn->query($sql_interests); if ( !empty($interests_result)) { while($interests_row = $interests_result->fetch_assoc()) { $interests[] = array($interests_row["summary"],$intersts_row["image"],$interests_row["link"]); } /* stuff data */ } $counterp++; }
mysql approach. clause filters tags in clause, , join gets activity table. a
, t
aliases either easier reading or lazy (like me)
select a.* activity join tags t on t.activityid=a.activityid t.tagid in ('sightseeing','parks') , t.placeid in ('istanbul','paris'); +----+------------+---------+---------------------------+ | id | activityid | placeid | summary | +----+------------+---------+---------------------------+ | 4 | 444 | paris | see arc d'triumph | | 6 | 666 | paris | see eifel tower | | 8 | 888 | paris | walk through central park | +----+------------+---------+---------------------------+ 3 rows in set (0.01 sec)
Comments
Post a Comment