我讀X,從MySQL數據庫Y座標。JSON數組值在JQuery的使用和執行功能
2檔,假裝連接有:coordinate_array和map.php
更新這裏在coordinate_array:我想提出一個多維數組這樣我就可以再使用json_encode($臺)。我只需要Javascript部分的x,y值。
<?php
include 'db_conn.php';
header('Content-Type: application/json');
$select_coordinate_query = "SELECT x_coord, y_coord FROM coordinates";
$result = mysqli_query($conn,$select_coordinate_query);
//see if query is good
if($result === false) {
die(mysqli_error());
}
//array that will have number of desks in map area
$desk = array(); // just added
while($row = mysqli_fetch_assoc($result)){
//get desk array count
$desk[] = array(array("x" => $row['x_coord']),
array("y" => $row['y_coord'])
);
} //end while loop
echo json_encode($desk); //encode array
>
上面的代碼給我此:
[[{ 「×」: 「20」},{ 「Y」: 「20」}],[ { 「×」: 「30」},{ 「Y」: 「30」}],[{ 「×」: 「40」},{ 「Y」: 「40」}],[{ 「×」:」 50「},{」y「:」50「}]]
在map.php中:我想通過使用JQuery來獲得這些值。我想獲取值並運行一個循環,該循環將執行我的Paint函數,該函數將爲表格中的每一行保留繪製矩形。我對JSON和JQuery非常新,並開始使用它。
<canvas id="imageView" width="600" height="500"></canvas>
<script type="text/javascript">
需要幫助請在這裏
//I have no idea how to get the encoded values
$(document).ready(function(){
$.getJSON('coordinate_array.php', function(data)){
$.each(data, function(k,v){
Paint(v[0].x, v[1].y);
});//end each
});//end get json
});//end rdy func
我想執行此功能
//function to paint rectangles
function Paint(x,y)
{
var ctx, cv;
cv = document.getElementById('imageView');
ctx = cv.getContext('2d');
ctx.lineWidth = 5;
ctx.strokeStyle = '#000000';
//x-axis,y-axis,x-width,y-width
ctx.strokeRect(x, y, x+100 , y+100);
}
</script>
預先感謝您它是非常感謝!
在你的'getJson'調用加入這一行'的console.log(數據);'和後輸出。 – Hackerman 2014-09-30 18:03:58
我沒有輸出? – mario 2014-09-30 18:07:41
在你的Javascript控制檯(Chrome中的F12)...那裏你應該能夠看到輸出... – Hackerman 2014-09-30 18:08:49