找到也发推文的朋友

时间:2013-01-10 16:02:32

标签: php smarty

我正在开发一个像twitter这样的社交工具,但我还是坚持“谁也推特推特。”

我的页面上有一个twitters列表,我想显示谁也为每条推文发推文。

我正在使用MySQL + php + Smarty。

这是代码: / * ---- php ---- * /

// Get twitters that tweeted over 200 times.
$sql = mysql_query("SELECT id, content, user, tweets FROM twitter_list WHERE tweets > '200'");
$twitter_array = array();
while($tweet_row = mysql_fetch_array($sql)){
    array_push($twitter_array, $tweet_row);
    // Get the users who tweeted these twitters.
    $twitter_id = $tweet_row['id'];
    // Find out 5 friends who also tweeted this twitter. twitter_relation stores who tweets what.
    $friends_who_also_tweet = mysql_query("SELECT tid, twitter_id, user_id FROM tweet_relation WHERE twitter_id = '$twitter_id' ORDER BY tid DESC LIMIT 5");
    $friends_who_also_tweet_array = array();
    while($friends_who_also_tweet_row = mysql_fetch_array($friends_also_tweet_array)){
        array_push($friends_who_also_tweet_array, $friends_who_also_tweet_row);
    }
    if($friends_who_also_tweet_array){
        $sm->assign("fwat", $friends_who_also_tweet_array);
    }
}
if($twitter_array){
    $sm->assign("twitter", $twitter_array);
}
$smarty->display('twt.html');

/ * ---- HTML:twt.html ---- * /

...
{section name=twitter loop=$twitter}
<div class="content">{$twitter[twitter].content}</div>
<div class="who_also_tweet">
  {section name=who loop=$fwat}
  <div class="i_also_tweet">{$fwat[who].user_id}</div>
  {/section}
</div>
{/section}

我希望得到一系列推特,并且对于每个推特,检索推文的5位用户。但它只显示第一个twitter的用户,我怀疑虽然()可能是错的,但无法找到它。有人可以帮我一把吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

$twitter_array = array();

然后:

if($twitter_array){
    $sm->assign("twitter", $twitter_array);
}

您从未向该数组添加任何内容。我假设你的意思是在这里(因为永远不会声明$tweet_array):

array_push($tweet_array, $tweet_row);

事实上,这个变量也被分配,然后从不被引用:

$friends_who_also_tweet

您的变量命名确实令人困惑,我认为这可能会导致您的问题。

编辑:另外,为什么要在第一个While循环的每次迭代中分配它?它只是要覆盖以前的值:

if($friends_who_also_tweet_array){
        $sm->assign("fwat", $friends_who_also_tweet_array);
    }
相关问题