jbonnett Posted May 1, 2012 Share Posted May 1, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/ Share on other sites More sharing options...
jbonnett Posted May 3, 2012 Author Share Posted May 3, 2012 No one? Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/#findComment-1342857 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2012 Share Posted May 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/#findComment-1342866 Share on other sites More sharing options...
Barand Posted May 3, 2012 Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/#findComment-1342868 Share on other sites More sharing options...
jbonnett Posted May 3, 2012 Author Share Posted May 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/#findComment-1342875 Share on other sites More sharing options...
Barand Posted May 4, 2012 Share Posted May 4, 2012 you can stop it with clearInterval() http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/ Quote Link to comment https://forums.phpfreaks.com/topic/261909-fetch-new-result-if-new-rows-inserted/#findComment-1342956 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.