Пример #1
0
 /**
  * Method to generate list of friends up to _maximumCircleRadius'th order (_maximumCircleRadius'th circle) for each user,
  * and save it to the database
  * 
  * @param	MySQLi			Database handler
  * 
  * @return  null
  */
 public static function prepareFriendList($db)
 {
     //read list of connections from the DB
     $result = $db->query("SELECT id, connect_from, connect_to FROM connections ORDER BY connect_from ASC");
     while ($row = $result->fetch_object()) {
         $connections[] = $row;
     }
     //start: prepare list of friends of the 1st order (1st circle)
     $friends = array();
     foreach ($connections as $c) {
         $friends[1][$c->connect_from][] = $c->connect_to;
     }
     SixDegrees::_prepareCircles($friends);
     //end: prepare list of friends of the 1st order (1st circle)
     //start: for each user, convert array of friends of different orders into a list
     foreach ($friends as $circle => $users) {
         foreach ($users as $id_user => $f) {
             $f = array_unique($f);
             sort($f);
             $friends[$circle][$id_user] = implode($f, ',');
         }
     }
     //end: for each user, convert array of friends of different orders into a list
     foreach ($friends[1] as $id_user => $v) {
         //for each user, save their circles to the database
         if (!empty($v)) {
             $t = $db->prepare("INSERT INTO friends SET id_user = '******',\n\t\t\tfriends1 = '" . $friends[1][$id_user] . "', \n\t\t\tfriends2 = '" . $friends[2][$id_user] . "',\n\t\t\tfriends3 = '" . $friends[3][$id_user] . "',\n\t\t\tfriends4 = '" . $friends[4][$id_user] . "' \n\t\t\tON DUPLICATE KEY UPDATE \n\t\t\tfriends1 = '" . $friends[1][$id_user] . "', \n\t\t\tfriends2 = '" . $friends[2][$id_user] . "',\n\t\t\tfriends3 = '" . $friends[3][$id_user] . "',\n\t\t\tfriends4 = '" . $friends[4][$id_user] . "' \n\t\t\t");
             $t->execute();
         }
     }
 }