用PHP,AJAX和MySQL随机引用生成器 [英] random quote generator with php, ajax and mysql

查看:108
本文介绍了用PHP,AJAX和MySQL随机引用生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用这code ,以使一个随机报价生成器,但它不显示任何内容。我的问题是:

  • 什么是错我的code?

  • 在上面啧啧,报价上点击一个按钮生成的,我想 要被显示的随机引用每 自动30分钟。我该怎么办 这个?

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

quote.html:

 <!DOCTYPE HTML>
<脚本SRC =ajax.js类型=文/ JavaScript的>< / SCRIPT>
<身体GT;

<! - 创建分区的报价陆基>
< D​​IV ID =引用><强>该< / STRONG>< / DIV>
< D​​IV><风格=光标:指针的onclick =run_query();>接下来报价...< / A>< / DIV>

< /身体GT;
< / HTML>
 

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

quote.php:

 < PHP
包括config.php文件;

//文本是你的表中包含的名字
你想从拉//信息
$行数=请求mysql_query(SELECT COUNT(*)从报价行);

//获取从数据库中抽取的项目总数。
而($行= mysql_fetch_assoc($行数))
{
 $最大值= $行[行];
}

//选择项目的指数随机
$兰特=兰特(1,$最大值)-1;
$结果= mysql_query(SELECT * FROM报价限制$兰特,1);

$行= mysql_fetch_array($结果);
$ randomOutput = $行['storedText'];

回声'< P> 。 $ randomOutput。 '&所述; / P>';
 

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

ajax.js:

  VAR XMLHTTP


功能run_query(){
XMLHTTP = GetXmlHttpObject();
如果(XMLHTTP == NULL){
警报(此浏览器不支持HTTP请求);
返回;
如果} //结束
VAR URL =quote.php;
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open(GET,URL,真正的);
xmlHttp.send(空);
} //函数结束

传播stateChanged(){
如果(xmlHttp.readyState == 4 || xmlHttp.readyState ==完成){
的document.getElementById(报价)的innerHTML = xmlHttp.responseText。
如果} //结束
} //函数结束

传播GetXmlHttpObject(){
VAR XMLHTTP = NULL;
尝试 {
//对于这些浏览器:Firefox,歌剧8.0+,Safari浏览器
XMLHTTP =新XMLHtt prequest();
}赶上(五){
//对于Internet Explorer
尝试{
XMLHTTP =新的ActiveXObject(MSXML2.XMLHTTP);
}赶上(五){
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
}
返回XMLHTTP;
} //函数结束
 

解决方案

尝试打印值$最大,$兰特和$结果。您可以使用的print_r获得的PHP页面的详细信息。

运行在浏览器上的quote.php来看看你得到的输出。 然后得到阿贾克斯调试。

您可以使用计时器在阿贾克斯您的要求自动每30分钟左右的时间。使用JavaScript的setTimeout函数这一点。

心连心

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

解决方案

Try to print the values for $max,$rand and $result. You can use print_r to get more info from the php page.

Run the quote.php on browser to see if you get an output. Then get to ajax to debug.

You can use a timer in ajax to automate your requests for every 30 mins or so. use javascript's settimeout function for this.

HTH

这篇关于用PHP,AJAX和MySQL随机引用生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆