Hi

I am trying to read the datas from mysql using php in json. but i am not getting my expected output. Please help me to fix the issue.

code

<?php
include('../config.php');
//mysql_query("SET NAMES 'utf8'");
$tdate = date('d-m-Y');
$url = 'url';
$sql = "SELECT * FROM pimage where date<='$tdate' and pstatus='Y' ORDER BY id desc";
$result = mysql_query($sql,$conn);
$array = array("photoOfDayList"=>array());    

while($row = mysql_fetch_array($result ))
{
    $b = array();
        $a = array("photoOfDay"=>array());
        $a["photoTitle"] = $row['ptitle'];
        $a["photoId"] = $row['id'];
        $a["date"] = $row['date'];
        $a["thumbImg_Phone"] = 'url'.$row['pimage'];
        $a["thumbImg_Tablet"] = 'url'.$row['pimage'];
    $a["img_Phone"] = 'url'.$row['pimage'];
        $a["img_Tablet"] = 'url'.$row['pimage'];
    $array["photoOfDay"][] = $a;
        $array["photoOfDayList"][] = $b;
}
header("Content-type: application/json; charset=utf-8");

echo json_encode($array);


exit();
?>

I am getting a below Output

{
    "photoOfDayList": [
    [],
    [],
    []
    ],

"photoOfDay": [
        {
            "photoOfDay": [],
            "photoTitle": "Title",
            "photoId": id,
            "dateTime": "date",
            "thumbImg_Phone": "$url/photo.jpg",
            "thumbImg_Tablet": "$url/photo.jpg",
            "img_Phone": "$url/photo.jpg",
            "img_Tablet": "$url/photo.jpg"
        },
        {
            "photoOfDay": [],
            "photoTitle": "Title",
            "photoId": id,
            "dateTime": "date",
            "thumbImg_Phone": "$url/photo.jpg",
            "thumbImg_Tablet": "$url/photo.jpg",
            "img_Phone": "$url/photo.jpg",
            "img_Tablet": "$url/photo.jpg"
        },
        ......
        {
            "photoOfDay": [],
            "photoTitle": "Title",
            "photoId": id,
            "dateTime": "date",
            "thumbImg_Phone": "$url/photo.jpg",
            "thumbImg_Tablet": "$url/photo.jpg",
            "img_Phone": "$url/photo.jpg",
            "img_Tablet": "$url/photo.jpg"
        }
    ]   
}

My expected Output should be

{
  "photoOfDayList": [
    {
      "photoOfDay": {
        "photoTitle": "Title",
        "photoId": id,
        "dateTime": "date",
        "thumbImg_Phone": "$url/photo.jpg",
        "thumbImg_Tablet": "$url/photo.jpg",
        "img_Phone": "$url/photo.jpg",
        "img_Tablet": "$url/photo.jpg"
      },
      {
        "photoTitle": "Title",
        "photoId": id,
        "dateTime": "date",
        "thumbImg_Phone": "$url/photo.jpg",
        "thumbImg_Tablet": "$url/photo.jpg",
        "img_Phone": "$url/photo.jpg",
        "img_Tablet": "$url/photo.jpg"
      },
      .
      .
      .
      .
      {
        "photoTitle": "Title",
        "photoId": id,
        "dateTime": "date",
        "thumbImg_Phone": "$url/photo.jpg",
        "thumbImg_Tablet": "$url/photo.jpg",
        "img_Phone": "$url/photo.jpg",
        "img_Tablet": "$url/photo.jpg"
      }
    }
  ],
  "code": 0,
  "result": "success"
}
Member Avatar for diafol

Your expected doesn't really make much sense to me either.

You have ...

"photoOfDay": {...}, {...}, {...}

Surely, this object should be an array of objects?

"photoOfDay": [{...}, {...}, {...}]

thanks diafol.

i fixed the code.

while($row = mysql_fetch_array($result ))
    {

$arr['photoOfDayList'][] = array('photoOfDay' => array('photoTitle' => $row['ptitle'],'photoId' => $row['id'],'date' => $row['date'],'thumbImg_Phone'=>'url'.$row['pimage'],'thumbImg_Tablet'=>'url'.$row['pimage'],'img_Phone'=>'url'.$row['pimage'],'img_Tablet'=>'url'.$row['pimage']));

    }
    header("Content-type: application/json; charset=utf-8");

    echo json_encode($arr);


    exit();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.