2

I have written a script to update my database, and every time the update process is done I want to open a new link

<?php
$con = mysql_connect("localhost","user","pass","db");
if (!$con){
    die ("connection error: ". mysql_error());
}
mysql_query("set @date1=(select date_add(max(date),interval 1 day) from mail.PB)");
mysql_query("set @date2=(select max(date) from Report.Traffic_City)");
$b=mysql_query("select count(*) day from date.datemonthyear_telkomsel where date between @date1 and @date2");
$day=array();
while($row=mysql_fetch_array($b)){
    for ($i=0; $i < $row['day']; $i++) {
mysql_query..................
$link="<script>window.open('http://localhost/link/mail/report.php');</script>";
    echo $link;
  } 
}
?>

With the script above, my data update is successful, but the link open after looping process is complete (open at the same time as many times as it loops).

report.php is script for send last update data. My plan is, every time the data update process finishes open report.php link (so the data sent is the latest data), if there are 3 recent data

(example 2018-03-31,2018-04-01,2018-04-02)

So the data sent is 3 emails with data for the date 2018-03-31,2018-04-01,2018-04-02.

bawel
  • 85
  • 1
  • 1
  • 9

1 Answers1

2

A couple of problems:

  1. Your PHP code is running on the server, building a response to send to the client. That response is likely buffered, so you need to flush the output stream if you want output to go proactively.

    According to this answer, you also need to ensure you set the charset of the response before the loop:

    header( 'Content-type: text/html; charset=utf-8' );
    

    (Assuming you're using UTF-8.)

  2. The browser is likely to refuse to open a pop-up that script asks for as part of page load, and is almost certain to refuse to open repeated ones. Instead, I suggest just outputting on-page content.

So perhaps:

<?php
header( 'Content-type: text/html; charset=utf-8' );
$con = mysql_connect("localhost","user","pass","db");
if (!$con){
    die ("connection error: ". mysql_error());
}
mysql_query("set @date1=(select date_add(max(date),interval 1 day) from mail.PB)");
mysql_query("set @date2=(select max(date) from Report.Traffic_City)");
$b=mysql_query("select count(*) day from date.datemonthyear_telkomsel where date between @date1 and @date2");
$day=array();
while($row=mysql_fetch_array($b)){
    for ($i=0; $i < $row['day']; $i++) {
        // mysql_query..................
        $msg="<p>Some useful information here...</p>";
        echo $msg;
    } 
}
?>

If it's really important that you show that report.php page, not just text, you could retrieve it via ajax and show its content in an element on the page, rather than using a pop-up, and use additional script tags output in your loop to do a refresh of the content. (Or load it as an iframe and refresh it.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875