我正在尝试使用ajax(在该区域非常新手)和/或jquery用我的mysql查询结果刷新div。我的index.php看起来像这样:
<head>
$( document ).ready(function() {
(function showscreen() {
$.ajax({
url: 'screen.php',
success: function(data) {
$('#screen').html(data);
},
complete: function() {
setTimeout(showscreen, 5000);
}
});
})();
});
</head>
<body>
<div id="screen"></div>
<?php
require '../dbauth/videos.inc.php';
//This handles my db connections.
$videosdbhandle = mysql_connect(
$videoshostname,
$videosusername,
$videospassword
) or die("INVALID USERNAME OR PASSWORD");
$videosselected = mysql_select_db($videosdbname, $videosdbhandle);
$checkvideosql = "SELECT * FROM videos ORDER BY votes DESC";
$checkvideoquery = mysql_query($checkvideosql, $videosdbhandle);
$checkvideocount = mysql_num_rows($checkvideoquery);
if ($checkvideocount > 0) {
$count = 0;
while ($checkvideocount > $count) {
$thevideoname = mysql_result($checkvideoquery, $count, "name");
$thevideovideo = mysql_result($checkvideoquery, $count, "video");
$thevideoid = mysql_result($checkvideoquery, $count, "id");
$thevideoimage = mysql_result($checkvideoquery, $count, "image");
echo '<div class="screen" id="video';
echo $thevideoid;
echo '" style="display: none; background-image: url(';
echo "'http://backcurrents.com/videos/";
echo $thevideoname;
echo '/';
echo $thevideoimage;
echo "'";
echo ');">';
echo '<video loop muted autoplay
poster="http://backcurrents.com/videos/';
echo $thevideoname;
echo '/';
echo $thevideovideo;
echo '" class="videobg">
<source src="http://backcurrents.com/videos/';
echo $thevideoname;
echo '/';
echo $thevideovideo;
echo '" type="video/mp4">
</video>';
echo '</div>';
$count++;
}
}else{
echo 'NO VIDEOS!';
}
?>
</body>
然后我的screen.php看起来像这样:
$videosusername = "theusername";
$videospassword = "thepassword";
$videoshostname = "localhost";
$videosdbname = "thedbname";
$videosdbhandle = mysql_connect(
$videoshostname,
$videosusername,
$videospassword
) or die("INVALID SHOW NAME OR PASSWORD");
$videosselected = mysql_select_db($videosdbname, $videosdbhandle);
$checkhighestvideosql = "SELECT * FROM videos ORDER BY votes DESC";
$checkhighestvideoquery = mysql_query($checkhighestvideosql,
$videosdbhandle);
$checkhighestvideocount = mysql_num_rows($checkhighestvideoquery);
if ($checkhighestvideocount > 0) {
$x=0;
while ($checkhighestvideocount > $x) {
$checkhighestvideoid = mysql_result($checkhighestvideoquery, $x, "id");
if ($x=0) {
echo '<script type="text/javascript">';
echo '$(".screen").hide();
$("#video';
echo $checkhighestvideoid;
echo '").show(); ';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo '$(".screen").hide();
';
echo '</script>';
}
$x++;
}
}else{
echo "NO VIDEOS!";
}
我想要做的就是每隔5秒刷新一次mysql查询,但是屏幕中没有任何内容出现在screen.php中。我已经尝试了几次不同的迭代,错误检查我的ajax代码是通过简单地放置:echo“TEST TEXT”;在我的screen.php中,这是有效的。任何时候我将一个mysql查询插入screen.php,什么都没有出现。有没有比这更好的方法来实现我想要的?我基本上有一个投票系统内置到一个存储投票的mysql数据库中。投票最多的视频会在我建立的index.php文件中播放。