PHP-提取数据时的标题重定向

时间:2019-01-04 08:34:15

标签: php mysql

当我要在页面上获取数据时,我希望页面自动重定向到另一个页面,该怎么办?我有这个代码

这是我的代码,这里是获取数据的地方,但是一旦获取,我想自动将其重定向。

<div>
    <?php
    $data = mysqli_query($con,"SELECT * FROM announcement");
    $count = mysqli_num_rows($data);

    if ($count != 0) {
        //IVE ASSUMED THAT THIS WILL WORK BUT IT DIDNT 
        header("location:../addRedirect.php");

        while($row = mysqli_fetch_array($data)) { 
            echo '<div id="myModal" class="modal" style="position:fixed;  display: none; padding-top: 100px; 
            left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); ">
                <div class="container" style="width: 100%">
                    <div class="card col-12" style="background-color: red; color: white;">
                        <div class="card-header">
                            <p><h3 class="text-center">Announcement</h3></p>
                        </div>
                        <div class="card-body text-center">
                            <h5>'.$row['additional_info'].'</h5>
                            <p>Please click refresh button to resume after the announcement</p>
                        </div>
                    </div>
                </div>
                <img class="modal-content" id="img01">
            </div>';
            $dataid = $row['id'];
        }
    }
    ?>
</div>

在我的第二页上,我想将其重定向回..所以当我只是获取数据时,我拥有的是自动刷新..谢谢指导我,这对我作为php和编码的起步者是一个巨大的帮助

<?php     
header("location:../home.php");   
?>

1 个答案:

答案 0 :(得分:0)

如果已设置标题,则可以使用javascript而不是PHP刷新页面。

<div>
    <?php
    $data = mysqli_query($con,"SELECT * FROM announcement");
    $count = mysqli_num_rows($data);

    if ($count != 0) {
        echo "<script>setTimeout(\"location.reload()\",1000);</script>";
        // Other stuff ...
    }
    ?>
</div>

如果要导航到其他页面,可以执行以下操作:

<div>
    <?php
    $data = mysqli_query($con,"SELECT * FROM announcement");
    $count = mysqli_num_rows($data);

    if ($count != 0) {
        echo "<script>setTimeout(\"location.href = '../addRedirect.php';\",1000);</script>";
        // Other stuff ...            
    }
    ?>
</div>

两个示例的延迟都为1秒(1000毫秒)