将 PHP 数组返回到 Javascript 数组

发布于 2024-10-11 20:49:08 字数 1458 浏览 2 评论 0原文

我正在尝试将 SQL 查询数组返回到 JavaScript 数组中,然后一次显示一个信息。我已经在这里找到了一些有用的帖子,但我仍然无法让它工作。我是 ajax 新手,所以请原谅任何愚蠢的错误。下面是 php,后面是说明。 php:这是在index.php 的外部文件中,

<?php
include('connection.php');

$query = "SELECT * FROM photos";
$queryresult = mysql_query($query);

while ( $line = mysql_fetch_array($result) ) {
    $path[] = $row[0];
}
$paths = json_encode($path);
header('Content-type: application/json');
echo $paths;
?>

它获取结果(它们是文件路径)数组,并对它们进行json 编码以传递给javascript。 Connection.php 是正确的并且正在运行。

HTML/Javascript:

<html>
<head>
<script src="JavaScript/gjs.js" type="text/javascript"></script>
<script src="jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script>
function imageload(){
var i = 1;
$.getJSON('phpfiles/getpiccode.php', function(data) {

    var can = document.getElementById('canvas').getContext('2d');

    $.each(data,function(idx, row){
    var img = new Image();
    img.onload = function(){
        can.drawImage(img, 0, 0, 1280, 800);
    }
    img.src = row;
            i++;
});

});

}
</script>
</head>

<body>
<div class="container">
<canvas id="canvas" class="canvas-one" width="1280" height="800">
<script>imageload();</script>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

</body>
</html>

我希望这是有道理的。再次感谢!

I am trying to return my SQL query array into a javascript array and then display the info one at a time. I have found a few helpful posts already on here but I still cannot get it to work. I am new to ajax and so please forgive any stupid mistakes. Below is the php followed by a description.
php: this is in an external file from index.php

<?php
include('connection.php');

$query = "SELECT * FROM photos";
$queryresult = mysql_query($query);

while ( $line = mysql_fetch_array($result) ) {
    $path[] = $row[0];
}
$paths = json_encode($path);
header('Content-type: application/json');
echo $paths;
?>

This gets the results (they are file paths) array and json encodes them to pass to javascript. Connection.php is correct and it is working.

HTML/Javascript:

<html>
<head>
<script src="JavaScript/gjs.js" type="text/javascript"></script>
<script src="jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script>
function imageload(){
var i = 1;
$.getJSON('phpfiles/getpiccode.php', function(data) {

    var can = document.getElementById('canvas').getContext('2d');

    $.each(data,function(idx, row){
    var img = new Image();
    img.onload = function(){
        can.drawImage(img, 0, 0, 1280, 800);
    }
    img.src = row;
            i++;
});

});

}
</script>
</head>

<body>
<div class="container">
<canvas id="canvas" class="canvas-one" width="1280" height="800">
<script>imageload();</script>This text is displayed if your browser does not support HTML5 Canvas</canvas>
</div>

</body>
</html>

I hope that makes sense. Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独木成林 2024-10-18 20:49:08

使用 json_encode() 将其编码为 JSON。
在最近的浏览器中,您可以简单地使用 var obj = JSON.parse(yourstring); 将该函数中的字符串转换为 JavaScript 对象 - 但最好使用例如 jQuery 进行 AJAX 和 JSON 解析。

更新:您的 JavaScript 应该看起来像这样,以迭代查询中的数据:

$.getJSON('phpfiles/getpiccode.php', function(data) {
    // get canvas object. it's the same for all images so only do it once
    var can = document.getElementById('canvas').getContext('2d');
    // iterate over the elements in data
    $.each(data, function(idx, row) {
        var img = new Image();
        img.onload = function() {
            can.drawImage(img, 0, 0, 1280, 800);
        }
        img.src = row;
    });
});

但是,它可能不会执行您想要的操作:它将几乎同时在同一位置绘制所有图像。

还要将 (假设这是包含 JavaScript 的函数)替换为 因为这是正确/正确的语法。

在 PHP 代码中,您必须将 return $paths; 替换为 echo $paths; ,除非您使用的框架依赖于文件返回某些内容。另外,最好发送一个 JSON 标头: header('Content-type: application/json');

PS: SELECT *MYSQL_NUM< 结合使用/code> 是一件坏事。它依赖于表中具有特定顺序的列。如果您只需要一列,请使用“SELECT columnName”;如果您需要全部,请使用 MYSQL_ASSOC 来获取关联数组。

Use json_encode() to encode it as JSON.
In recent browsers you can simply turn the string from that function into a JavaScript object using var obj = JSON.parse(yourstring); - but better use e.g. jQuery for AJAX and JSON parsing.

Update: Your JavaScript should looke like that to iterate over the data from your query:

$.getJSON('phpfiles/getpiccode.php', function(data) {
    // get canvas object. it's the same for all images so only do it once
    var can = document.getElementById('canvas').getContext('2d');
    // iterate over the elements in data
    $.each(data, function(idx, row) {
        var img = new Image();
        img.onload = function() {
            can.drawImage(img, 0, 0, 1280, 800);
        }
        img.src = row;
    });
});

However, it might not do what you want: It will draw all images pretty much at once at the same position.

Also replace <script>imageload() </script> (assuming that's the function containing your JavaScript) with <script type="text/javascript">imageload();</script> as that's the correct/proper syntax.

In your PHP code you'll have to replace return $paths; with echo $paths; unless you are using some framework which relies on your file returning something. Additionally it'd be good to send a JSON header: header('Content-type: application/json');

PS: SELECT * combined with MYSQL_NUM is a BadThing. It relies on the columns in the table having a certain order. If you just need one column use "SELECT columnName"; if you need all, use MYSQL_ASSOC to get an associative array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文