合并来自两个不同数据集的数据(Facebook 和 MySQL)
我想知道这是否是解决这个问题的最佳方法。我正在将 Facebook 用户的朋友数据(来自 facebook - 返回一个多数组)与该列表中投票的用户的投票(来自 MySQL)合并。
这就是我完成此任务的方法。我是一名初级开发人员,正在寻求帮助以尽可能优化我的代码。
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;
}
I'm wondering if this is the best way to tackle this issue. I am merging a Facebook users friends data, (from facebook - returns a multi array) with the votes from the users in that list that voted (from MySQL).
This is how I accomplished this. I'm a junior developer and looking for help on making my code as optimized as possible.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为 fql_query 确实支持 mysql 语法,并且使用 LEFT JOIN 而不是创建额外查询会更有效,这是我的代码版本:
I asume that fql_query does support mysql syntax and it would be more efficient to use LEFT JOIN instead creatig extra query, here is my version of your code:
如果不进行测量和测试,我无法告诉您代码的执行情况。我会寻找您的代码的其他问题,这将使其更具可读性/可维护性。例如:
创建更小的方法。
在 main 方法中,我看到一些注释良好的代码块。为什么不创建一个方法而不是在 main 方法中进行大量注释呢?
例如:
变得有趣
会以同样的方式
良好候选者
, 是为上下文赋予变量有意义的名称的
。
$return
不是一个好名字。例如,为什么不将其命名为$users_votes
呢?尽量保持平台的命名约定。
检查您正在使用的 api。他们使用驼峰命名法吗?他们使用下划线吗?尽量遵守您的库和平台约定。查看此主题以获得良好的参考。
欢迎来到SO。你的代码没问题。尝试阅读一些面向对象的原则,你甚至可以减少更多的代码行。我在这里写的所有简单建议都可以在一本名为 Code Complete 的好书中找到。
I can't tell you how your code would perform without measuring and testing. I would look for other issues with your code, that would make it a bit more readable/maintanable. For example:
Create smaller methods.
Inside the main method , I see some chunks of code that are well commented. Why not create a method instead of making a huge comment in the main method?
For example:
Would make an interesting
In the same manner,
Is a good candidate to
Give variables meaningful names to the context.
$return
isn't a good name. Why don't you name it$users_votes
for example?Try to keep the naming convention of your plataform.
Check the apis you're using. Are they using camelCase? Are they using underscores? Try to keep with your libraries and plataform conventions. Check this topic for a good reference.
And welcome to SO. Your code is fine. Try to read some OO principles, you could even cut more lines of your code. All the simple advices I wrote here are avaiable in a great book named Code Complete.
我从你的所有评论中吸取了要点,并重写了这个方法,如下所示。感谢您提供的所有宝贵意见。
I took points from all your comments and rewrote this method as below. Thanks for all the great input.
您在使用这种方法时遇到性能问题吗?因为除非你是,否则没有必要优化它。
首先编码,分析代码,然后优化最有效的地方。
Are you having performance troubles in this method? Because unless you are, there's no need to optimize it.
Code first, profile the code, and then optimize where it does the most good.
可能可以缩短为,
因为 uid 是您似乎从 fb 使用的唯一内容
could probably be shortened to
because the uid is the only thing you seem to be using from fb
我有点难以说出您想要做什么,但您可以考虑查看 PHP 的
array_intersect
(及其表兄弟)。语法已经关闭,但我希望它能引导您走向正确的方向。
It was a little hard for me to tell what you are trying to do, but you might consider looking at PHP's
array_intersect
(and its cousins).The syntax is off there but I hope it leads you in the right direction.