Results 1 to 5 of 5

Thread: Script to reload every 10 seconds.

  1. #1
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Script to reload every 10 seconds.

    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)); ?>

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ok:
    ticker.php:
    Code:
    <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:
    Code:
    <?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'];
    }
    ?>
    Jeremy | jfein.net

  3. #3
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Make sure ticker.php, and data.php are in the same directory. If they are, change data.php
    To:
    PHP Code:
    <?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(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.
    Last edited by Nile; 03-02-2009 at 10:41 PM.
    Jeremy | jfein.net

  5. #5
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •