javascript - 从 PHP 到 JQuery 的 JSON 值并运行函数

标签 javascript php jquery mysql json

您好,我在每段代码之前描述了我遇到的以下问题。提前致谢!!

我正在从 MySQL 数据库读取 X,Y 坐标。 我现在使用 3 个文件:coole_array、db_conn 和 map.php

连接:

<?php 
  //declaring connection 
  $db_user = "u"; 
  $db_pwd = "p"; 
  $db_server = "v"; 
  $db_name = "sandbox"; 
  //1. connection to the database 
  $conn = mysqli_connect($db_server, $db_user, $db_pwd); 
//check connection 
  if(!$conn){ 
   die("Database connection failed: " . mysqli_error()); 
  }else{ 
   echo "connected to MySQL<br>"; 
  } 
// 2. Select a database to use 
  $db_select = mysqli_select_db($conn, $db_name); 
  if (!$db_select) { 
  die("Database selection failed: " . mysqli_error()); 
  } 
 mysqli_close($conn); 
 ?> 

坐标数组:中,我正在制作一个多维数组,这样我就可以绘制所有的矩形 通过我的查询获取,然后我使用 json_encode($desk)。我忽略了表中的coole_id 因为我只需要 Javascript 部分的 x,y 值。

<?php 
 $select_coordinate_query = "SELECT * FROM coordinates";// WHERE coordinate_id ='1' 
 $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
 while($row = mysqli_fetch_assoc($result)){  
  //get desk array count 
  $desk = array( array("x" => $row['x_coord']), 
     array("y" => 
 $row['y_coord']) 
    ); 
  // Frame JSON 
 // Return the x, y JSON here 
 echo json_encode($desk); 
  } //end while loop 
 ?> 

ma​​p.php 中:我试图使用 JQuery 获取这些值。我想获取值并运行 循环将执行我的 Paint 函数,该函数将继续为中的每一行绘制矩形 table 。我对 JSON 和 JQuery 很陌生,并开始使用它。

<div class="section_a" > 
 <p>Section A</p> 
 <canvas id="imageView" width="600" 
 height="500"></canvas> 
  <script type="text/javascript"> 
   $(document).ready(function(){ 
 /* call the php that has the php array which is json_encoded */ 
 $.getJSON('coordinate_array.php', function(data) { 
/* data will hold the php array as a javascript object */ 
 if(data != null){ 
  $.parseJSON(data); 
  $(data).each(Paint(x,y)){ 
 //get values from json 
 //for every row run the functionpaint by passing X,Y coordinate  
 });//end getJson 
}//end if 
}); //end rdy func 
});//end 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> 
 </div> <!-- end div section_a --> 

另外,当包含我的 jquery 文件时,我的语法是否正确......它与 我正在使用的所有其他文件。

我的另一个问题是:在每个文件中包含连接文件并在最后关闭它是否很好 或者在我建立连接的文件中保持连接打开?

预先感谢您,非常感谢!

最佳答案

在 PHP 文件中,您将在 while 循环中输出每个 MySQL 行的 JSON。您可能想要构建一个大对象并在最后将其全部输出。

 //array that will have number of desks in map area
 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);
 exit;

这应该会让你在 JS 中处理这些数据时取得成功。

关于javascript - 从 PHP 到 JQuery 的 JSON 值并运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26126850/

相关文章:

javascript - 在 iFrame 中加载时 Asp.Net 页面抛出错误

javascript - 重定向后获取

javascript - History.js 和同源策略遍历 SSL

PHP 手册 OOP 可见性示例 - 有人可以解释一下吗

javascript - 从浏览器 URL 中删除查询字符串而不重新加载页面

javascript - 0x800a01bd - JavaScript 运行时错误 : Object doesn't support this action - Kendo UI

javascript - 面临有关填充 javascript 数组的问题

php - 如何将评论发布到从数据库获取并使用 while 循环显示的帖子

javascript - 基于 Html5 的加载进度 HUD

javascript - 使用 jquery 检查元素是否存在