从数据库中将变量加载到我的游戏中的正确方法是什么?
我尝试使用Ajax和Prototype库,但这似乎不起作用。这是我做的:
在我的主要.js游戏文件中......
var rawVocab = new Array();
var optionVocab = new Array();
new Ajax.Request('load_vocab.php', {
onSuccess : function(xmlhttp) {
eval(xmlhttp.responseText);
}
});
'load_vocab.php'看起来像这样......
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$username = "user";
$password = "***************";
try {
$conn = new PDO('mysql:host=localhost;dbname=tygrif_school', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT * FROM vocabulary_game WHERE game_type = :game_type');
$stmt->execute(array('game_type' => 'target'));
$i=0;
while($row = $stmt->fetch()) {
echo "rawVocab[".$i."]['word']='".$row['word']."';";
echo "rawVocab[".$i."]['translation']='".$row['translation']."';";
echo "rawVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
$stmt = $conn->prepare('SELECT * FROM vocabulary_game');
$stmt->execute(array());
$i=0;
while($row = $stmt->fetch()) {
echo "optionVocab[".$i."]['word']='".$row['word']."';";
echo "optionVocab[".$i."]['translation']='".$row['translation']."';";
echo "optionVocab[".$i."]['example_sentence_1']='".$row['example_sentence_1']."';";
$i++;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo '</response>';
?>
是否有一些内置方法可以使用goog库来处理它?</ p>
答案 0 :(得分:0)
显然,Google Closure有一种内置方式(goog.net.XhrIo)来处理Ajax调用。
1 - http://docs.closure-library.googlecode.com/git/class_goog_net_XhrIo.html
2 - http://www.daveoncode.com/2009/11/17/goog-net-xhrio-make-simple-ajax-calls-with-google-closure/