Miscellaneous
  Home arrow Miscellaneous arrow Page 3 - Mini-Chat Tutorial
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Mini-Chat Tutorial
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2002-07-24

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

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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


       · test
       · I modified the Mini-Chat. I added:- Example for database connection- Different...
       · 
       · I read the disclaimer and also the authors terms, but I didnt seem to find anything...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek