PHP Coding problem(Newbie)
Posted: Mon Aug 08, 2011 10:54 pm
Postby flashery ยป Mon Aug 08, 2011 9:39 am
I want to know how can I manipulate this problem.
I am making a chat app from scratch and what I want is to list a user on the div if he/she is on the chat page (not just online) and I succeeded in doing it. But the problem is when the user is not on the chat page want the user to be deleted on the list and also if the user came back to the chat page he also be added again on the list.
Here is what I have done
I created a database for chat user with the field name id username and status
I created this class to manipulate the listing and deleting of the user
I call this class on my chat page using this
And use its function, like this, on my chat page also
<div id="userlst">
<?php
?>
</div>
The above coding is doing great..
Here is my problem if I want to delete the user in the list by using the function on jquery like this event
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the chatting?");
if(exit==true){
window.location = 'index.php';
<?php $Chat_User->user_left($name); ?> //my php code to delete the user's listing
$.post("postlogOut.php");
}
});
Nothing happened what it done is when i refreshed the page the user will be deleted even if he/she is on the page.
As well as putting the code on the postlogOut.php to be manipulated their also nothing happened
Please Help......I am really a newbie.
I want to know how can I manipulate this problem.
I am making a chat app from scratch and what I want is to list a user on the div if he/she is on the chat page (not just online) and I succeeded in doing it. But the problem is when the user is not on the chat page want the user to be deleted on the list and also if the user came back to the chat page he also be added again on the list.
Here is what I have done
I created a database for chat user with the field name id username and status
I created this class to manipulate the listing and deleting of the user
Code: Select all
<?php
class Chat_User{
function user_left($user){
$query = mysql_query("UPDATE chat_users SET status = 0 WHERE username = '$user'");
if(!$query){
die(mysql_error());
}
}
function user_enter($user){
$query = mysql_query("UPDATE chat_users SET status = 1 WHERE username = '$user'");
if(!$query){
die(mysql_error());
}
}
function list_users(){
$query = mysql_query("SELECT * FROM chat_users WHERE status = 1");
if(!$query){
die(mysql_error());
}
$i = 0;
while($i < mysql_num_rows($query)){
$username = mysql_result($query, $i, "username");
echo $username."<br/>";
$i++;
}
}
}
?>Code: Select all
require("chat_module/Chat_User.class.php");
$Chat_User = new Chat_User;
<div id="userlst">
<?php
Code: Select all
$Chat_User->list_users();</div>
The above coding is doing great..
Here is my problem if I want to delete the user in the list by using the function on jquery like this event
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the chatting?");
if(exit==true){
window.location = 'index.php';
<?php $Chat_User->user_left($name); ?> //my php code to delete the user's listing
$.post("postlogOut.php");
}
});
Nothing happened what it done is when i refreshed the page the user will be deleted even if he/she is on the page.
As well as putting the code on the postlogOut.php to be manipulated their also nothing happened
Code: Select all
<?php
require("handlers/user_handler.php");
require("chat_module/Chat_User.class.php");
$name = $User->get_info("first_name");
$Chat_User = new Chat_User;
$Chat_User->user_left($name); //my php code to delete the user's listing
if(isset($_SESSION['name'])){
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User ".$_SESSION['name']." has left the chat session.</i><br/></div>");
fclose($fp);
}
?>