uid; $user_rel = db_query(" SELECT u.name, u.uid, u.picture FROM {users} AS u LEFT JOIN {user_relationships} AS ur ON (u.uid = ur.requester_id OR u.uid = ur.requestee_id) WHERE (u.uid = ur.requester_id OR u.uid = ur.requestee_id) AND (ur.requester_id = $cur_user OR ur.requestee_id = $cur_user) AND (ur.approved = 1) AND (ur.rtid = 2) GROUP BY (u.name) ORDER BY COUNT(ur.requester_id OR ur.requestee_id) DESC "); //rtid is the relationship ID, in my case it represents "Friends" if ( db_num_rows($user_rel) ) { //skip the rest if the user has no friends $user_rel_com = array(); //assigns the actual values into an array while($value = db_fetch_array($user_rel)) { $user_rel_com[] = $value; } foreach($user_rel_com as $row){ //writes the sub-query for $user_rel_rel $req_req_id_list .= " OR ur.requester_id = " . $row['uid'] . " OR ur.requestee_id = " . $row['uid']; $no_include_friends .= " AND u.uid != ". $row['uid']; } $len = strlen($req_req_id_list); $req_req_id_list = substr($req_req_id_list, 4, $len); //trims the first " OR " $len = strlen($no_include_friends); $$no_include_friends = substr($no_include_friends, 5, $len); //trims the first " AND " //gets all of the current user's friends' friends and orders by how many times they show up in the list $user_rel_rel = db_query(" SELECT u.uid, u.name, u.picture FROM {users} AS u LEFT JOIN {user_relationships} AS ur ON (u.uid = ur.requester_id OR u.uid = ur.requestee_id) WHERE (u.uid = ur.requester_id OR u.uid = ur.requestee_id) AND ($req_req_id_list) AND (ur.approved = 1) AND (ur.rtid = 2) AND u.uid != %d GROUP BY (u.name) ORDER BY COUNT(ur.requester_id OR ur.requestee_id) DESC ", $cur_user); $user_rel_rel_com = array(); //assigns the actual values into an array while($value = db_fetch_array($user_rel_rel)) { $user_rel_rel_com[] = $value; } //array_diff() wasn't working so I wrote my own; this just removes users who the current user already has a relationship with $remaining = array(); foreach($user_rel_rel_com as $value){ if ( !array_search($value,$user_rel_com) ) { $remaining[] = $value; } } //This section re-orders the users non-randomly. It moves users up and down the list slightly so the block doesn't show the same people every time. $z = $remaining; $i = 0; $a = 1; while ($i < count($remaining) - 1) { $b = $a; $a = rand(0, 1); $j = $i + 1; if (!$a && $b) { $z[$i] = $remaining[$j]; $z[$j] = $remaining[$i]; } $i++; } $remaining = $z; $i = 0; echo "" . l('More', 'node/X'); //The . l('More', 'node/X') is optional. Use it if you have a page that displays more results than show up in your block, and replace 'X' with the NID of that page. } ?>