/** * Public method to generate the associative array with a chain from user with ID $id_from to user with ID $id_to * * @param integer ID of the user from whom we want to build the chain * @param integer ID of the user to whom we want to build the chain * @param MySQLi Database handler * * @return array array which contains the chain (IDs, names, and other data) */ static function buildConnection($id_from, $id_to, $db) { $result = array(); $r = $db->query("SELECT * FROM friends WHERE id_user = '******'"); $ufrom = $r->fetch_object(); $r = $db->query("SELECT * FROM friends WHERE id_user = '******'"); $uto = $r->fetch_object(); //start: find possible intersection of circles of the two users $connections = new self(); $result['path'] = $connections->_findIntersection($db, $ufrom, $uto, 1); $result['brokenLink'] = $connections->_brokenLink; //if true, $ufrom and $uto are not connected $result['chainLength'] = $connections->_chainLength; //length of the chain //end: find possible intersection of circles of the two users if (!empty($result['path'])) { //if there's a chain exist, find out the names $path = implode(',', $result['path']); $r = $db->query("SELECT id, name FROM users WHERE id IN ({$path})"); $names = array(); while ($row = $r->fetch_object()) { $names[$row->id] = $row->name; } $result['pathNames'] = $names; //the names of users in the chain } return $result; }