2

So i was thinking, i have a simple cms with and admin page listing all of the users registered to my system, but what i would like is a way to realtime see those users being created and any updates they do to there information, so if im on the admin page and a new user signs up or someones changes there email address i would like for the page to show the change without me having to refresh, i dont really know where to start except i know that i would have to use Ajax to refresh the information without refreshing the page. If anyone could give me any kind of pointers or snippets i would be very greatful.

Thanks,
Ben

3 Answers 3

1

Not sure exactly how much detail you are looking for here, but here's a high level shot at it;

check_new_users.php

$timestamp = $_GET['timestamp'];
$array_of_new_users = getNewUsersFromDB($timestamp);
echo json_encode(array('new_users' => $array_of_new_users));

check_new_users.js (assuming you have JQuery included)

$(document).ready(function () {

  // function to poll the server and get the new users from your script
  var CheckNewUsers = function () {
    var data = {timestamp : new Date().getTime()};
    $.getJSON("path/to/check_new_users.php", data, function (JSON) {
      var new_users = JSON.new_users,
        rowHTML = '';

      // the exact structure here depends on what the array
      // returned from the function getNewUsersFromDB() looks like
      // lets assume it looks like:
      // array(userid => array(info about the user))
      // E.G.
      // array(1 => array(userid => 1, username => "matt"),
      //       2 => array(userid => 2, username => "joe")...

      // process and display the new users onto your page
      for (userid in new_users){
        u = new_users[userid];
        rowHTML = '<tr><td>' + u.userid + '</td><td>' + u.username '</td></tr>'
        $('#user_table').append(rowHTML);
      }
    }
  };

  // and set the function to run every 5 minutes
  setInterval(CheckNewUsers, 300000);  // 300000 milli = 5 minutes

});

Let me know if you want more detail on any of that I'd be happy to elaborate!

Sign up to request clarification or add additional context in comments.

3 Comments

Hi mate, thanks this helps alot but once i get the new array of data, how do i then display it in a table?
@Ben Cobbold, I updated my answer with a loop to write them out to a table. See the comment for how that depends on how your data is structured.
Hi mate, im really confused, is there anyway to contact you over msn or skype so that you could explain this to me and possibly show me how to intergrate it? Thanks, Ben
0

Depending on the CMS you use, this might be easy or hard. Like Nik said, you will need to code a client side site for this that polls the server-side script at set intervals. This shouldn't be too hard, especially using something like jQuery.

On the server side, you need to know whether or not these modifications are stored in a table, or not. Some CMS's might put a timestamp on edits, and new user creation. If this is the case, then you should pass a timestamp to the AJAX page along with any updates. Then on the next poll, include the last timestamp in the request and have the database query for activity where TIME > TIMESTAMP.

You might need to edit the CMS in multiple places to add this functionality, and its hard to say exactly what is needed without knowing more details.

Comments

0

hope this helps, i know i didn't get into the exact details, but this may give you a roadmap.

you can use javascript to poll the server at some regular interval (maybe once every 15 seconds).

it can make an HTTP request to some url dedicated to this purpose, like maybe yoursite.com/poll_for_user_changes. this url, rather than serving HTML web content back, can simply send a JSON object (or some other format of data), which will contain any changes that you would want to know about.

when you hit yoursite.com/poll_for_user_changes, your server is going to have to figure out whether there have been changes to your user table in the database. so it would be helpful if your javascript would keep track of when the browser last received an update from the server, so when it asks for a new update it can send in the get string of the URL (or via post) the last_updated_timestamp of when it last received an update. the server can use this info by building a database query that asks for, for instance, rows in the user table that have a creation timestamp that is > the last_updated_timestamp.

once your javascript receives the data from the server about whether there have been any changes, you will have to decide how to incorporate this data into the website to automatically show the changes without refreshing.

3 Comments

Hi well, i do have a timestamp, everytime a user logs in it stores a timestamp in epoch of when they did, but even know they logged in doesnt mean that there is a change in information, so lets say i had 500 registered people i would be pulling a lot of information if only 300 were logged in, so i need a way of checking specific things have changed. also with the new users couldnt i just take the total amount of rows and take the previous total amount and compare the two, if the current total amount is greater than the previous then it gets all of the new information?
oh well i meant whenever you poll, every 15 seconds or whatever, keep track of that timestamp of when you polled, so next time you poll you are only asking the database for changes that occurred in the last 15 seconds. i edited my post a little to make it more clear what i was saying.
btw, the answer / code example from goggin13 lines up with my outline perfectly, so if you read them together you might be able to understand the code better. good luck! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.