I'm trying to check for users that are online in a selected city.... but the problem is that I would have to run multiple queries to get the information needed. here is how my queries look so far just to get what I need.
I'm pulling from the following tables:
- location_town (towns are sub-neighborhoods within the cities)
- location_cities
- sessions
/// First Query to get the CITY ID ///
$getmycity_query = mysql_query("SELECT city_id FROM location_town WERE id = '".$_SESSION['userinfo']['town']."'");
$getmycity_results = mysql_fetch_assoc($getmycity_query);
Ok, now that I know the city_id ... I can check the town table again to find out which towns have that city ID
/// Second Query to get a list of all towns within the selected city ///
$gettowns_query = mysql_query("SELECT id FROM location_town WHERE city_id = '".$getmycity_results['city_id']."'");
/// Now I can store each town's ID into an array ///
$townArray = array();
while($gettowns_results = mysql_fetch_assoc($gettowns_query)){
array_push($townArray, $gettowns_results['id']);
}
/// Now I implode the list into something SQL will understand ///
$townIDs = implode(" ) OR ( `town_id` =", $townArray);
$townIDs = "( `town_id` =".$townIDs." )";
Now I have created the string $townIDs which looks something like ( town_id =5 ) OR ( town_id =10 ) OR ( town_id =349 )
I will use that string as part of my query statment when looking for town_id's in the session table
/// Third Query is to find all members with that Town ID in the sessions table ///
$find_query = mysql_query("SELECT uID FROM sessions WHERE ".$townIDs);
And that's it.... once the 3rd query is done... I know which users are online for a certain city.
But this doesn't seem like the most efficient way to do this.. anyone have suggestions?