Skip to content

Instantly share code, notes, and snippets.

@prio101
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save prio101/79180dc7c0070545e507 to your computer and use it in GitHub Desktop.

Select an option

Save prio101/79180dc7c0070545e507 to your computer and use it in GitHub Desktop.
Chat Client Core
<?php
/*
* Start of the Base_chat
* Chat App based on PHP
* Author : Infobase Team
*/
class BaseChat{
// Database Connection
private $dbConnection ;
/**************Connection details*************/
private $_dbHost = 'localhost';
private $_dbUserName = 'root';
private $_dbPassword = 'root';
private $_databaseName = 'chat_db';
/*******End of the connection details********/
/*
* Create the connection with the database
* and stores it in dbConnection property
*/
public function __construct(){
$this->dbConnection = new mysqli( $this->_dbHost,$this->_dbUserName,
$this->_dbPassword,$this->_databaseName
);
if ($this->dbConnection->connect_error) {
die('Connection Error') ;
}
}
/*End of the construct*/
/*
* Get the message from the chat
* @return array
*/
public function getMessage(){
$message = array();
$query = <<<QUERY
SELECT
'chat'.'message',
'chat'.'sent_on',
'users'.'id',
'users'.'uesrsname'
FROM 'uesrs'
JOIN 'chat'
ON 'chat'.'user_id' = 'users'.'id'
ORDER BY 'sent_on'
QUERY;
// excute the query
$resultObj = $this->dbConnection->query($query);
while ($row = $resultObj->fetch_assoc()) {
$message[] = $row;
}
return $message;
}
/*End of the get message*/
/*
* Add a new message to that chat table
* @param Integer $userId the user who sent the message
* @param String $meesge
* @return bool|integer the last inserted id of success
*/
public function addMessage($userId,$message){
$addResult = FALSE ;
$cUserId = (int) $userId ;
// Escape the message
$cMessage = $this->dbConnection->real_escape_string($message) ;
$query = <<<QUERY
INSERT INTO 'chat'('user_id','message','sent_on')
VALUES ({$cUserId},'{$cMessage}',UNIX_TIMESTAMP())
QUERY;
$result = $this->dbConnection->query($query);
if($result != false){
// Get last row of message
$addResult = $this->dbConnection->insert_id;
}else{
echo $this->dbConnection->error;
}
return $addResult;
}
/*End of the addMessage*/
/*
* Start of the Clear Last massage
* @param Integer $userId
* @param String $message
* @return Delete $lastMessageDelete
*/
public function clearLastMessage($userId){
$lastMessageDelete = FALSE ;
$cUserId = (int) $userId;
$query = <<<QUERY
DELETE FROM 'chat'('message','sent_on')
WHERE ({$cUserId},UNIX_TIMESTAMP())
QUERY;
}
/*End of the clearLastMessage*/
}/*End of the class*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment