Auto Show number for ammount of new chat messages

Discussion in 'PHP' started by shivampaw, Nov 17, 2013.

  1. #1
    I have a link to my chat on my menu and I would like a number in brackets to show when a new message is posted or at the very least an explanation mark (!). When user 1 send a message it automatically shows user2 and all the other users an (!) or number of new messages in brackets. When user2 views the chat page the brackets disappear and it just says chat however the brackets do not go from all users link.

    How is this possible and how would I go about making it work? I have mysql database and the table is chat_chats.

    I would also need the link to auto update itself instead of a user having to go to another page for it to update.

    Thanks :)
     
    shivampaw, Nov 17, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Sounds to me you need a combination of PHP and AJAX calls - basically, this is a javascript-question, more than a PHP-question - the PHP / MySQL-part is fairly simple, just have a table with msg-id, user-id, and on/off for read or not, then pull that in for each user based on user-id.
     
    PoPSiCLe, Nov 17, 2013 IP
  3. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    so that is a seperate table which means two inserts on the chat_submit.php page?
    insert query will be
    mysql_query("INSERT INTO chat_read VALUES('on', '$userid')");
    PHP:
    Would it be ok if I scrap message id from the table?

    checking query will be
    $query = mysql_query("SELECT * FROM chat_read WHERE read='1' AND user_id='$userid'");
    $numrows = mysql_num_rows($query);
    
    Code (markup):
    Now code for the chat link:
    <a href='chat.php'>Chat ($numrows)</a>
    HTML:
    And on the chat.php we will have this code:
    mysql_query("UPDATE chat_read SET read='0' WHERE user_id='$userid'");
    PHP:
    That is the php and mysql done I think can you just clarify.

    But what is the javascript and ajax part of this.

    Thanks :)
     
    shivampaw, Nov 17, 2013 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    That will work, more or less. I don't think "on" and "1" is interchangable, so that needs to be worked out, and as long as you don't need to update separately for each user, what you have is fine. Also, if there isn't more than one chat, you don't need the chat-id.

    And, you shouldn't be using mysql_ <- it's deprecated, and will be removed from coming PHP-versions alltogether, hence it's not very futureproof.

    As for the AJAX-bit, that was mostly for checking on each page, and updating at set intervals, etc. so the counter is always updated. Depending on what you need, you might not really need this, but, if for instance you need the message counter to update whenever there's a new message, regardless if the user has updated the page, or gone off to a new page, then you need to call via AJAX a server-script (PHP) that pulls the information from the database.
     
    PoPSiCLe, Nov 17, 2013 IP
  5. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    You say that
    I will need it to show separately for each use so how would I sort this bit out as now I realise how it would fail. 'If i post the message it will be user_id of 1 which means other people wont see the notification.'

    Any ideas or sample code?

    Thanks
     
    shivampaw, Nov 17, 2013 IP
  6. shivampaw

    shivampaw Greenhorn

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    Sorted the php and mysql stuff.

    Not sure how to reload the link every 2 seconds though.

    codes:
    <?php
      $query = mysql_query("SELECT * FROM users WHERE username='$user' AND admin='1'");
      $numrows = mysql_num_rows($query);
    
      ?>
      <li><a href='chat.php'><span>Chat <?php if($numrows == "1"){echo "<b><font color='red'>(!)</b></font>";} ?></span></a></li>
    Code (markup):
    This needs to reload every 2 seconds not sure how to though.

    Thanks
     
    shivampaw, Nov 17, 2013 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    Every two seconds? That seems a bit... over the top, even for a chat (which runs in the background, I assume, since people are moving away from the chat window). Do a check every 5 seconds, or so.
    THAT would, however, be a AJAX-request, so I suggest going over to the javascript or jQuery forum here on DigitalPoint and ask there. Basically, you'd need to put the PHP in a separate file (most likely) and call that via AJAX, Json, etc. to get the information. Not entirely sure about the timing, but I assume it's basically just adding a timer for the javascript-function. Just remember that this will create a lot of HTTP-requests, and might slow down the page overall.
     
    PoPSiCLe, Nov 17, 2013 IP