[英]I want to send json response of my select query two in one
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);
Here I am getting one for one row in my json response but I want two database row in one for photo gallery here below I mention my output and the format of response I want 在这里,我在json响应中得到一行一,但是我想在其中的一图库中有两行数据库,下面我提到我的输出和想要的响应格式
My output is giving me output in this manner. 我的输出以这种方式给我输出。
{
"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"
}
]
}
I want this the response of my json in this manner so that I can show my response two images in a row. 我希望以此方式响应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"
}
]
]
}
try something like this 尝试这样的事情
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);
You are already getting what you need. 您已经获得了所需的东西。 Wrapping an array inside of another array is pointless if the outer array will only ever have one element. 如果外部数组仅包含一个元素,则将一个数组包装在另一个数组中是没有意义的。 If you for some reason want it thought you can do it. 如果您出于某种原因想要它,您可以做到。 I am going to refactor your code a bit so we can see what is going on. 我将对您的代码进行一些重构,以便我们可以看到发生了什么。
$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'];
}
your json has a random "sucesss": "1"
not in your code 您的json中有一个随机的"sucesss": "1"
不在您的代码中
This is essentially the same thing you have but it is easier to understand. 这基本上与您拥有的东西相同,但是更容易理解。 This is perfect because now in javascript we can access the first image like this: 这是完美的,因为现在在javascript中,我们可以像这样访问第一个图像:
var schools = JSON.parse(jsonResponseString);
var firstImage = schools['info'][0]['image_url'];
You are asking how to wrap this in an extra array though so I will show you finally. 您正在询问如何将其包装在额外的数组中,所以我将最终向您展示。 Just change my code above to: 只需将我上面的代码更改为:
$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'];
}
I went through all the explaining and changing up of the code to make it easier to see that it is pointless to wrap the array in an array. 我经历了代码的所有解释和更改,以使您更容易看出将数组包装在数组中毫无意义。 Hope this was helpful. 希望这会有所帮助。
In javascript to access the first image we now have to add the extra pointless array: 在javascript中,要访问第一个图像,我们现在必须添加额外的无意义数组:
var schools = JSON.parse(jsonResponseString);
var firstImage = schools['info'][0][0]['image_url'];
Note: I haven't run any of this code so I may have some syntactical errors but the ideas are all there. 注意:我没有运行任何代码,因此我可能会遇到一些语法错误,但是所有想法都存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.