我有一个使用Twitter API存储Twitter帖子的数据库 - 在标题id(存储为tag:search.twitter.com,2005:195795633834176512(,作者,时间,文本 - 我有一个页面打印出该数据库中最近7天的帖子。有时可能是 10-20 个帖子的最后 7 个,有时可能是 100-200 个。我一直在尝试将新闻自动收报机集成到页面中,因此每个帖子都出现在一个框中,然后淡出以替换为另一个帖子,这意味着您可以迭代一个迭代的不是 200 行:
<div id="news1">
<h3>Stackoverflow</h3>
<!-- Displaying News Module -->
<div class="news-container" style="overflow-x: hidden; overflow-y: hidden; position: relative; height: 1207px; ">
<!-- ul id="news-scroller" -->
<ul style="left: 207px; position: absolute; margin-top: 0px; margin-right: 0px; margin- bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; top: 0px; ">
<li style="display: list-item; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; height: 69px; ">
<div style="padding-bottom: 5px;">
<!-- Displaying News Module -->
<?php
require_once 'db-functions.inc.php' ; //custom database functions
echo "<ul class='"twitter_feed'">'n" ;
$result = dbQuery("SELECT * FROM `HASH` WHERE text LIKE '%stackoverflow%' AND `hidden` != 'y' AND from_unixtime(time) > date_sub(curdate(), interval 7 day) ORDER BY `time` DESC LIMIT 10");
while ($row = dbGetRow($result)) {
$text = stripslashes($row['text']);
$time = $row['time'];
$author = stripslashes($row['author']);
echo " <li><span class='"twitter_time'">".date('M j, Y, g:i a',$time)."</span> · $author: $text</li>'n" ;
}
echo "</ul>'n" ;
?>
但是,这只是将过去 7 天作为一个整体输出,而不是一次输出一个:http://sanctuary-westminster.org/server/scroll.php
问题是如何单独输出最后一个条目(时间戳最大的条目(作为"新闻项目",即最后一个帖子、倒数第二个帖子等。它不会是一个固定数量的条目,一个 7 天可能是 10 天,另一个是 200 天,所以我发现编写回显和迭代很棘手。
提前谢谢你
我觉得最好的方法是使用 AJAX,如评论中所述,但最简单的方法是使用您已经编写的代码,将帖子数组从数据库转储到 javascript 数组中,然后使用您的新闻自动收报机循环浏览 javascript 数组。您可以像这样操作:
echo '<script type="text/javascript"> var tweets = [""];
var author = [""];
var times = [""]; ';
while ($row = dbGetRow($result))
{
echo "tweets.push('$row[text]');
author.push('$row[author]');
times.push('$row[times];";
}
echo '</script>';
然后,您可以使用JavaScript循环访问数组,以一次在新闻自动收报机中显示其信息。
编辑:我修复了最初的 var 分配,如下面的评论所示。