while ($newmessage = 0) {
$testa = "SELECT * FROM messages WHERE session = '$session' AND time < '$timestamp'";
$testb = mysql_query($testa, $link) or die(mysql_error());
$newmessage=mysql_numrows($testb);
sleep(1);
if ($newmessage > 0) {
header( "location: http://www.domain.com" );

}
}

Hi, I have that code. Basically, its for a small two way chat script. That code is within an iFrame and basically should refresh the page when a new message is found in the datatbase.

As you can see this is done by comparing the timestamp of the message and an infinate loop, untill it find a message. However this is not functioning properally. I am unsure where the problem is from. The MySQL is fine, so I guess its either the header or the loop, or both.

I do not think the header works as code is already echoed earlier up on the page. I do not no of another way of doing this though..

Also is the loop setup correctly?

Thanks,

    Wouldn't an infinate loop hitting your database like that be stressful on the server?

      I can increase the sleep() to take load off the server if it becomes to high. Also I plan to make the infinant look time out once the user becomes inactive.

      Any help on getting it working?

      Thanks,

        Hi,

        in which way isn't it functioning properly ?

        Thomas

          Hi,

          If i add a new message into the DB, with the same sessioncode and a higher value in time it dosnt reload the page like it should. There are no error messages etc, it just dosnt do what it should.

          Thanks,

            Hi,

            shouldn't it be

            time > '$timestamp'

            instead of

            time < '$timestamp'

            So the query fetches all records with a time > $timestamp ?

            Thomas

              Yes I noticed this error just after posting. Even with this changed thought it still does not work.

              Is there an alternative to header() ?

              Thanks,

                Is this a visible IFRAME ? Do you want to refresh the complete page or just the iframe ?

                Thomas

                  An alternative to header() is javascript's "window.location"

                    Originally posted by Kudose
                    An alternative to header() is javascript's "window.location"

                    I just searched google for this, but have found little related to this command and refreshing the page. Can you explain a litte more please?

                    Thanks,

                      Hi,

                      I'd choose document.location in this case, but using the header function should work anyways. Using a while loop with sleep can cause troubles with the max_execution_time php setting.

                      <?PHP
                        // insert code that starts session and does the other stuff.
                        $newmessage = 0;
                        $header = "thisscript.php";
                        $testa = "SELECT * FROM messages WHERE session = '$session' AND time > '$timestamp'"; 
                        $testb = mysql_query($testa, $link) or die(mysql_error()); 
                        $newmessage=mysql_numrows($testb); 
                        sleep(1); 
                        if ($newmessage != 0)
                          $header = "http://www.domain.com";
                      
                        header("Location: $header";
                        exit;
                      ?>
                      

                      With javascript you can do the same, e.g.

                      <html>
                      <head>
                      <script language="JavaScript">
                        document.location.replace("http://www.domain.com");
                      </script>
                      </head>
                      <body>
                      &nbsp;
                      </body>
                      </html>
                      

                      Thomas

                        Hi, thanks for all the help so far.

                        <?PHP
                        // insert code that starts session and does the other stuff.

                        $newmessage = 0;
                        $testa = "SELECT * FROM messages WHERE session = '$session' AND time > '$timestamp'";

                        $testb = mysql_query($testa, $link) or die(mysql_error());

                        $newmessage=mysql_numrows($testb);

                        sleep(1);

                        if ($newmessage != 0)

                        <script language="JavaScript">
                        window.location.reload();
                        </script>

                        exit;
                        ?>

                        Is that right for the javascript way? I dont see how this loops? 😕

                        Thanks,

                          Hi,

                          please post a description of how that chat application works. Is the IFRAME the chat window ?

                          There might be several ways to solve the problem but I need to know more about the app.

                          One possible solution.

                          Use tables to print the chat submissions. You could use an infinite loop but that could lead to problems with the max_execution_time setting in php.ini.
                          Inside the (infinite) while loop check if someone submitted new data. If yes, print a new row of the table (or even a new table for each submission) and then use flush() to force the output.

                          Another solution could be to have another hidden frame or iframe that reloads itself every one second and updates the iframe by using JavaScript each time someone submitted new data.

                          Thomas

                          Thomas

                            Ok,

                            Basically, the chat page has two iframes on it. One is to send the message, which just puts the message into the database. The other iframe, simply displays all the messages in the database. The only part which should reload is the iframe displaying the messages. I quite like the sound of the hidden IFRAME, which refreshed the main IFRAME when a new message is available. I am unsure how you would go about this though.

                            Thanks,

                              Hi,

                              did you already think about using frames instead of iframes ? By using iframes you might run in troubles with several non IE browsers.

                              In a hidden frame you could load a script that just reloads every n seconds. That script checks if new data has been posted and if yes you can use javascript code to reload the main frame.

                              Like (in short)

                              if (new data exists) {
                              parent.frames['chatmainframe'].location.reload();
                              }
                              document.location.reload();

                              Thomas

                                The problem is though, i am using the trasparancy feature as its all mapped round an image.

                                Thanks,

                                  Ok,

                                  that makes using standard frames a little bit more complicated. I think it could be done with frames but that would be some work. At least you would have to cut the image into several parts.

                                  Did you already try to use a hidden frame ?

                                  Thomas

                                    Hi,

                                    I am giving it a go with the hidden IFRAME now. I am unsure how to do this though. The IFRAMES are all setup, but I do not no how to make the page to check if a new message has been left as once its refreshed it would loose its value for $timestamp

                                    Thanks,