Jump to content

Recommended Posts

I cant work out how I could fetch new result when a new row or more has been inserted into the database and only fetch the new rows

I'm trying to do a news feed like what facebook has and there it does this, the news feed updates the results without refreshing the page when a friend adds a status any ideas?

Link to comment
https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/
Share on other sites

You need to keep track of the id of the last item you have displayed. Then, you simply query for items having an id greater than that value. You would either store the last id in a session variable or carry it on the of the url as a get parameter.

AJAX is the answer to this problem.

 

Here is a simple example.

 

Your html page will need a javascript function to call the xmlhttp request to myfeeds.php script and put results on the page. A javascript timer call setInterval() is used to call the function every 5 seconds.

 

the html page

<html>
<head>
<meta name="generator" content="PhpED Version 4.6.3 (Build 4635)">
<title>sample feed</title>
<meta name="author" content="barand">

<script type="text/javascript">
    function getFeeds()
    {
        var xmlhttp = new XMLHttpRequest();
        var obj = document.getElementById('feeds');
        var tm = new Date();
        var tstr = tm.getTime();    // prevent caching
        
        var url = "myfeeds.php?dummy=" + tstr;
        xmlhttp.open("GET" , url , true);
        xmlhttp.onreadystatechange=function() {
          if (xmlhttp.readyState==4) {
              if (xmlhttp.responseText != '') {
                obj.innerHTML = xmlhttp.responseText;
              } else {
                obj.innerHTML = "No new feeds";
              }

          }
        }
        xmlhttp.send(null);

    }
    
    setInterval("getFeeds()", 5000);           // call getFeeds() every 5 seconds;

</script>

</head>
<body>
Some content here<br /><br />
Newsfeeds:<br />
<div id='feeds' style='border: 1px solid gray; margin: 20px; padding: 10px'>
      <!-- feeds will go here  -->
      No new feeds
</div>
</body>
</html>

 

the php script "myfeeds.php"

<?php
include("testDBconnect.php"); //connect to database

$sql = "SELECT posted, item
        FROM newsfeed
        WHERE posted > NOW() - INTERVAL 1 HOUR
        ORDER BY posted DESC";
$res = mysql_query ($sql);

if (mysql_num_rows($res) == 0) {
    exit('');
}

while (list ($t, $i) = mysql_fetch_row($res)) {
    echo "<p><span style='color:blue;'>" . date('g:i a', strtotime($t)) . "</span><br />$i</p>";
}
?>

 

the newsfeed table

CREATE TABLE `newsfeed` (
  `idnewsfeed` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `item` varchar(245) NOT NULL DEFAULT '',
  `posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`idnewsfeed`)
)

 

Hope it helps

Thank you i'm not very good with AJAX/ Jquery is there any way this code can be modified to become inactive after a set amount of time and then when the results div is hovered over the inactive time becomes active again.

 

Thanks Jamie

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.