I'm using the JavaScript function setInterval every 30 seconds to check the MySQL table with AJAX. Using AJAX it updates the page with new results without reloading the page.
I would like to use the effect highlight to colour certain records, in the example below this highlights ID 1 and 10:
$("#image_li_1").effect("highlight", {}, 25000);
$("#image_li_10").effect("highlight", {}, 25000);
I would like to highlight all new records that have been added since the last load.
index.php
// Run polling function every 60 seconds
var myVar = setInterval(myfunction, 30000);
// Load data from check_status page
function myfunction() {
$.ajax({
url: "check_status.php", success: function(result2) {
$("#div2").html(result2);
$("#title").html("Food Items AUTO Poll");
$("#image_li_1").effect("highlight", {}, 25000);
$("#image_li_10").effect("highlight", {}, 25000);
}
});
}
check_status.php
// Include and create instance of db class
require_once 'DB.class.php';
$db = new DB();
<?php
// Fetch all items from database
$data = $db->getRows();
if (!empty($data)) {
foreach ($data as $row) {
?>
<li id="image_li_<?php echo $row['id']; ?>" class="ui-sortable-handle">
<a href="javascript:void(0);" style="float:none;" class="image_link">
<?php echo $row['name']; ?>
</a>
</li>
<?php
}
}
?>
DB.class.php
<?php
class DB {
// Database configuration
private $dbHost = "###";
private $dbUsername = "###";
private $dbPassword = "###";
private $dbName = "###";
private $itemTbl = "###";
function __construct() {
if (!isset($this->db)) {
// Connect to the database
$conn = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName);
if ($conn->connect_error) {
die("Failed to connect with MySQL: " . $conn->connect_error);
} else {
$this->db = $conn;
}
}
}
// Get rows from data table
function getRows() {
$query = $this->db->query("SELECT * FROM ".$this->itemTbl." ORDER BY img_order ASC");
if ($query->num_rows > 0) {
while ($row = $query->fetch_assoc()) {
$result[] = $row;
}
} else {
$result = FALSE;
}
return $result;
}
check_status.phpto return a JSON response, like something like this[ {"id":0,"name":"..."}, {"id:... ]instead of returning an HTML response. Then in AJAX success function you could do something like this:var array = JSON.parse(result2); for ( var i=0; i < array.length; i++) { $('#div2').append("<li id=.....") }, then you could save the array and compare the old array to the new array so see what has changed.