Miscellaneous

  Home arrow Miscellaneous arrow Page 3 - Mini-Chat Tutorial
MISCELLANEOUS

Mini-Chat Tutorial
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 17
    2002-07-24

    Table of Contents:
  • Mini-Chat Tutorial
  • Basic Printing
  • Improving
  • Final Script

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Mini-Chat Tutorial - Improving


    (Page 3 of 4 )

    Since only a small number of messages are to be printed out, why keep other messages in our DB? We can, of course keep them for logging, but I prefer dropping them. So we update our DB by adding a field to order messages and we update our 'addMessage' function to ensure that no more than the desired number of messages is kept.

    <?php
    /*
    CREATE TABLE MINICHAT (
      NB tinyint(4) NOT NULL auto_increment,
      LOGIN varchar(20) NOT NULL default '',
      MESSAGE varchar(255) NOT NULL default '',
      ITSTIME varchar(10) NOT NULL default '',
      PRIMARY KEY  (NB),
      UNIQUE KEY NB (NB),
      KEY NB_2 (NB)
    );
    */

    // Add message into minichat
    function addMessage$login$message ) {

       
    $login mysql_escape_stringstrip_tags$login ) );
       
    $message mysql_escape_stringstrip_tags$message'&lt;a&gt;&lt;b&gt;&lt;i&gt;&lt;u&gt;') );
       
    mysql_query"INSERT INTO MINICHAT ( NB, ITSTIME, LOGIN, MESSAGE ) VALUES ( '10', '"time()."', '".$login."','".$message."' )" );
       
    mysql_query"UPDATE MINICHAT SET NB=NB-1" );
       
    mysql_query"DELETE FROM MINICHAT WHERE NB &lt; 1" );
    }
    ?>

    Since we have the time of each post, we can keep it and improve our print function. The second thing to do is to remove the LIMIT in the query, since now no more than 10 messages stay in the DB:

    <?php
    // Prints messages
    function printMessages() {
       
    $rs mysql_query"SELECT * FROM MINICHAT ORDER BY ITSTIME" );

       while ( 
    $msg mysql_fetch_array$rs )) {
          echo 
    date'h:m'$msg['ITSTIME'] )." ".$msg['LOGIN']." &gt;".$msg['MESSAGE']."&lt;br&gt;";
       }
    }
    ?>

    Now we'll drop that login box that appears each time the form is shown. For this, we'll update our print function to have it test if the box has to appear. A cookie will be set to keep your login if possible. If not, the login will stay in an hidden field.

    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;Mini Chat Sample&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form method="post"&gt;&lt;br&gt;
      &lt;?php
       if ( !$_COOKIE['minichatlogin'] ) {
          if ( !$_POST['login'] )
             echo 'Login:&lt;input type="text" name="login" size="6"&gt;&lt;br&gt;';
          else {
             @setcookie( "minichatlogin", strip_tags( $login ) );
             echo '&lt;input type="hidden" name="login" value="'.$_POST['login'].'"&gt;';
          }
       }
      ?&gt;
      Message:&lt;input type="text" name="message" size="10"&gt;&lt;br&gt;
      &lt;input type="submit" value="Send"&gt;&lt;br&gt;
      &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;

    Like this, the setcookie call will always fail since some html as been printed before the call, so we put all this in a func and reorganize the code to arrive to our final script.

    More Miscellaneous Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap