Database:
- xx_users (id, name, user_id, email, location)
- xx_questions (id, question, description, user_id)
Code:
$friends = $facebook->api('/me/friends');
$arr= $friends['data'];
$friend_ids_arr = array();
foreach($arr as $friend) {
$friend_ids_arr[] = $friend['id'];
}
$sql = "SELECT (SELECT * FROM xx_questions WHERE user_id IN (" . implode(',', $friend_ids_arr) . ") OR user_id = '$user' ORDER BY time DESC) (SELECT * from questions INNER JOIN users ON users.user_id = questions.user_id WHERE users.location = 'Cambridge, Cambridgeshire')";
$data = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($data)) {
// echo values from what's been selected
}
I am trying to select questions from xx_questions (that have been posted by users) should any of three situations arise:
- Those values where the poster’s user_id matches that of one of the current user’s friends (i.e. the poster and the user are friends)
- Those values where the poster’s user_id matches that of the user (i.e. the poster is the user)
- Those values where the poster’s location matches that of the current user (i.e. the poster and user are currently in the same city)
I have managed to make 1 and 2 work, but the problems arise when I try to introduce 3, so my two questions are:
- What is the correct MySQL for 3?
- How is that integrated with 1 and 2?
Thanks in advance, let me know if you need more information.
To get the information that you want, you need to join xx_questions to xx_users. Something like the following returns questions that match any one of the three conditions:
I have written this more like SQL than as a string, so it is easier to read.