简体   繁体   中英

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

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.

{
    "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

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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM