在循环中获取PHP中的#hashtag推文计数

时间:2013-03-30 12:03:20

标签: php javascript jquery hashtag

我正在创建twitter #hashtag搜索的web应用程序,我希望集成以下代码。

https://github.com/henrahmagix/tweetCount/blob/master/total_tweets.html

(注意:只需复制/粘贴上面的代码即可获得演示)

以上链接包含获取任何搜索推文数量的代码(对于主题标签趋势非常有用)。

以下是我的代码:

<table width="100%">
        <tr>
            <th>#Hashtag</th>
            <th>Description</th>
            <th>Tags</th>           
            <th>Participants</th>
        </tr>

        <?php
        $i = 1;
        $j = 1;
        while ($row = mysqli_fetch_array($result)):            
            ?>

            <tr>                            
                <td class="hashtd"><a href="#"><?php echo "#" . $row['hashtag']; ?></a></td>
                <td class="hashtd"><?php echo $row['description']; ?></td>
                <td class="hashtd"><a href="<?php echo $row['id']; ?>" role="button" data-toggle="modal" class="show_modal_box">Show</a></td>
                <td class="hashtd">                                    
                    <script type="text/javascript">
                        submitTerms("<?php echo $row['hashtag']; ?>","1","0");
                    </script>
                    <div id="totalTweets">Total tweets: <span></span></div>
                    <div id="loading">Loading!</div>
                    <div id="pagesDone">Pages done: <span></span></div>
                </td>
            </tr>

            <?php
            $i++;
            $j++;
        endwhile;
    ?>
</table>

我在这里使用tweetCount脚本:

                <td class="hashtd">                                    
                    <script type="text/javascript">
                        submitTerms("<?php echo $row['hashtag']; ?>","1","0");
                    </script>
                    <div id="totalTweets">Total tweets: <span></span></div>
                    <div id="loading">Loading!</div>
                    <div id="pagesDone">Pages done: <span></span></div>
                </td>

在tweetCount脚本中,他们使用表单并在提交表单并从http://search.twitter.com/search.json?q=获取tweetcount结果后搜索一个关键字

但是我想在页面加载时调用它,这就是为什么我调用submitTerms()函数来获取实时推文数。因为我在while循环中调用脚本,所以我不能使用“id”来表示totalTweet,loading和pagesDone div标签。我尝试在那里添加“class”,但最后对于所有不正确的hashtag都会产生相同的推文数。

希望你明白这一点。需要帮忙。

1 个答案:

答案 0 :(得分:1)

我建议你分叉total_tweets.html并对JS代码做一些修改。这样您就可以传递一个参数来指定您想要定位的#id div。

// total_tweets function
function getTweets(search, page, pageTotal, **hashtag**) {
  $.getJSON( url + search + '&page=' + page + '&callback=?',
    function(data) {
      if( data.results.length != 0 && page != pageTotal ) {
        $('#pagesDone'** + hashtag + **' span').html(page);
        getData(data);
      }
      else {
        showTotal(**hashtag**);
      }
    }
  );
}

function showTotal(**hashtag**) {
  $('#totalTweets'** + hashtag + **' span').html(beforeCounter + totalTweets + afterCounter);
  $('#pagesDone'** + hashtag + **' span').html('0');
  totalTweets = 0;
  loading = false;
}

function submitTerms(**hashtag**) {
  $('#totalTweets'** + hashtag + **' span').html('');
  $('#pagesDone'** + hashtag + **' span').html('0');
  search = encodeURIComponent($('#query').prop('value'));
  page = $('#startPage').prop('value');
  pageTotal = $('#pageTotal').prop('value');
  if( search == '' ) {
    alert('Please enter search query');
    return;
  }
  if( page == 0 ) {
    alert('0 not allowed as start page');
    return;
  }
  loading = true;
  getTweets(search, page, pageTotal, **hashtag**);
}

然后在你的php文件中你会称之为:

<td class="hashtd">                                    
    <script type="text/javascript">
        **submitTerms("<?php echo $row['hashtag']; ?>");**
    </script>
    <div id="totalTweets<?php echo $row['hashtag'];">Total tweets: <span></span></div>
    <div id="loading<?php echo $row['hashtag'];">Loading!</div>
    <div id="pagesDone<?php echo $row['hashtag'];">Pages done: <span></span></div>
</td>