Jump to content

auto refrest with select query


deepson2

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.?

Link to comment
Share on other sites

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.:(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.