random quote generator with php, ajax and mysql

Posted by fusion on Stack Overflow See other posts from Stack Overflow or by fusion
Published on 2010-04-07T14:05:26Z Indexed on 2010/04/07 14:23 UTC
Read the original article Hit count: 192

Filed under:
|
|
|
|

i've tried using this code and this to make a random quote generator, but it doesn't display anything. my questions are:

  • what is wrong with my code?

  • in the above tut, the quote is generated on a button click, i'd like a random quote to be displayed every 30 mins automatically. how do i do this?

////////////////////////

quote.html:

<!DOCTYPE html>
<script src="ajax.js" type="text/javascript"></script>
<body>

<!–create the div for the quotes land–>
<div id="quote"><strong>this</strong></div>
<div><a style="cursor:pointer" onclick="run_query();">Next quote …</a></div>

</body>
</html>

/////////////////////

quote.php:

<?php
include 'config.php';

// 'text' is the name of your table that contains
// the information you want to pull from
$rowcount = mysql_query("select count(*) as rows from quotes");

// Gets the total number of items pulled from database.
while ($row = mysql_fetch_assoc($rowcount))
{
 $max = $row["rows"];
}

// Selects an item's index at random 
$rand = rand(1,$max)-1;
$result = mysql_query("select * from quotes limit $rand, 1");

$row = mysql_fetch_array($result);
$randomOutput = $row['storedText'];

echo '<p>' . $randomOutput . '</p>';

////////////

ajax.js:

var xmlHttp


function run_query() {
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("This browser does not support HTTP Request");
return;
} // end if
var url="quote.php";
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
} //end function

function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("quote").innerHTML=xmlHttp.responseText;
} //end if
} //end function

function GetXmlHttpObject() {
var xmlHttp=null;
try {
// For these browsers: Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
//For Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} //end function

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX