i would like to ask how can i make a php script which echoes the id of the data stored in a database repeat itself after specific time for example after 2 minutes.i don't want to use a cron job or another scheduler.Just php or javascript implemantation.Thanks in advance..
Asked
Active
Viewed 636 times
1
-
1why do you not want to use a cron job? – Derek Downey Jan 12 '11 at 00:33
-
2if you just need the page to auto-reload: [meta refresh](http://en.wikipedia.org/wiki/Meta_refresh). If that's not what you need, please clarify your question. – drudge Jan 12 '11 at 00:35
-
Honestly i don't know how to use it.But this not the reason!!!i prefer to implement that in php or javascript... – olaf36 Jan 12 '11 at 00:36
-
Well the script i want to repeat chooses randomly the id of rss feed in a database.I want to repeat that script in order after 10 minutes for example a different feed to be selected.The basic idea is to autochoose a feed randomly in order its articles to be shown in a scroller. – olaf36 Jan 12 '11 at 00:40
3 Answers
3
I've done similar with this script. While the user is on the page, it runs scriptToRun.php
every 2 minutes.
function changeFeedAddress() {
$.ajax({
type: 'get',
url: 'scriptToRun.php',
success: function(txt) {
// do something with the new RSS feed ID here
}
});
}
setInterval(changeFeedAddress,120000); // 2 MINUTES

JakeParis
- 11,056
- 3
- 42
- 65
-
1
-
1No problem jquery is ok,too.The scriptToRun.php you mention is the script itself or the name of another script??? – olaf36 Jan 12 '11 at 00:45
-
No, `scriptToRun.php` is another script that lives on the server. It's sole purpose is to get a new feed ID and echo it back. – JakeParis Jan 12 '11 at 00:46
1
Alternate to @JMC Creative (Self-contained for example's sake):
<?php
// check if the $.post below is calling this script
if (isset($_POST['ajax']))
{
// $data = /*Retrieve the id in the database*/;
// ---vvvv---remove---vvvv---
// Example Data for test purposes
$data = rand(1,9999);
// End Example Data
// ---^^^^---remove---^^^^---
// output the new ID to the page so the $.post can see it
echo $data;
exit; // and stop processing
}
?>
<html>
<head>
<title>Demo Update</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
// assign a timeout for this script
var timeout = 2 * 60000; // 2 minutes
// create a function we can call over and over to fetch the ID
function updateDBValue(){
// call this same script and retrieve an "id from the database" (see top of page)
$.post('<?php echo $_SERVER['PHP_SELF']; ?>',{ajax:true},function(data){
// 'data' now contains the new ID. For example's sake, place the value
// in to an input field (as shown below)
$('#db-value').val(data);
// set a timer to re-call this function again
setTimeout(updateDBValue,timeout);
});
}
// call the function initially
updateDBValue();
});
</script>
</head>
<body style="text-align:center;">
<div style="margin: 0 auto;border:1px solid #000;display:block;width:150px;height:50px;">
DB Value:<br />
<input type="text" id="db-value" style="text-align:center;" />
</div>
</body>
</head>

Brad Christie
- 100,477
- 16
- 156
- 200
1
Why not just do this?
<?php
header('refresh: 600; url=http://www.yourhost.com/yourscript.php');
//script here
?>
If your generating your ID at random from within the script...this will work fine. The page will refresh itself every 10 minutes.

Mike L.
- 1,936
- 6
- 21
- 37
-
If it needs to be in order or something use `$_SESSION['id'] = LASTID` when the page refreshes you still have your last id you used – Mike L. Jan 12 '11 at 02:22