PHPBuilder - Storing Data in the Client Page 5



RSS Twitter
Articles Tricks And Hacks

Storing Data in the Client - Page 5

by: PHP Builder Staff
|
July 30, 2000

PHP & JavaScript World Domination Series: Storing data in the client.

Ok, then and just for your "testing" purposes the "form" frame:

<?php

session_start
();

if (!isset(
$timestamp)) {
      
$timestamp=0;
}

// Display form, use JavaScript to get last timestamp.
if (isset($msg)) {
    
$dab=mysql_connect("localhost","root","seldon");
    
mysql_select_db("testbase",$dab);
    
$query="insert into testeable(timestamp,message) values(now(),'$msg')";
    
mysql_query($query,$dab);
    
// Now get all messages after last_time.
    
$query="select * from testeable where timestamp>'$tt'";
    
$result=mysql_query($query,$dab);
    
$msgs=array();$i=0;$timestamp=0;
    while(
$res=mysql_fetch_array($result)) {
        
$msgs[]=$res["message"];
        if(
$res["timestamp"]>$timestamp) {
            
$tt=$res["timestamp"];
        }
    }
    
session_register("timestamp");
    
// No I have the max timestamp...
    // Using JavaScript, we can set those values...
    
?>
      <script>
      <?php
         
for($i=0;$i<$count($msgs);$i++) {
             
?>
                top.lines[top.lines.length]="<?print("$msgs[$i]");?>";
             <?php
         
}
      
?>
      top.display.location.reload();
      </script>
    <?php
}

?>
<form name="foo" action="<?php print("$PHP_SELF"); ?>" method="post">
Message:<input type="text" name="msg">
<input type="submit" name="newmsg" value="send">
</form>

Note that we make the display refresh from the "form" frame this is nice to the chat user since it sees his message and the new ones as soon as he submits his text. Users like this since it adds some dynamic to the chat mechanism. The form frame is very similar to the loader as you can see.

We have presented you a powerful technique to minimize client-server transfers storing data in the client and to reduce server load making the client execute the complex display routines. You have the power, go and conquer the world.


« Previous Page
1
|
2
|
3
|
4
|
5

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:

Comment:



Comment:

(Maximum characters: 1200). You have characters left.