{"id":2308,"date":"2020-09-11T12:01:46","date_gmt":"2020-09-11T17:01:46","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2308"},"modified":"2026-08-20T17:27:22","modified_gmt":"2026-08-20T22:27:22","slug":"show-new-records-on-the-list-page-automatically","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2020\/09\/11\/show-new-records-on-the-list-page-automatically\/","title":{"rendered":"Automatically Show New Records on a PHPRunner List Page"},"content":{"rendered":"<p>In multi-user applications, new records may be added while another user is already looking at the List page. A helpdesk is a typical example: customers submit tickets while support staff keep the ticket list open and need to see new items quickly.<\/p>\n<p>This example periodically checks whether new records were added. When new data is detected, the page can either reload automatically or display a notification. Newly arrived records are also highlighted so they are easy to notice.<\/p>\n<p>\n<a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_record_notification.png\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_record_notification-300x217.png\" alt=\"PHPRunner notification showing new records on a List page\" width=\"300\" height=\"217\" class=\"alignnone size-medium wp-image-2309\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_record_notification-300x217.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_record_notification-768x557.png 768w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2020\/09\/scr_record_notification.png 996w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><br \/>\n<\/a>\n<\/p>\n<p><!--more--><\/p>\n<h2>How it works<\/h2>\n<p>The example stores the current number of records in a session variable. JavaScript then sends a small request to the List page at regular intervals. If the number of records has increased, the page knows that new data is available.<\/p>\n<p>This approach is simple and works well for pages where new records normally appear at the top. It is a good idea to sort the data by an ID, timestamp or another appropriate field in descending order so newly added records appear first.<\/p>\n<p><strong>Note:<\/strong> This technique compares the current number of rows with the previous count. Searches, filters or other conditions that change the number of rows can therefore also affect the result.<\/p>\n<h2>1. List page: Before Display event<\/h2>\n<p>Add the following code to the <strong>List page: Before Display<\/strong> event.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\nif(postvalue(\"a\") === \"check_new\" ){\r\n    $_SESSION[\"new_records\"] = 0;\r\n\r\n    if($pageObject->numRowsFromSQL > $_SESSION[\"current_count\"]){\r\n        $_SESSION[\"new_records\"] =\r\n            $pageObject->numRowsFromSQL - $_SESSION[\"current_count\"];\r\n    }\r\n\r\n    print json_encode(array(\r\n        \"action\" => $_SESSION[\"new_records\"] > 0 ? \"update\" : \"\"\r\n    ));\r\n\r\n    exit();\r\n}\r\n\r\n$_SESSION[\"current_count\"] = $pageObject->numRowsFromSQL;\r\n\r\n$pageObject->setProxyValue(\r\n    \"new_records\",\r\n    isset($_SESSION[\"new_records\"]) ? $_SESSION[\"new_records\"] : 0\r\n);\r\n\r\nunset($_SESSION[\"new_records\"]);<\/textarea><\/pre>\n<\/div>\n<p><strong>C#<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\ndynamic action = MVCFunctions.postvalue(\"a\");\r\n\r\nif(action == new XVar(\"check_new\")){\r\n    XSession.Session[\"new_records\"] = 0;\r\n\r\n    if(pageObject.numRowsFromSQL > XSession.Session[\"current_count\"]){\r\n        XSession.Session[\"new_records\"] =\r\n            pageObject.numRowsFromSQL - XSession.Session[\"current_count\"];\r\n    }\r\n\r\n    dynamic info = XVar.Array();\r\n\r\n    info.InitAndSetArrayItem(\r\n        XSession.Session[\"new_records\"] > 0 ? \"update\" : \"\",\r\n        \"action\"\r\n    );\r\n\r\n    MVCFunctions.Echo(MVCFunctions.my_json_encode((XVar)(info)));\r\n    MVCFunctions.Exit();\r\n}\r\n\r\nXSession.Session[\"current_count\"] = pageObject.numRowsFromSQL;\r\n\r\npageObject.setProxyValue(\r\n    \"new_records\",\r\n    XSession.Session.KeyExists(\"new_records\")\r\n        ? XSession.Session[\"new_records\"]\r\n        : new XVar(0)\r\n);\r\n\r\nXSession.Session.Remove(\"new_records\");<\/textarea><\/pre>\n<\/div>\n<p>The first time the page loads, the current row count is saved in the session. The value is also passed to JavaScript through a proxy value so the page can highlight newly added rows after a reload.<\/p>\n<h2>2. List page: JavaScript OnLoad event<\/h2>\n<p>Add the following code to the <strong>List page: JavaScript OnLoad<\/strong> event.<\/p>\n<p>The <strong>autoreload<\/strong> variable controls what happens when new records are detected. Set it to <strong>true<\/strong> to reload the page automatically, or <strong>false<\/strong> to display a message with a reload link.<\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"javascript\" name=\"mshighlighter\" >\r\nvar autoreload = false;\r\nvar new_row_color = \"#FF7373\";\r\n\r\nvar message = $(\"<div class='alert alert-success'>There is new data available. <a href=''>Reload page<\/a><\/div>\");\r\n\r\nif (proxy[\"new_records\"] > 0) {\r\n    var new_rows = pageObj.getAllRecords().splice(\r\n        0,\r\n        proxy[\"new_records\"]\r\n    );\r\n\r\n    $.each(new_rows, function(index, row) {\r\n        $(\"#gridRow\" + row.row.id).css(\r\n            \"background-color\",\r\n            new_row_color\r\n        );\r\n    });\r\n}\r\n\r\nfunction check_new() {\r\n    $.get(\"\", { a: \"check_new\" }, function(response) {\r\n        var data = JSON.parse(response);\r\n\r\n        if (data.action === \"update\") {\r\n            if (autoreload) {\r\n                window.location.reload();\r\n            } else {\r\n                $(\".r-grid\").before(message);\r\n            }\r\n        }\r\n    });\r\n}\r\n\r\nvar seconds = 10;\r\nsetInterval(check_new, seconds * 1000);<\/textarea><\/pre>\n<\/div>\n<p>The example checks for new data every 10 seconds. You can change the value of <strong>seconds<\/strong> to match your application.<\/p>\n<p>Choose the interval carefully. A shorter interval makes updates appear faster, but it also creates more requests to the server. If many users keep the same List page open, checking every second or two can create unnecessary load. For most applications, an interval of several seconds or longer is sufficient.<\/p>\n<p>You can also change <strong>new_row_color<\/strong> to use a different highlight color.<\/p>\n<h2>3. Inline Add<\/h2>\n<p>If users add records with <strong>Inline Add<\/strong> on the same List page, update the stored count after a record is added. Otherwise, the record added by the current user could be detected as a new record during the next check.<\/p>\n<p>Add the following code to the <strong>Add page: After Record Added<\/strong> event.<\/p>\n<p><strong>PHP<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"php\" name=\"mshighlighter\" >\r\nif($inline){\r\n    $_SESSION[\"current_count\"]++;\r\n}<\/textarea><\/pre>\n<\/div>\n<p><strong>C#<\/strong><\/p>\n<div class=\"my-syntax-highlighter\">\n<pre><textarea id=\"mshighlighter\" class=\"mshighlighter\" language=\"\" name=\"mshighlighter\" >\r\nif(inline){\r\n    XSession.Session[\"current_count\"]++;\r\n}<\/textarea><\/pre>\n<\/div>\n<h2>Things to keep in mind<\/h2>\n<p>This technique is designed for cases where the number of records is a useful indication that new data has arrived. It works especially well for queues, helpdesks, orders, messages and similar pages where new records are normally inserted and displayed at the top.<\/p>\n<p>If users frequently apply searches or filters, or if records are also being deleted, the total row count can change for reasons other than a new record being added. In more complex applications, you may want to track the newest record ID or timestamp instead of relying only on the total number of rows.<\/p>\n<p>The polling interval is another important consideration. Use the longest interval that still provides the responsiveness your users need. This reduces unnecessary requests while still keeping the List page reasonably current.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In multi-user applications, new records may be added while another user is already looking at the List page. A helpdesk is a typical example: customers submit tickets while support staff keep the ticket list open and need to see new items quickly. This example periodically checks whether new records were added. When new data is detected, the page can either reload automatically or display a notification. Newly arrived records are also highlighted so they are easy to notice.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,10,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2308"}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2308"}],"version-history":[{"count":9,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2308\/revisions"}],"predecessor-version":[{"id":3454,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2308\/revisions\/3454"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}