我想将我的选择查询的json响应二合一发送

时间:2014-06-21 05:32:50

标签: php json

header('Content-type: application/json');
    include("con.php");
    $school_id=1;
    $sql=mysql_query("SELECT * FROM tbl_photogallery WHERE school_id=$school_id");
    $response=array();
    $info=array();
    while($rows=mysql_fetch_assoc($sql)){      
        $galleryInfo=array();
        $galleryInfo["image_name"]=$rows["image_name"];
        $galleryInfo["image_url"]=$rows["image_url"];
        array_push($info,$galleryInfo);  
    }
    $response["school_id"]=$school_id; 
    $response['info']=$info;
    echo json_encode($response);

这里我在json响应中获得了一行但是我想在下面的照片库中有两个数据库行我在下面提到我的输出和我想要的响应格式

我的输出以这种方式给我输出。

{
    "sucesss": "1",
    "school_id": 1,
    "info": [
{
    "image_name": "school1.jpg",
    "image_url": "http://mydomain.in/mobi_school/photogallery/school1.jpg"
},
{
    "image_name": "School2.jpg",
    "image_url": "http://mydomain.in/mobi_school/photogallery/School2.jpg"
}
]
}

我希望这是我的json以这种方式响应,以便我可以连续显示我的响应两个图像。

{
    "sucesss": "1",
    "school_id": 1,
    "info": [
[
    {
        "image_name1": "school1.jpg",
        "image_url1": "http: //geetaarts.in/mobi_school/photogallery/school1.jpg",
        "image_name2": "school1.jpg",
        "image_url2": "http: //geetaarts.in/mobi_school/photogallery/school1.jpg"
    }
]
]
}

2 个答案:

答案 0 :(得分:0)

尝试这样的事情

header('Content-type: application/json');
include("con.php");
$school_id=1;
$sql=mysql_query("SELECT * FROM tbl_photogallery WHERE school_id=$school_id");
$response=array();
$info=array();
$info_arr=array();
while($rows=mysql_fetch_assoc($sql)){
    $galleryInfo=array();
    $galleryInfo["image_name"]=$rows["image_name"];
    $galleryInfo["image_url"]=$rows["image_url"];
    array_push($info_arr,$galleryInfo);
}
array_push($info,$info_arr);
$response["school_id"]=$school_id;
$response['info']=$info;
echo json_encode($response);

答案 1 :(得分:0)

您已经得到了所需要的东西。如果外部数组只有一个元素,则将数组包装在另一个数组中是没有意义的。如果你出于某种原因想要它,你认为你可以做到。我将稍微重构您的代码,以便我们可以看到发生了什么。

$school_id=1;
$sql = mysql_query("SELECT * FROM tbl_photogallery WHERE school_id=$school_id");
$response = array("school_id" => $school_id, "info" => array());
for($i = 0; $rows=mysql_fetch_assoc($sql); $i++){  
  $response['info'][$i] = array();
  $response['info'][$i]['image_name'] = $rows['image_name'];
  $response['info'][$i]['image_url'] = $rows['image_url'];
}

您的json随机"sucesss": "1"不在您的代码中

这与你的基本相同,但更容易理解。这是完美的,因为现在在javascript中我们可以访问第一个图像:

var schools = JSON.parse(jsonResponseString);
var firstImage = schools['info'][0]['image_url'];

你问的是如何将它包装在一个额外的数组中,所以我最后会告诉你。只需将上面的代码更改为:

$school_id=1;
$sql = mysql_query("SELECT * FROM tbl_photogallery WHERE school_id=$school_id");
$response = array("school_id" => $school_id, "info" => array());
for($i = 0; $rows=mysql_fetch_assoc($sql); $i++){  
  $response['info'][$i] = array();
  $response['info'][$i][0] = array();
  $response['info'][$i][0]['image_name'] = $rows['image_name'];
  $response['info'][$i][0]['image_url'] = $rows['image_url'];
}

我经历了所有解释和更改代码,以便更容易看到将数组包装在数组中是没有意义的。希望这有用。

在javascript中访问第一个图像,我们现在必须添加额外的无意义数组:

var schools = JSON.parse(jsonResponseString);
var firstImage = schools['info'][0][0]['image_url'];

注意:我没有运行任何此类代码,因此我可能会遇到一些语法错误,但这些想法都存在。