I have a news feed that has been developed using PHP, MySQL, and jQuery. The news feed does it’s job by getting new content every 5 seconds. However, this is done by a “refresh” of the complete content. I want to only ADD any new content to the news feed. I want a fade in or scroll effect to be used (jQuery) for this to occur. The items already loaded should stay on the feed and NOT reload with any new content. How can I do this?
Here are my 2 pages and code.
feed.php
<body >
<script>
window.setInterval(function(){
refreshNews();
}, 5000);
</script>
<div id="news"></div>
<script>
function refreshNews()
{
$("#news").fadeOut().load("ajax.php", function( response, status, xhr){
if (status == "error"){
}
else
{
$(this).fadeIn();
}
});
}
</script>
</body>
</html>
ajax.php
<?php require_once('../../Connections/f12_database_connect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
mysql_select_db($database_f12_database_connect, $f12_database_connect);
$query_newsfeed_q = "SELECT * FROM newsfeed ORDER BY pk DESC";
$newsfeed_q = mysql_query($query_newsfeed_q, $f12_database_connect) or die(mysql_error());
$row_newsfeed_q = mysql_fetch_assoc($newsfeed_q);
$totalRows_newsfeed_q = mysql_num_rows($newsfeed_q);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<table border="1">
<tr>
<td>pk</td>
<td>title</td>
<td>content</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_newsfeed_q['pk']; ?></td>
<td><?php echo $row_newsfeed_q['title']; ?></td>
<td><?php echo $row_newsfeed_q['content']; ?></td>
</tr>
<?php } while ($row_newsfeed_q = mysql_fetch_assoc($newsfeed_q)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($newsfeed_q);
?>
I am not quite sure exactly how you have this setup, but it does not seem right:
newsFeed.php
index.php
I have not test this, but this is the overall idea of how it should work. index.php is where the user is going to be, every interval we are going to execute updateNews and this function is in charge of calling newsFeed.php through AJAX. newsFeed.php is in charge of query the database and responding with a json object that contains all the news information. Once updateNews() gets a response from newsFeed.php it will loop through the JSON object and add the content of the response to the div.
Hope that makes sense.