アクティビティ テーブルから読み取る PHP スクリプトに対して、JQuery を介して x 秒ごとに呼び出しを行うアクティビティ テーブルをポーリングすることができます。そうすることで、ユーザーがボタンを押したかどうかを検出できます。これを行う簡単な方法は次のようになります: ユーザーがボタンを押す -> アクティビティをタイムスタンプと共にアクティビティ テーブルに送信するので、ユーザーがいつボタンを押したかがわかります (これについては後で説明します)。JQuery を介して PHP に投稿し、データベースに挿入することで送信できます。アクティビティ テーブルには、id (INT)、primary、auto_increment が含まれます。ボタン (INT) [クリックされたボタン ID] dateposted [タイムスタンプ] さて、JQuery と PHP を使用してそのテーブルをポーリングする/そのテーブルを呼び出すたびに、時間 (重要) とボタン ID をポーリングすることになります。多くの場合、次のようにします。
クライアント側: (このようにボタンごとに異なる ID を持つことができます)
var auto_refresh = setInterval(
function reloadstring()
{
$.get("checknewactivity.php", function(buttonid){
if (buttonid == 1) /* Say he clicked the record button for example */
{
$("#recordbtn").css({"background-color":"#FF0000"}); /* Make the record button red */
}
});
}, 1000); // refresh every 1000 milliseconds
/* And this would poll every 1 second as it says above */
サーバー側: (checkactivity.php)
<?php
checkactivity();
function checkactivity() {
$querystats = "SELECT activity.id, activity.buttonid, activity.dateposted FROM activity ORDER BY id DESC LIMIT 1";
$resultstats = mysqli_query($yourdbhandle,$querystats);
$num_stats = mysqli_num_rows($resultstats);
$rowactivity = mysqli_fetch_assoc($resultstats);
if($num_stats > 0) { /* If there was activity */
$activity_id = $rowactivity["id"];
$activity_date = $rowactivity["dateposted"]; //I can't indent this here for some reason.
$activity_buttonid = $rowactivity["buttonid"];
$timeactivity = strtotime( "$activity_date" );
$actualtime = time(); //The actual time in a timestamp
$timetoseconds = $actualtime - $timeactivity; //Actual time - timeactivity
if($timetoseconds < 6) { /* If the last activity was sent less than 6 seconds ago */
echo "$activity_buttonid"; //This would send the last clicked button id
}
}
}
?>
これは最良の方法ではありませんが、代わりに簡単な方法です。最良の方法は、webSockets または node.JS を使用して、データベースを何度もポーリングすることを避け、サーバーが検出した場合にのみ結果をクライアントに送信することです。アップデート。これはドラフトであり、その方法についての簡単な説明です。