duanqiao1926 2014-09-30 21:22
浏览 69

JQuery没有读取JSON数组值

I’m reading X,Y Coordinates from MySQL Database.

2 files(I can post the connection file if needed): coordinate_array, and map.php

coordinate_array: I am making a multidimensional arrays so I can then use json_encode($desk). I only need x,y values for the JS/Jqueryt part.

updated PHP code and JQuery CODE.. still does not work!?

    <?php
          include 'db_conn.php';

    header('Content-Type: application/json');

    $coordinate_sql = "SELECT x_coord, y_coord FROM coordinates";

    $result = mysqli_query($conn,$coordinate_sql);

    //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('x' => $row['x_coord'],                                 
                        'y' => $row['y_coord']);
    } //end while loop
          echo json_encode($desk);  //encode array
?>

The code above gives me this as the result for the array:

[{"x":"20","y":"20"},{"x":"30","y":"30"},{"x":"40","y":"40"},{"x":"50","y":"50"}]

map.php : This is the part where is not entirely working. I'm trying to get the values from json_encode with JQuery but I get no output on screen.

Don't know which of these two I should use

 <script type="text/javascript" src="jquery-1.11.1.min.js"></script> 
 <script  src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

Need help here please

 <canvas id="imageView" width="600" height="500"></canvas>          
 <script type="text/javascript">

                //I have no idea how to get the encoded values
    $.post('coordinate_array.php', {}, function(data) {
    results = JSON.parse(data);
        for(i = 0;i < results.length;i++) {
            Paint(results[i].x, results[i].y);
        }
    });

I want to execute this function

        //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>
  • 写回答

2条回答 默认 最新

  • dpblwmh5218 2014-09-30 21:29
    关注

    The JSON formal definition tends to have an object (or, key-associative-array: {}) as the root of any JSON object, as opposed to an array ([]).

    To wrap it inside one associative object:

    $deskOutput = array("coords" => $desk);
    echo json_encode($deskOutput);  //encode array
    

    Then, on the JavaScript side, you will just need to get "each(data.coords," and you'll have your array.

    For that matter, it seems like you're putting unnecessary complexity in your array structure at the PHP level. How about this?

    $desk[] = array("x" => $row['x_coord'], "y" => $row['y_coord']));
    

    That will create just one object for each x/y set. I'll leave it to you to figure out how you could simplify your JavaScript accordingly.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?