如何在返回结果时重新加载AJAX div中的MySQL记录?

时间:2013-04-24 11:31:41

标签: php javascript mysql ajax

我想知道如何使用<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 ,如果不是,它会回到显示序列index.php的开头。

以下是index3.php重定向功能:

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

以下是我的AJAX代码,用于加载<div>

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

1 个答案:

答案 0 :(得分:0)

好吧,我弄清楚了。

  1. index3.php检查数据库以查看是否有任何设置为“活动”(在这种情况下,它还会检查显示它设置为显示的显示)。
  2. 当找到记录时,它会通过$i$_GET发送变量index4.php(用作键并设置为最高结果)。
  3. index4.php页面加载时,它会触发function MediaQuery(i),然后根据给定的密钥向QueryMedia.php询问数据库记录。
  4. 加载QueryMedia.php结果后,i键减少1,然后在短暂延迟后再次发送function MediaQuery(i)
  5. 一旦i密钥到达最后一个结果(总是0),该页面将重定向到index.php上显示序列的开头。
  6. 以下是index3.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>
    

    以下是index4.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'];?>)">
    

    以下是QueryMedia.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 -->
    

    我毫不怀疑这可能不是最有效的做事方式,有很多服务器请求正在进行,我确信代码有点臃肿。如果有人想帮助我做得更好,我一定会很感激你的意见。