0

I want my page to automatically redirect to another page when the data is fetch on the page so how can I do it ? i have this code

here is my code here is where the data is fetched but once it fetch i want to redirect it automatically.

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

and on my second page i want to redirect it back.. so what i have is an auto refresh when im just fetching data.. thanks guide me pls this is a huge help for me as a starter of php and codings thanks.

<?php     
header("location:../home.php");   
?>
R Pelzer
  • 1,188
  • 14
  • 34
  • 1
    Do you only want to refresh your page? Then you should look to this question: [Refresh a page using php](https://stackoverflow.com/questions/12383371/refresh-a-page-using-php). The problem you are facing is that you have rendered HTML before replacing the header and this is not allowed in PHP. [Display message before redirect to other page](https://stackoverflow.com/questions/18305258/display-message-before-redirect-to-other-page) – R Pelzer Jan 04 '19 at 08:37
  • @RPelzer Yes, thank you. but my problem is i dont know if the header is reading my fetched data. how can I do this syntax in php `if row has 1 data then refresh or locate to other url` –  Jan 04 '19 at 08:42
  • To set headers you need to make sure this is done before any HTML is sent. This code may need to go at the very top of the page, instead of using echo on the modal you can just store it in a variable and echo later on. Also `
    – Second2None Jan 04 '19 at 08:48
  • @Second2None sir can u pls edit my code please thanks im new to php any guidance pls thanks –  Jan 04 '19 at 08:49
  • It's really difficult to know what you want it to actually do, what you are about to do is set up an infinite loop if there is an announcement. – Second2None Jan 04 '19 at 08:50
  • Sorry, not able to understand clear requirement. As you mention code after redirect other code will not show into current page which mention into div tag. Why you not using AJAX and update div id based on response. It will work without refresh page and added data will append into current page – Dipti Jan 04 '19 at 08:55

1 Answers1

0

You can refresh your page using javascript instead of PHP, if the headers are already set.

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

If you want to navigate to an other page you can do this:

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

Both examples have a delay of 1 second (1000 milliseconds)

R Pelzer
  • 1,188
  • 14
  • 34
  • @R Pelzer , sir i think there's something wrong with your syntax. as for mine the `\"` has a different color than the rest of the code it is color violet but the rest is yellow. thnkas sir –  Jan 04 '19 at 08:57