We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP Chatting

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?

7
Contributors
30
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
31
Views
vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Hello,

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

phorce
Master Poster
747 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

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?

phorce
Master Poster
747 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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

BTW, thanks.

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

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.

phorce
Master Poster
747 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

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..

Robert1995
Junior Poster in Training
58 posts since Feb 2010
Reputation Points: 32
Solved Threads: 7
Skill Endorsements: 0

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

phorce
Master Poster
747 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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 chat.JPG 6.89KB
vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

Hey,

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

phorce
Master Poster
747 posts since Jul 2011
Reputation Points: 63
Solved Threads: 94
Skill Endorsements: 16

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

vizz
Posting Whiz
399 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 5

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.

Robert1995
Junior Poster in Training
58 posts since Feb 2010
Reputation Points: 32
Solved Threads: 7
Skill Endorsements: 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!

Dani
The Queen of DaniWeb
Administrator
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124

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.

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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

Dani
The Queen of DaniWeb
Administrator
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124

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. :(

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

I've reported the bug to Thumbshots.com.

Dani
The Queen of DaniWeb
Administrator
21,555 posts since Feb 2002
Reputation Points: 1,555
Solved Threads: 376
Skill Endorsements: 124

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

diafol
Keep Smiling
Moderator
10,838 posts since Oct 2006
Reputation Points: 1,675
Solved Threads: 1,534
Skill Endorsements: 61

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

peter_budo
Code tags enforcer
Moderator
15,812 posts since Dec 2004
Reputation Points: 2,871
Solved Threads: 944
Skill Endorsements: 52

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page generated in 0.1159 seconds using 2.73MB