简体   繁体   English

从PHP while循环中调用JS函数以获取Twitter主题标签计数

[英]Calling JS Function from PHP while loop for getting twitter hashtag count

Following is my PHP code : 以下是我的PHP代码:

<tr  <?php echo $backclr; ?>>                            
    <td class="hashtd"><a href="#"><?php echo "#" . $row['hashtag']; ?></a></td>
    <td class="hashtd">                                    
        <script type = "text/javascript">
            submitTerms("<?php echo $row['hashtag']; ?>", "1", "2");
        </script>
        <div id="totalTweets<?php echo $row['hashtag']; ?>"><span></span></div>
        <div class="loading"><img src="img/preloader.gif"/></div>                                    
    </td>
</tr>

<?php
endwhile; ?>

Here I am calling javascript function submitTerms(); 在这里,我正在调用javascript函数submitTerms(); and passing the php argument in that function. 并在该函数中传递php参数。

Following is the javascript code: 以下是JavaScript代码:

<script type="text/javascript">

            var getPage = new RegExp( /page=([^&]+)/ );
            var url = "http://search.twitter.com/search.json?q=";
            var search, page, pageTotal, tweets, loading;
            var beforeCounter = "<b>";
            var afterCounter = "</b>";
            var totalTweets = 0;

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

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

            function getData(data) {
                tweets = data.results;
                totalTweets += tweets.length;
                nextPage = getPage.exec(data.next_page);
                if( nextPage == null ) {
                    showTotal();
                    return;
                }
                nextPage = nextPage[1];
                getTweets(search, nextPage, pageTotal);
            }

            function submitTerms(query, pg, pgtot) {

                $('#totalTweets'+query+' span').html('');

                search = query;
                page = pg;
                pageTotal = pgtot;

                if( search == '' ) {
                    alert('No search query found');
                    return;
                }
                loading = true;                
                getTweets(search, page, pageTotal);
            }

            function status() {
                if( loading ) $('.loading').show();
                else $('.loading').hide();
            }

            $(function() {
                loading = false;
                setInterval(status, 10);
            });
        </script>

The above the javascript is returning the twitter hashtag count. 上面的javascript返回了Twitter的hashtag计数。 However I want hashcount for each row in <td><div id="totalTweets<?php echo $row['hashtag']; ?>"></td> . 但是我想要<td><div id="totalTweets<?php echo $row['hashtag']; ?>"></td>每一行的<td><div id="totalTweets<?php echo $row['hashtag']; ?>"></td> Also I am using the different id's here in div tag for getting it. 我也在div标签中使用不同的ID来获取它。

The issue is its displaying the count only for last row not for all row. 问题是它仅显示最后一行的计数,而不显示所有行的计数。 Example : If there are 5 records in database then its showing hashtag count only for last row. 示例:如果数据库中有5条记录,则其仅显示最后一行的主题标签计数。 When I debugged the code then its showing count for all hashtag but the final rendering is only ln last row not for all row. 当我调试代码时,它的显示标记为所有主题标签,但最终渲染仅在最后一行而不是所有行中进行。

Where is the issue ? 问题出在哪里? Need Help. 需要帮忙。

You are calling 你在打电话

submitTerms("<?php echo $row['hashtag']; ?>", "1", "2");

which translates to 转化为

submitTerms("movies", "1", "2");

(movies is an example ofcourse) (电影是课程的一个例子)

The javascript normalizes this call to: getTweets("movies", 1, 2) where 1 is the page number and 2 is the callback method. javascript将此调用标准化为: getTweets("movies", 1, 2) ,其中1是页码,2是回调方法。 This is where your problem seems to be. 这就是您的问题所在。 You have not defined a callback method called '2'. 您尚未定义名为“ 2”的回调方法。

If you leave this out, your returned JSON will contain an array key called 'results' which is itself an array containing your resultset. 如果不考虑这一点,返回的JSON将包含一个名为“结果”的数组键,它本身就是一个包含结果集的数组。 You can access this resultset by properly defining your $.getJSON method. 您可以通过正确定义$.getJSON方法来访问此结果集。 The second parameter is the data that is SEND to the server, not the data that comes back from the server, this is stored in the third parameter, see: http://api.jquery.com/jQuery.getJSON/ 第二个参数是发送到服务器的数据,而不是从服务器返回的数据,它存储在第三个参数中,请参阅: http : //api.jquery.com/jQuery.getJSON/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM