Frage

Ich frage mich, ob dies der beste Weg ist, um dieses Problem zu lösen. Ich verschmelzenden ein Facebook-Nutzer Freunde Daten (von Facebook - gibt eine Multi-Array) mit den Stimmen von den Benutzern in dieser Liste, die (von MySQL) gewählt.

Dies ist, wie ich dies erreicht. Ich bin ein Junior-Entwickler und auf der Suche nach Hilfe macht meinen Code so weit wie möglich optimiert werden.

public function getFriendVotes(){
    global $facebook;

    // Get The users friends that use this app from facebook
    $friends = $facebook->api_client->fql_query(
      "SELECT uid, first_name, last_name
        FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user)"
    );

    // Create an array of just the ids
    foreach($friends as $friend){
      $userids[] = $friend['uid'];
    }

    // Create a string of these ids
    $idstring = implode(",", $userids);

    // Get the votes from only the users in that list that voted
    $result = $this->db->query(
      "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"
    );

    // Create a new result set (multi array).  Include the data from the first
    // Facebook query, but include only those who voted and append their votes
    // to the data
    $row = $result->fetch_assoc();
    foreach($friends as $friend){
      if($row['userid'] == $friend['uid']){
        $return[$count] = $friend;
        $return[$count]['vote'] = $row['vote'];
        $row = $result->fetch_assoc();
        $count++;
      }
    }
    return $return;
}
War es hilfreich?

Lösung

Ich gehe davon aus, dass fql_query tut Unterstützung mysql Syntax und es wäre effizienter zu sein, LEFT statt creatig zusätzliche Abfrage JOIN zu verwenden, hier ist meine Version des Codes:

public function getFriendVotes(){
    global $facebook;

    // Get The users friends that use this app from facebook
    $friends = $facebook->api_client->fql_query("
        SELECT DISTINCT u.uid,u.first_name,u.last_name 
        FROM user AS u 
        LEFT JOIN friend AS f ON uid=uid2 
        WHERE f.uid1='{$this->user}'
    ");
    $arrayUsers = array();
    // Create an array of just the ids
    foreach($friends as $v){
        $arrayUsers[$friend['uid']] = $v;
    }
    unset($friends);

    // Create a string of these ids
    $idstring = implode(",", array_keys($arrayUsers));

    // Get the votes from only the users in that list that voted
    $result = $this->db->query(
      "SELECT vote, userid FROM user_votes WHERE userid IN ({$idstring})"
    );

    $result = array();
    // Create a new result set (multi array).  Include the data from the first
    // Facebook query, but include only those who voted and append their votes
    // to the data
    while($v = $result->fetch_assoc())
    {
        if(isset($arrayUsers[$v['userid']])
        {

            $arrayUsers[$v['userid']] = $v['vote'];

            $result[] = $arrayUsers[$v['userid']];

            unset($arrayUsers[$v['userid']], $v);
        }
    }

    return $return;
}

Andere Tipps

Ich kann Ihnen nicht sagen, wie Sie den Code ohne Messung und Prüfung durchführen würde. Ich würde mit Ihrem Code für andere Fragen suchen, die es ein bisschen besser lesbar / maintanable machen würden. Zum Beispiel:

Erstellen kleine Methoden.

Im Hauptverfahren, sehe ich einige Teile des Codes, die gut kommentiert sind. Warum nicht eine Methode stattdessen einen großen Kommentar im Hauptverfahren zu machen?

Zum Beispiel:

// Get The users friends that use this app from facebook
$friends = $facebook->api_client->fql_query(
  "SELECT uid, first_name, last_name
    FROM user
    WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user"
);
return $friends;

Würde ein interessante

functin get_users_friends_from_facebook($facebook){
    // Get The users friends that use this app from facebook
    $friends = $facebook->api_client->fql_query(
      "SELECT uid, first_name, last_name
        FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user"
    );
    return $friends;
}

In der gleichen Weise,

// Get the votes from only the users in that list that voted
$result = $this->db->query(
  "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"
);

Ist ein guter Kandidat zu

function get_votes_from_voters(){
    // Get the votes from only the users in that list that voted
    $votes = $this->db->query(
      "SELECT vote, userid FROM user_votes WHERE userid IN ($idstring)"
    );
}

Geben Sie Variablen sinnvolle Namen auf den Kontext.

$return ist kein guter Name. Warum Sie es nicht zum Beispiel nennen $users_votes?

Versuchen Sie, die Namenskonvention Ihrer plataform zu halten.

Überprüfen Sie die apis Sie verwenden. Sind sie mit Camelcase? Sind sie mit Unterstrichen? Versuchen Sie, mit Ihren Bibliotheken und Konventionen plataform zu halten. Überprüfen Sie dieses Thema für eine gute Referenz.

Und begrüßen zu SO. Der Code ist in Ordnung. Versuchen Sie, einige OO Prinzipien zu lesen, könnten Sie noch mehr Zeilen Code schneiden. Alle einfachen Ratschläge Ich schrieb hier sind verfügbar in einem großen Buch namens Code Complete .

Ich habe Punkte aus all Ihren Kommentaren und neu geschrieben, wie unten diese Methode. Danke für all die großen Eingang.

public function getAppUserFriends(){
    global $facebook;
    return $facebook->api_client->fql_query(
        "SELECT uid, first_name, last_name
        FROM user
        WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user)
        AND is_app_user;"
    );
}

public function getFriendVotes(){

    // Get the users friends that use this app
    $friends = $this->getAppUserFriends();

    // Create an array with the ids as the key
    foreach($friends as $v){
        $arrayFriends[$v['uid']] = $v;
    }

    // Create a string of these ids
    $idString = implode(",", array_keys($arrayFriends));

    // Get the votes from only the users in that list that voted
    $result = $this->db->query(
        "SELECT vote, userid
        FROM user_votes
        WHERE pollid=$this->poll
        AND userid IN ($idString)"
    );

    // Pluck out user data from facebook array where the user has voted
    // and add the vote to that array
    while($row = $result->fetch_assoc()){
        $friendsVotes[$row['userid']] = $arrayFriends[$row['userid']];
        $friendsVotes[$row['userid']]['vote'] = $row['vote'];
    }
    return $friendsVotes;
}

Haben Sie Performance Probleme bei dieser Methode? Denn es sei denn, Sie sind, gibt es keine Notwendigkeit, es zu optimieren.

-Code zuerst, Profil den Code, und dann optimieren, wo sie tut das gut.

$friends = $facebook->api_client->fql_query(
  "SELECT uid, first_name, last_name
    FROM user
    WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user"
);

wahrscheinlich verkürzt werden könnte

$userids = $facebook->api_client->fql_query(
  "SELECT uid
    FROM user
    WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$this->user)"
);

, weil die uid das einzige, was Sie scheinen von fb werden mit

Es war ein wenig schwer für mich zu sagen, was Sie zu tun versuchen, aber Sie könnten überlegen in PHP array_intersect (und seine Cousins) suchen.

A = {1:'fred', 2:'bob'}
B = {1: 2, 3: 0}

C = array_intersect( array_keys(A), array_keys(B) )
D = {}
foreach (C as c) {
  D[c] = (A[c], B[c])
}

Die Syntax off es ist, aber ich hoffe, es führt Sie in die richtige Richtung.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top