[英]Returning 2 tables after a $http.post query in AngularJS
I feel like it's a very simple question but i'm not sure about the syntax. 我觉得这是一个非常简单的问题,但我不确定语法。 I do a $http.post query in my javascript file :
我在我的javascript文件中执行了$ http.post查询:
$http.post('server.php', {"data" : cleTexte, "serverFlag" : 2})
.success(function(data, status)
{
console.log(data);
grantTable = data;
}
The php file is returning a table after a SQL query : php文件在执行SQL查询后返回一个表:
$result = $conn->query("SELECT grantAmount FROM granttoassociation WHERE HEX(grantReceiver) = '$key' ");
while($rs = $result->fetch()) {
if ($outp != "") {
array_push($outp,$rs["grantAmount"]);
}
}
$outp =json_encode($outp);
echo($outp);
And, as you can see, the result is affected to grantTable in the javascript file. 而且,如您所见,结果将影响到javascript文件中的grantTable。 It's working fine, however, i would like to have TWO tables.
它工作正常,但是,我想要两个表。 The second table would be obtained after a second SQL query in the php file.
第二张表将在php文件中进行第二次SQL查询后获得。 The final result in the javascript file should be looking like :
javascript文件中的最终结果应如下所示:
$http.post('server.php', {"data" : cleTexte, "serverFlag" : 2})
.success(function(data, status)
{
console.log(data);
grantTable = data...
grantYear = data...
}
Houw should i do that ? 我该怎么办?
The second SQL query would be something like : 第二个SQL查询将类似于:
"SELECT grantYear FROM granttoassociation WHERE HEX(grantReceiver) = '$key' "
The basic idea is to run something like 基本思路是运行类似
"SELECT grantYear, grantAmount FROM granttoassociation WHERE HEX(grantReceiver) = 'yourkey' "
Beware SQL injection though and use PDO to pass the parameters or mysqli_ . 不过要当心SQL注入,并使用PDO传递参数或mysqli_ 。
In your javascript you can iterate data and .push
the respective columns to the object you want to hold it in 在您的JavaScript中,您可以迭代数据,然后将相应的列
.push
送到您要保存在其中的对象
function(data, status)
{
console.log(data);
grantTable = [];
grantYear = [];
for (var index in data) {
grantTable.push(data[index]["grantAmount"]);
grantYear.push(data[index]["grantYear"]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.