mySql、PHP、jquery Web アプリに取り組んでいます。
jQuery スクリプトが .php スクリプトから取得したデータで正常に動作するようになりましたが、mySQL データベースから .php スクリプトでそのデータを取得すると、ページの値が定義されていません。デシベルから。
phpはこちら。
$get_par ="SELECT hole, par FROM course_card cc
LEFT JOIN tour_event_rounds ter ON ter.course_id = cc.course_id
WHERE tour_id='$tid' AND rnd='$rnd' limit 9";
$gp_res = mysql_query($get_par) or die(mysql_error());
$golfer_id=25;
$score_arr[$golfer_id]["name"]="Bob Golfer";
while ($array = mysql_fetch_array($gp_res)) {
$hole = $array['hole']+0;
$score_arr[$golfer_id]["$hole"] = $array['par']+0;
}
$golfer_id=45;
$score_arr[$golfer_id]["name"]="Joe Golfer";
$score_arr[$golfer_id][1]=4;
$score_arr[$golfer_id][2]=3;
$score_arr[$golfer_id][3]=5;
$score_arr[$golfer_id][4]=5;
$score_arr[$golfer_id][5]=5;
$score_arr[$golfer_id][6]=5;
$score_arr[$golfer_id][7]=5;
$score_arr[$golfer_id][8]=5;
$score_arr[$golfer_id][9]=5;
echo json_encode($score_arr);
上記のphpスクリプトからエンコードされたJSON配列はこれを出力します
{"25":
{"name":"Bob Golfer","1":4,"2":3,"3":4,"4":5,"5":4,"6":5,"7":4,"8":4,"9":3},
"45":
{"name":"Joe Golfer","1":4,"2":3,"3":5,"4":5,"5":5,"6":5,"7":5,"8":5,"9":5}
}
ご覧のとおり、Bob のデータ ホール データは、JSON 配列の Joe と同じです。
しかし、js jQuery スクリプトを実行すると。ボブのデータは未定義ですが、ジョーのデータは問題ありません。どうすればいいの??
ここにjsがあります
<?php
$hole=1;
$grp=$_REQUEST['grp'];
$tid=$_REQUEST['tid'];
?>
<script type="text/javascript">
tid= <?php echo "$tid"; ?>+0;
grp= <?php echo "$grp"; ?>+0;
rnd= <?php echo "$rnd"; ?>+0;
hole = <?php echo "$hole"; ?>+0;
gid=25;
var score_arr ;
var total = new Array() ;
var total_f = new Array() ;
var total_b = new Array() ;
$(document).ready(function(){
// SET the intial values
$("#hole_num").text(hole);
// Get Data from db
$.getJSON("gse_db_int.php",{"tid":tid,"grp":grp,"rnd":rnd}, function(data){
console.log(data); //uncomment this for debug
$("#score").text(data[gid][hole]);
$("#golfer_name").text(data[gid]["name"]);
score_arr= data;
}); //End json
// show the right view
$("table#one_nine").show();
$("table#ten_plus").hide();
$("table#score_entry").show();
$("table#score_summary").hide();
$("#next_hole").click(function(){
hole=hole+1;
$("#hole_num").text(hole);
$("#score").text(score_arr[gid][hole]);
$("#golfer_name").text(score_arr[gid]["name"]);
});
$("#prev_hole").click(function(){
hole=hole-1;
$("#hole_num").text(hole);
$("#score").text(score_arr[gid][hole]);
});
$(".score_button").click(function() {
score_arr[gid][hole]= parseInt(this.value);
$("#score").text(score_arr[gid][hole]);
});
$("#but_ten_plus").click(function() {
$("table#one_nine").hide();
$("table#ten_plus").show();
});
$("#but_one_nine").click(function() {
$("table#one_nine").show();
$("table#ten_plus").hide();
});
$("table#tab_head").click(function() {
score_summary();
$("table#score_entry").hide();
$("table#score_summary").show();
});
$("tr#sum_head").click(function() {
$("table#score_entry").show();
$("table#score_summary").hide();
});
// build the score summary
function score_summary(){
$("#plug").text("");
$.each(score_arr,function(gid,i){
console.log(gid);
console.log(score_arr[gid]["name"]);
total[gid]=0;
total_f[gid]=0;
total_b[gid]=0;
$("#plug").append('<td> '+score_arr[gid]["name"]+' </td> ');
//FRONT
for(hol=1; hol <= 9 ; hol++){
$("#plug").append('<td> '+score_arr[gid][hol]+' </td> ');
console.log(score_arr[gid][hol]);
total_f[gid]= total_f[gid]+ score_arr[gid][hol];
}
$("#plug").append('<td> '+total_f[gid]+' </td> ');
//BACK
for(hol=1; hol <= 9 ; hol++){
$("#plug").append('<td> '+score_arr[gid][hol]+' </td> ');
console.log(score_arr[gid][hol]);
total_b[gid]= total_b[gid]+ score_arr[gid][hol];
}
$("#plug").append('<td> '+total_b[gid]+' </td> ');
total[gid]= total_f[gid]+total_f[gid];
$("#plug").append('<td> '+total[gid]+' </td> ');
$("#plug").append('</tr> <tr>');
});
}
});
</script>
デイブ