Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a chat quest... 3

Status
Not open for further replies.

MAWarGod

Programmer
Feb 15, 2002
352
US
complete.gif


is it possible to do a chat interface like above in PHP? if so how would I get started? I have been trying to build this type of chat for years.. in perl or cgi but can not get the features I want.. So that I am very new to php I ask if this can be done?

MA WarGod

I believe if someone can think it, it can be programmed
 
i had a couple of mintes spare so ported the chatroom to mysql. code hereafter but completely untested

Code:
<?php
$chat = new chats;

class chats{

	private $timestamp = 0;
	private $action;
	
	function chats(){
		require 'databaseConfig.txt';
		$this->timestamp = time();
		mysql_connect(HOSTNAME, USERNAME, PASSWORD) or die (mysql_error());
		mysql_select_db(DATABASENAME);
		$result = mysql_query("SHOW TABLES LIKE 'chat'");
		if (mysql_num_rows($result) === 0){
			$this->createDatabaseSchema();
		}
		$this->action = isset($_POST['action']) ? $_POST['action'] : 'getChatData';
		$this->switchBoard();
	}
	
	function switchBoard(){
		switch ($this->action){
			case 'writeChatData':
				if ($this->writeChatData()){
					echo '1';
				} else {
					echo '0';
;				}
			break;
			default:
				echo $this->getChatData();
		}	
	}
	
	function buffer(& $result){
		$temp = array();
		while ($row = myqsl_fetch_assoc($result)){
			$temp[] = $row;
		}
		return $temp;
	}
	
	function getChatData(){
		$return ='';
		$timestamp = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
		if ($timestamp == 'null') $timestamp = 0;
		$query = "Select chatID, chatPerson, chatTimestamp, chatText from chat where chatTimestamp > ? order by chatTimestamp DESC limit 50 ";
		$result = mysql_query($query) or die(mysql_error());
		$chats = buffer($result);
		foreach ($chats as $chat){
			$date = date('Y-m-d H:i:s', $chat['chatTimestamp']);
			$chatText = nl2br($chat['chatText']);
			$return .= <<<HTML
<tr>
	<td id="{$chat['chatID']}">{$chat['chatPerson']}<br/>$date</td>
	<td>{$chatText}</td>
</tr>
HTML;
		}
		if (empty($return)){
			return 'none';
		} else {
			return json_encode(array('timestamp'=>$this->timestamp,'chatText'=>$return));
		}
	}
	
	function writeChatData(){
		$chatPerson = empty($_POST['chatPerson']) ? NULL : trim($_POST['chatPerson']);
		$chatText = empty($_POST['chatText']) ? NULL : trim($_POST['chatText']);
		if (empty($chatText) || empty($chatPerson)){
			return false;
		} else {
			$query = "Insert into chat (chatTimestamp, chatPerson, chatText) values ('%s','%s','%s')";
			$params = array(time(), $chatPerson, $chatText);
			array_walk($params, 'mysql_real_escape_string');
			$query = vsprintf($query, $params);
			$result = mysql_query($query);
			if (!$result){
				echo mysql_error();
				return false;
			}
			return true;
			
		}
	}
	
	function createDatabaseSchema(){
		$query = "	CREATE TABLE  chat (
							 chatID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
							 chatTimestamp INT( 11 ) NOT NULL ,
							 chatPerson VARCHAR( 255 ) NOT NULL ,
							 chatText LONGTEXT NOT NULL
							) ;
					)";
		mysql_query($query);
	}
}
?>

 
jpadie
Thank You so much..I know I will be back to You with more question I am sure..but I am learning and I thank You for helping Me..




southbeach

Yes I would be interested..The reason being You have removed the html so it shows only text I want to see how You did that and a few other features You made.. see I want html in lower chat but to make drop down menu I must remove it in upper chat like the picture above.. So if You could please share it. I would be very grateful..


MA WarGod

I believe if someone can think it, it can be programmed
 
I will put all code together and put it up for download. I will come back here to post the link.

Regards,
 
southbeach
thank You for taking the time to help Me..I am very grateful..

MA WarGod

I believe if someone can think it, it can be programmed
 
MAWarGod,

Follow this link

Once there, scroll down and click on page image to download the zip file with all PHP, CSS, HTML and JS code I am using.

Also, click on PHP Chat Room link - Within the description, I pasted the structure for the two MySQL tables used with this code.

Enjoy ... Again, much thanks to jpadie for his help and contribution to get this code to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top