1.11M Members

PHP Chatting

 
0
 
CREATE TABLE IF NOT EXISTS `users` (
  `ID` mediumint(9) NOT NULL AUTO_INCREMENT,
  `username` varchar(60) DEFAULT NULL,
  `password` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

CREATE TABLE IF NOT EXISTS `chatmessage` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `from` varchar(250) NOT NULL,
  `to` varchar(250) NOT NULL,
  `message` text NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;


   $query =("SELECT * FROM  users ");
   $result = mysql_query($query);

   while ($row = mysql_fetch_assoc($result))
 {
    $users=$row['username'];
    echo"<a href='chat.php'>$users</a><br>";    
 }

Above while loop gives users from database.
How to chat with one or multiple users listed inside anchor tag?

 
0
 

Hello,

Are you trying to create these tables everytime you run the SQL query?

 
0
 

no it's only database I'm using to collect information

 
0
 

Okay, it's really unclear what you want to do...

I'm guessing you want some kind of message board, is this correct?

If so, you'd need a page that has something like "chat.php?id={id}" and then select/post messages to the table depending on it's ID..

<?php
   // chat.php
   $id = $_GET['id'];

   // include mysql connection data

   // query the database:
      $query = "SELECT * FROM chat WHERE id='$id'";
      $result = mysql_query($query);
      if(mysql_affected_rows() >= 1)
      {
          // display the chat
      }else{
        echo 'There are currently no messages';
      }
   ?>

Something along those lines?

 
0
 

this helped little.
I'm creating something like gmail chat.

BTW, thanks.

 
0
 

Okay..

But how would you do it via realtime? How will you alert the user that they have another message?

I would start with something easier, something like a private messaging system (This will give you a rough idea of how to create something simular).

So I'm guessing your using cookies/sessions?

<?php

    // connection information
    // connection information

        $query = "SELECT * FROM chat WHERE to='{$_SESSION['user_id']}'";
        $result = mysql_query($query);
        if(mysql_affected_rows() >= 1)
        {
           while($row = mysql_fetch_array($result))
           {
              // Here you can display the actual message "gmail"
              // or create a link (like the previous post) that contains the message.

           }
         }else{
           echo 'No new messages';
         }
        }
   ?>

Hope this helps. Look into "polling"/"Sockets" if you want something realtime.

 
0
 

ok
This is for receiving message but how to send message?

 
-1
 

Just get this one {URL SNIPPED} and problem solved, you seem like a bad coder so you should probably take a look how others done it before. Maybe i'll get rep- but who cares..

 
0
 

Robert, was there any need to post that? No. Grow up.

 
0
 

chat

How to send message? is meant for How to create chat window on click, for each user selected, to send message like gmail chat?

Attachments
 
0
 

Hey,

This type of application is going to require jQuery/Ajax and PHP.

 
0
 

yes, can you help me for coding to send message to selected user?

 
0
 

Wow, honestly I had not ideea what I posted THE HOVER IS BAD, THIS IS A BUG CLICK THE LINK FOR GODS SAKE, i put this : https://blueimp.net/ajax/ it's a free open source ajax chat.

 
0
 

There's some type of bug ... you're posting a legit site, but for some reason the hover is showing an asian porn site of some kind!

 
0
 

Ha ha ha. Dani - are you pandering to some of your more concupiscent members? Now that's funny. Why didn't you make the hovers link to a speciality tea or a cleaning fluid site?

But seriously, it seems like a thumbshot.com error

DO NOT FOLLOW THE LINK BELOW!!!

<img src="http://images.thumbshots.com/image.aspx?cid=1162&amp;v=1&amp;w=300&amp;url=https://blueimp.net/ajax/">

From Inspect Element / Chrome, this is one of the last elements in the page.

 
0
 

thumbshot.org error or not ... It's not very appropriate.

 
0
 

I agree. When I investigated further (not trying to look at Asian porn sites, I assure you!), the link to the offending thumb only seemed to happen if the entire url without the urlencoded & was entered. Using just the id or just the url parameteres in the querystring did not show the image. Similarly taking out the v or w parameters stopped the image from appearing. WHen I searched for the blueimp url in thumbshots.com, it said coming soon, so it doesn't appear to have been previously referenced. However, I may be wrong - I frequently am.

//EDIT

I don't think the search function is working as search for referenced sites. So disregard my last bit of nonsense. :(

 
0
 

I've reported the bug to Thumbshots.com.

 
0
 

OK, back to the thread. Sorry for going off-piste.

The blueimp chat is very good and I've used it myself in a few projects. However, if you are determined to roll your own - a great way to develop your skills as it's not very difficult to get going, but takes time to get it to look and work like you need. One of the bigegst problems IMO is resources. Ajax is all well and good, but you still need to check the server for posts periodically (pull). There is a similar method out there called comet, which pushes data from the server. It's sometimes called "long polling". The APE project has some nice examples, although it's not that straightforward.

By now there are a number of different 'push' technologies, but I'm no expert on these I'm afraid.

//EDIT

forgot to mention - Comet - it uses javascript on both server and client - not php

 
0
 

I've reported the bug to Thumbshots.com.

At the time when post was reported, I actually opened that link and it was going to porn site. Hence reason for infraction

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article