Jump to content

PHP Chat


twilitegxa

Recommended Posts

I have this script for a chat, but it's supposed to display the last 20 messages, but an error pops up after the twenty messages, instead of letting more message be submitted. Any suggestions on how I can change it to where it shows all messages, without a limit? Here is the script:

 

<?php

include "connect.php";

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>21)

{

   $startrow=$getmessages3-20;

}

else

{

  $startrow=1;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

while($getmsg3=mysql_fetch_array($getmsg2))

{

  $message=Smiley($message); //Smiley faces

   print "<font color='red'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}



function Smiley($texttoreplace)

{

    $smilies=array( 

    

    

    '' => "<img src='images/smile.gif'>",

    ':blush' =>"<img src='images/blush.gif'>",

    ':angry' =>"<img src='images/angry.gif'>",

    ''=>     "<img src='images/shocked.gif'>",  

    'fuck'=>"$#$%",

    'Fuck'=>"&$#@"

  



    );



    $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace);

    return $texttoreplace;

}

?>

<script>

  setTimeout("window.location.replace('chatlog.php')",2000);



</script>

Link to comment
Share on other sites

Just to make clear what Burn is saying, column names can be upper/lower or any combo when referring to them (mysql doesn't care). However table names MUST be the same case as they are set up. i.e. myTable must be written "myTable", NOT "mytable" or "MYTABLE".

 

What you really need twil is to use the LIMIT clause of MySQL, to grab the last 20 messages, or alternatively, you can have a TIMESTAMP column in your table, and do.

SELECT * FROM <table name here> ORDER BY <timestamp column name> DESC LIMIT 0,20

 

The above will select the last 20 messages from the database.

Link to comment
Share on other sites

I am looking at my chatmessages table in my database and it looks liek I have a 'postime' field, but it doesn't appear to be set to a timestamp. In my submit.php page, this is what is pulling the date, from my understanding:

 

$thedate = date("U"); //grab date and time of the post

 

Here is the full code:

 

<?php

include "connect.php";

if(isset($_POST['submit'])) //if submit button push has been detected

{

   $message=$_POST['message'];

   $name=$_POST['name'];

   if(strlen($message)<1)

   {

      print "You did not input a message";

   }

   else if(strlen($name)<1)

   {

      print "You did not enter a name, please try again.";

   }

   else

   {

      $message=strip_tags($message);

      $IP=$_SERVER["REMOTE_ADDR"]; //grabs poster's IP

      $checkforbanned="SELECT IP from ipbans where IP='$IP'";

      $checkforbanned2=mysql_query($checkforbanned) or die("Could not check for banned IPS");

      if(mysql_num_rows($checkforbanned2)>0) //IP is in the banned list

      {

         print "Your IP is banned from posting.";

      }

      else

      {

         $thedate = date("U"); //grab date and time of the post

         $insertmessage="INSERT into chatmessages (name,IP,postime,message) values('$name','$IP','$thedate','$message')";

         mysql_query($insertmessage) or die("Could not insert message");

    



      }

   }



      

}

print "<form action='submit.php' method='post' name='form'>";

print "Your name:<br>";

print "<input type='text' name='name' size='20'><br>";

print "Your message:<br>";

print "<textarea name='message' cols='40' rows='2'></textarea><br>";

print "<a onClick=\"addSmiley(':-)')\"><img src='images/smile.gif'></a> "; //replace images/smile.gif with the relative path of your smiley

print "<a onClick=\"addSmiley(':-(')\"><img src='images/sad.gif'></a> ";

print "<a onClick=\"addSmiley(';-)')\"><img src='images/wink.gif'></a> ";

print "<a onClick=\"addSmiley('>:-(')\"><img src='images/angry.gif'></a> ";

print "<a onClick=\"addSmiley('???')\"><img src='images/confused.gif'></a> ";

print "<a onClick=\"addSmiley('0:-)')\"><img src='images/angel.gif'></a> ";

print "<a onClick=\"addSmiley('8-)')\"><img src='images/cool.gif'></a> ";

print "<a onClick=\"addSmiley('X-(')\"><img src='images/dead.gif'></a> ";

print "<a onClick=\"addSmiley(':-D')\"><img src='images/grin.gif'></a> ";

print "<a onClick=\"addSmiley('(o_o)')\"><img src='images/shocked.gif'></a> ";

print "<a onClick=\"addSmiley('')\"><img src='images/kiss.gif'></a> ";

print "<a onClick=\"addSmiley(':-P')\"><img src='images/tongue.gif'></a> ";

print "<a onClick=\"addSmiley(':-/')\"><img src='images/hmm.gif'></a> ";

print "<a onClick=\"addSmiley('`(o_o)')\"><img src='images/tear.gif'></a> ";

print "<input type='submit' name='submit' value='submit'></form>";

print "<script language=\"Java Script\" type=\"text/javascript\">\n";

print "function addSmiley(textToAdd)\n";

print "{\n";

print "document.form.message.value += textToAdd;";

print "document.form.message.focus();\n";

print "}\n";

print "</script>\n";

print "<br><br>";



?> 

Link to comment
Share on other sites

This is my chatlog.php:

 

<?php

include "connect.php";

$getnummessages="SELECT COUNT(*) as messagecount from chatmessages";

$getnummessages2=mysql_query($getnummessages) or die("blah");

$getnummessages3= mysql_result($getnummessages2, 0);

if($getnummessages3>21)

{

   $startrow=$getmessages3-20;

}

else

{

  $startrow=1;

}

$getmsg="SELECT name, message from chatmessages order by postime ASC limit $startrow,$getnummessages3";

$getmsg2=mysql_query($getmsg) or die(mysql_error());

while($getmsg3=mysql_fetch_array($getmsg2))

{

  $message=Smiley($message); //Smiley faces

   print "<font color='red'><b>$getmsg3[name]:</b></font> $getmsg3[message]<br>";



}



function Smiley($texttoreplace)

{

    $smilies=array( 

    

    

    '' => "<img src='images/smile.gif'>",

    ':blush' =>"<img src='images/blush.gif'>",

    ':angry' =>"<img src='images/angry.gif'>",

    ''=>     "<img src='images/shocked.gif'>",  

    'fuck'=>"$#$%",

    'Fuck'=>"&$#@"

  



    );



    $texttoreplace=str_replace(array_keys($smilies), array_values($smilies), $texttoreplace);

    return $texttoreplace;

}

?>

<script>

  setTimeout("window.location.replace('chatlog.php')",2000);



</script>

 

Where would I put the code you suggested, aschk?

 

SELECT * FROM <table name here> ORDER BY <timestamp column name> DESC LIMIT 0,20

 

Or what would have to be changed in my script to do this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.