douweng3383 2013-03-30 12:03
浏览 48

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

I am creating the web application of twitter #hashtag search where I want to integrating the following code.

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

(Note : Just copy/paste above code for getting demo)

The above link contains the code for getting the number of tweets for any search (Useful for hashtag trending).

And following is my code:

<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>

Here I am using tweetCount script for this :

                <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>

In the tweetCount script they are using form and searching for one keyword after submitting the form and getting the tweetcount result from http://search.twitter.com/search.json?q=

But I want to call that on page load thats why I am calling the submitTerms() function for getting the real time tweet count. Because of I am calling script in while loop I cannot use "id" for totalTweet, loading and pagesDone div tag. I tried by adding "class" there but it's resulting same tweet count finally for all hashtag which is not correct.

Hope you get this. Need Help.

  • 写回答

1条回答 默认 最新

  • doumei1908 2013-03-31 00:28
    关注

    I recommend you to fork the total_tweets.html ans do some modifications to the JS code. This way you can pass a param to specify the #id div you want to target.

    // 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**);
    }
    

    Then in your php file you would call it:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害