0

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

Edited by rpv_sen: updated in details

2
Contributors
3
Replies
25
Views
2 Years
Discussion Span
Last Post by rpv_sen
0

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

You have ...

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

Surely, this object should be an array of objects?

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

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();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.