1

I have looked around the web including Stack Overflow's 'question and answer' history, but I can't find the related answer to mine; maybe because I don't really understand all posts published in English but as a beginner in programming, I really need this explanation and I think it should be clear and easy to understand.

My question is, is there some easy way to make PHP and JavaScript to answer this question:

I have two computers online, one computer opens 'my website' where there is scrolling news at the top of the website. The other computer is in my hand, here inside admin area.

Now, I add a new data in 'news table db' in my admin area, type it, and push click.

Then I look at the other computer, I want the new data to BE THERE without touching any part of the keyboard.

That's all, a simple question maybe. It's easy for Facebook's developers, but not for me. I need a detailed explanation from someone who know this. I know a little of PDO, but less JS.

Synchro
  • 35,538
  • 15
  • 81
  • 104
user3706926
  • 181
  • 2
  • 12
  • May be LONG POLLING will help you, http://stackoverflow.com/questions/333664/how-to-implement-basic-long-polling – Dimag Kharab Nov 12 '14 at 06:14
  • Use Ajax to display live data without refreshing your page. By setting certain time out to your Ajax method – fidel castro Nov 12 '14 at 06:16
  • @fidel castro, By setting certain time out? but couldn't ajax is set from my click (as admin) handler? – user3706926 Nov 12 '14 at 06:25
  • 3
    [html5 SSE](http://www.w3schools.com/html/html5_serversentevents.asp) or [Sockets](http://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php) will be helpful – turtle Nov 12 '14 at 06:43
  • fetch the data from your DB in the client machine in frequent interval through ajax call. Any update made on the server DB will affect the scroll news.That is new content is automatically replaced without doing refresh – fidel castro Nov 12 '14 at 07:16
  • $(function worker(){ $.ajaxSetup ({ cache: false, complete: function() { setTimeout(worker, 10000); } }); var ajax_load = "loading..."; var loadUrl = "testimony.php"; $("#result").html(ajax_load).load(loadUrl); }); right? – user3706926 Nov 12 '14 at 07:26
  • What is the disadvantage of the system? overload the server internet connection? or more? :( – user3706926 Nov 12 '14 at 07:32

1 Answers1

0

Method 1: Create a log table that records what is inserted, then have javascript make requests to a script that implements long polling:

$time=(isset($_GET['timestamp']) ? $_GET['timestamp'] : time());
while(true){
    if($data=get_new_data($time)){
        echo $data;
        exit;
    }
    sleep(5);
}

Method 2: Use web sockets to push data to clients as it is inserted by extending the method above to send a websocket message, set $time to time(), and sleep instead of exit. https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API

Chinoto Vokro
  • 928
  • 8
  • 17