PDA

View Full Version : Script to reload every 10 seconds.



javrana
02-27-2009, 03:55 PM
Not sure if this should go in javascript or PHP forum. I have a PHP script that pulls 1 random record from MYSQL database on my website. What I would like for it to do is every 10 seconds pull another random record. I can't do it with PHP alone, and I was told I could use javascript to effectively reload the PHP script every 10 seconds, causing the random record it is pulling to change. Any ideas on how to write this code? Here is my PHP code:

<?php require_once('../Connections/john.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_john, $john);
$query_listing = "SELECT * FROM testimonials_display ORDER BY RAND() LIMIT 1
";
$listing = mysql_query($query_listing, $john) or die(mysql_error());
$row_listing = mysql_fetch_assoc($listing);
$totalRows_listing = mysql_num_rows($listing);


And this is the code in the body of the document:

<?php do { ?>
<p>&nbsp;<?php echo $row_listing['comment']; ?></p>
<hr>
<?php } while ($row_listing = mysql_fetch_assoc($listing)); ?>

Nile
02-27-2009, 11:46 PM
Ok:
ticker.php:


<style type="text/css">
.ticker {
padding: 10px;
border: 1px solid #000;
background: #A7FF84;
width: 150px;
font-family: arial;
font-size: 12px;
}
</style>
<script type="text/javascript">
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
var init_ticker = function(){
xmlHttp.open("GET", "data.php", true);
xmlHttp.send(null);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){
document.getElementById("special_ticker").innerHTML = xmlHttp.responseText;
}
};
};
setInterval(init_ticker, 10000);
window.onload = init_ticker;
</script>
<div class="ticker" id="special_ticker">Loading...</div>


Data.php:


<?php
require_once('../Connections/john.php')
mysql_select_db($database_john, $john);

$query = "SELECT * FROM testimonials_display ORDER BY RAND() LIMIT 1";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)){
echo $row['comment'];
}
?>

javrana
03-02-2009, 05:19 PM
Can't seem to get this to work. The ticker just says "loading..." and doesn't do anything else. Not sure if it's not connecting to my database or what.

Nile
03-02-2009, 10:31 PM
Make sure ticker.php, and data.php are in the same directory. If they are, change data.php
To:


<?php
require_once('../Connections/john.php')
mysql_select_db($database_john, $john) or die(mysql_error());

$query = "SELECT * FROM testimonials_display ORDER BY RAND() LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){
echo $row['comment'];
}
?>


If you would like to see a working example, go here (http://beta.unlinkthis.net)(my site), and in the sidebar you should see it. (except that one is supposed to refresh every 2 seconds ;))

If the above script doesnt work, please provide the problematic page, so we can see whats happening. ;)

javrana
03-12-2009, 01:51 PM
site for code:

http://www.arklacomp.com/site_flash/index.php