PHP & JavaScript World Domination Series: Storing data in the client.
The loader frame is next:
<?php
session_start(); // We use Sessions here!
if(!isset($timestamp)) {
//If no timestamp set we define it as 0
$timestamp=0;
}
$dab=mysql_connect("localhost","user","password"); // Use your own values here
mysql_select_db("testbase",$dab);
// Query the messages not seen by the client
$query="select * from testeable where timestamp>'$timestamp'";
$result=mysql_query($query,$dab);
$msgs=array();
// In the loop we store the messages in an array and also get the
// maximum timestamp
while($res=mysql_fetch_array($result)) {
$msgs[]=$res["message"];
if($res["timestamp"]>$timestamp) {
$timestamp=$res["timestamp"];
}
}
session_register("timestamp"); // Register the timestamp
echo '<script>';
// In this loop we generate JavaScipt Code to
// Add the messages to the array in "master" (note the use of "top"
// to refer to the parent frame which is master)
for($i=0;$i<$count($msgs);$i++) {
?>
top.lines[top.lines.length]="<?php print("$msgs[$i]"); ?>";
<?php
}
// Now we'll generate JS code to make the display frame reload.
?>
top.display.location.reload();
</script>
<!-- Note the JS setInterval method to make this frame reload each 4 seconds -->
<body onLoad="window.setInterval('location.reload()',4000);">
</body>