我想知道如何使用<div>
重新加载AJAX
,其中显示的内容是从MySQL
database
检索的记录。棘手的是,我希望<div>
显示每个结果30秒,然后重新加载以显示下一个结果。
这里是media table
:
_______________________________________________
| FIELD 1 | FIELD 2 | FIELD 3 | FIELD 4 |
-----------------------------------------------
| ID | TVOne | TVTwo | Active |
-----------------------------------------------
| 15 | no | no | no |
-----------------------------------------------
| 29 | yes | no | yes |
-----------------------------------------------
| 53 | no | no | no |
-----------------------------------------------
| 71 | yes | yes | yes |
-----------------------------------------------
在TVOne页面上的
<div>
中,我希望它加载记录29和71。在TVTwo页面上的<div>
中,我希望它只加载记录71。这个table
由后端的用户修改,因此结果总是不同的。我读到了THIS QUESTION,我相信
javascript
选择器可能是拼图的一部分,但我不知道如何把它们放在一起。.eq()
所在的页面称为<div>
。上一页(index4.php
)执行查询,查看是否有任何记录设置为活动,如果是,则将index3.php
设置为num_rows
并转到$count
,如果不是,则返回到显示序列index4.php?count=$count
的开头。这里是重定向函数:
<?php
$result = $mysqli->query("SELECT * FROM media WHERE Active = 1 AND TVTwo = 1");
$count = $result->num_rows;
if($count > 0){$media = "index4.php?count=$count";}
else{$media = "index.php";}
?>
<script>
refresh=window.setTimeout(function(){location.href="<?php echo $media;?>"},15000);
</script>
下面是加载
index.php
的代码:<script>
function MediaQuery(qty)
{
if (qty=="")
{
document.getElementById("Media").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Media").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","../process/QueryMedia.php?count="+qty,true);
xmlhttp.send();
}
</script>
这里是QueryMedia.php:
<?php
$count = $_GET["count"];
$sql = ("SELECT * FROM media WHERE Active = 1 AND TVTwo = 1 LIMIT $count");
if(!$result_sql = $mysqli->query($sql))
{
echo QueryCheck("getting the media records ","from the media",$mysqli);
}
// fetch it here
?>
最佳答案
好吧,我搞清楚了。index3.php
检查数据库以查看是否有任何内容设置为“活动”(在本例中,它还检查设置为显示的内容)。
当它找到一个记录时,它通过$i
发送变量$_GET
(用作键并设置为最高结果)。
当加载index4.php
页时,它触发index4.php
,然后根据给定的键向function MediaQuery(i)
请求数据库记录。
加载QueryMedia.php
结果后,QueryMedia.php
键减小1,然后在短时间延迟后再次发送i
。
一旦function MediaQuery(i)
键到达最后一个结果(总是i
),页面将重定向到0
上显示序列的开始。
这里是index.php
:
<?php
$sql = $mysqli->query("SELECT * FROM media WHERE Media_Active = 1 AND Media_TVTwo = 1");
$count = $sql->num_rows;
if($count != 0)// If there are results returned then redirect to the media page.
{
$i = $count - 1;
$media = "index4.php?i=$i";
}
else // Else redirect to the first page.
{
$media = "index.php";
}
?>
<script>
window.setTimeout(function(){location.href="<?php echo $media;?>"},20000);
</script>
这里是
index3.php
:<script>
function MediaQuery(i)
{
if (i < 0)
{
window.setTimeout(function(){location.href="index.php"},0);
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("Media").innerHTML=xmlhttp.responseText;
var i = document.getElementById("i").innerHTML;
var i = i-1;
setTimeout(function() {MediaQuery(i)},20000);
}
}
xmlhttp.open("GET","../process/QueryMedia.php?i="+i,true);
xmlhttp.send();
}
</script>
<body onload="MediaQuery(<?php echo $_GET['i'];?>)">
这里是
index4.php
:<?php
$i = $_GET["i"];
$sql = ("SELECT * FROM media WHERE Media_Active = 1 AND Media_TVTwo = 1 ORDER BY Media_Name");
if(!$result_sql = $mysqli->query($sql))
{
echo QueryCheck("getting the media records ","from the media",$mysqli);
}
$id = array();
while($idquery = $result_sql->fetch_assoc())
{
$id[] = $idquery['Media_ID'];
}
$sql2 = ("SELECT * FROM media WHERE Media_ID = $id[$i]");
if(!$result_sql2 = $mysqli->query($sql2))
{
echo QueryCheck("getting the media records ","from the media",$mysqli);
}
while($media = $result_sql2->fetch_assoc())
{
$img = $media['Media_Name'];
$width = $media['Media_Width'];
$height = $media['Media_Height'];
$msg = $media['Media_Msg'];
}
?>
<img src="../images/media/<?php echo $img; ?>" border="2" width="<?php echo $width; ?>px" height="<?php echo $height; ?>px">
<p align="center" class="f24" style="margin:5px"><?php echo $msg; ?></p>
<p id="i" style="display:none;"><?php echo $i; ?></p><!-- used to return the key value -->
毫无疑问,这可能不是最有效的方法,有很多服务器请求正在进行,我相信代码有点臃肿。如果有人想帮助我做得更好,我绝对会感谢你的投入。