deepson2 Posted January 2, 2010 Share Posted January 2, 2010 Hello, I was just wondering how the twitters new post comes without even refreshing the page. see this http://juitter.com/ I just wanted to ask that can i apply it for really time application also apart from twitters new post? Suppose i am showing recent visitors on profile page.The most recent visitor would be "1 min ago".If suddenly new user comes so i just want to append that user with the message "about a min ago" without refresh like done there on the about site.. Can anyone please tell me whether is a possible or not . if yes, then what things i need to consider? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/ Share on other sites More sharing options...
deepson2 Posted January 2, 2010 Author Share Posted January 2, 2010 ok, here is the following code, where page is refreshing after some time <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/ libs/jquery/1.3.0/jquery.min.js"></script><script type="text/javascript"> function getRandom() { $("#random").hide("slow"); $("#random").load("http://www.google.co.uk", '', callback); } function callback() { $("#random").show("slow"); setTimeout("getRandom();", 10000); } $(document).ready(getRandom); </script> </head> <body> <h1>Test</h1> <div id="random"> <?php $link = mysql_connect('###', '###', '##') or die('Could not connect: ' . mysql_error()); //echo 'Connected successfully'; mysql_select_db('##') or die('Could not select database'); $sql = mysql_query("Select ename from table1"); $record_count=mysql_num_rows($sql); //Display count......... if(mysql_num_rows($sql) > 0){ while($row = mysql_fetch_array($sql)){ echo $row['ename']; echo "<br/>"; } } echo $record_count; ?> </div> </body> </html> getting the name like this- asha rahul vineet meera aniket pankaj I don't want to refresh this page until and unless the new name wont get added into my list. I mean how we can check with this using JS that div should get refreshed only when the new record is inserted into the database? please help me. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987085 Share on other sites More sharing options...
trq Posted January 2, 2010 Share Posted January 2, 2010 Considering your already using jQuery, you should take a look at there Ajax methods. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987086 Share on other sites More sharing options...
deepson2 Posted January 2, 2010 Author Share Posted January 2, 2010 Thanks for your reply. but that link didn't give me any direction for my this question -> how we can check with this using jquery that refresh div only when the new record is inserted into the database? Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987090 Share on other sites More sharing options...
Psycho Posted January 3, 2010 Share Posted January 3, 2010 As Thorpe suggested, you need to look into AJAX. Simply create a JavaScript timed event that makes an AJAX request every n seconds. That request will return back the current content to display. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987700 Share on other sites More sharing options...
deepson2 Posted January 4, 2010 Author Share Posted January 4, 2010 As Thorpe suggested, you need to look into AJAX. Simply create a JavaScript timed event that makes an AJAX request every n seconds. That request will return back the current content to display. Thanks for your reply mjdamato , Could you please elaborate more? As you mean i should take one php page(lets say result.php) and i need to refresh this page after n secondand i want to show that result on another page(lets say index.php) so you think this ll work. is that something you are suggesting? Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987955 Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 I'm not sure you read the link I provided. Ajax will do what your after and jQuery ,makes it easy to use Ajax. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-987957 Share on other sites More sharing options...
deepson2 Posted January 4, 2010 Author Share Posted January 4, 2010 I have been searching around but not able to find any related link on this.can anyone please give me any similar link so i can check it and ll get some idea probably. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-988041 Share on other sites More sharing options...
deepson2 Posted January 4, 2010 Author Share Posted January 4, 2010 ok, i want to do something like this- 1)Get one page where my select query result is there. 2)Refresh(or check) it in every 2 sec, and if new result is exactly the same as previous result don't do anything. 3)If you get new entry(record) result then show(refresh) it immediately without refreshing the page . But i really really don't know how i can do this with AJAX. Can anyone please guide me in right direction.? Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-988054 Share on other sites More sharing options...
trq Posted January 4, 2010 Share Posted January 4, 2010 Take a look here. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-988070 Share on other sites More sharing options...
deepson2 Posted January 4, 2010 Author Share Posted January 4, 2010 Thanks a lot i wanted something similar but the problem is if i have following result for the first time i get the following o/p- The data returned from the server was: asha rahul vineet meera aniket pankaj Ameya jitu Now if i added new name "sachin" it gives me me result like this The data returned from the server was: asha rahul vineet meera aniket pankaj Ameya jitu The data returned from the server was: asha rahul vineet meera aniket pankaj Ameya jitu sachin it should append only sachin not the entire query again. whats the problem can anyone please tell me. and i was trying something to do this without Jquery also. Here is the code which i found on the net <html> <head> <script> /* Auto Refresh Page with Time script By JavaScript Kit (javascriptkit.com) Over 200+ free scripts here! */ //enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59 // now we are considering the time here is 30 sec. var limit="0:30" if (document.images) { var parselimit=limit.split(":") parselimit=parselimit[0]*60+parselimit[1]*1 } function beginrefresh(){ if (!document.images) return if (parselimit==1) window.location.reload() else{ parselimit-=1 curmin=Math.floor(parselimit/60) cursec=parselimit%60 if (curmin!=0) curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!" else curtime=cursec+" seconds left until page refresh!" window.status=curtime setTimeout("beginrefresh()",1000) } } window.onload=beginrefresh </script> </head> <body> <center><h1>****Test****</h1></center> <div id="random"> <?php ini_set( "display_errors", 0); ini_set("display_errors",0); $link = mysql_connect('****', '***', '***') or die('Could not connect: ' . mysql_error()); //echo 'Connected successfully'; mysql_select_db('***') or die('Could not select database'); $sql = mysql_query("Select ename from table1"); $record_count=mysql_num_rows($sql); //Display count......... if(mysql_num_rows($sql) > 0){ while($row = mysql_fetch_array($sql)){ echo $row['ename']; echo "<br/>"; } } @mysql_free_result($sql); //echo $record_count; ?> </div> </body> </html> But problem with this page is, i can actually see the page is getting refreshed though i am not clicking on the refresh button and another thing is i want to refresh only one div not the whole page. Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-988092 Share on other sites More sharing options...
Psycho Posted January 4, 2010 Share Posted January 4, 2010 That is not using AJAX. That code is ...drummroll please... refreshing the page. Which is why you are seeing the page refresh. You need to use a timed event to make an AJAX call. THen use that AJAX call to get the new content and update the contents of the div. A forum is not the appropriate forum in which to teach you how to implement AJAX. There are many tutorials out there on exactly the kind of functionality you are trying to implement. Here is one: http://www.w3schools.com/Ajax/Default.Asp Quote Link to comment https://forums.phpfreaks.com/topic/186906-auto-refrest-with-select-query/#findComment-988168 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.