php - Codeigniter - 链接多个查询结果

标签 php codeigniter

我正在使用 codeigniter 框架创建一个 api。

我已经成功地为各种 API 调用创建了一组响应。

public function test1($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

public function test2($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

所以当我单独运行它们时 - 它返回正常。

例如.. http://localhost/codeigniter/index.php/api/test1/sw1

我的主要问题是尝试将这些不同的 api 调用链接到一个主 api 调用中。所以像这样。

public function master($postcode){
    //run a query

    $response = array(
        $this->test1($postcode),
        $this->test2($postcode)
    );

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response
}

但是当我调用这个 master api 时 例如..http://localhost/codeigniter/index.php/api/master/sw1

我得到了一个奇怪的结果——我得到了两个 json 响应——但随后是一个空数组

{
    "similar-property-avg-sold": [
        [{
            "average": "651164.042021172"
        }]
    ]
} {
    "avg-property-sold-growth": {
        "data": [{
            "year": "2011",
            "average": "448696.91018672835"
        }, {
            "year": "2016",
            "average": "651164.042021172"
        }],
        "difference": 145.12336217118
    }
}
Array([0] => [1] => )

最佳答案

您没有返回前两个子方法的响应...因此主输出将为空...

public function test1($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response

    //return response
    return $response;
}

public function test2($postcode){
    //run a query

    //get a response in an array structure 
    print_r(json_encode($response)); //print the json response

    //return response
    return $response;
}

关于php - Codeigniter - 链接多个查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39268397/

相关文章:

php - 在 float LARAVEL 上调用成员函数 addEagerConstraints()

java - 使用 volley 库获取具有整数的 json

mysql - 在 codeigniter 中使用 $this->db->e​​scape 时如何防止单引号

php - 在 PHP 中自动检测 360 度自然图像

javascript - 如何使用 AJAX 和 PHP 更新网页

javascript - PHP 在 URL 字符串中连接变量

php - 当需要多种尺寸时,图像应该如何存储?

php - 如何使用 CodeIgniter 检索 HTTP $_GET 值

mysql - Codeigniter:数据库列选择后备

php - fgetcsv 没有正确读取保存在 linux 系统中的 csv 文件