-2

I have data in the format shown below, and now i want to show this data JSON object wise like.

I have this function:

function getAllpoi(){
    global $results_poi; $results_poi = array();
    $stmt = $this->conn->prepare("SELECT * FROM poi");
    if ($stmt->execute()){
        $poi =$stmt->get_result();
        while($row = $poi->fetch_array(MYSQLI_ASSOC)){
            $results_poi[] = $row;
        }
        $stmt->close();
        return $results_poi;
    } else {
        echo "Poi is not found";
    }
}    

This is the JSON:

[
  {
    "error": false,
    "uid": 1,
    "poi": {
      "active": 1,
      "name_long": "Buckingham Fountain",
      "name_short": "Buckingham Fountain",
      "subtitle": "",
      "address": "301 S Columbus Dr",
      "city": "Chicago",
      "state": 0,
      "zip": "60605",
      "highway": "",
      "community": 0,
      "phone": "1-312-742-3918",
      "email": "",
      "website": "",
      "facebook": "",
      "twitter": "",
      "latitude": "41.87571077",
      "longitude": "-87.61973798",
      "altitude": "0",
      "timezone": "CT",
      "categories": "0",
      "parking": "Street & Public Parking Garage(s)",
      "services": "",
      "tours": "",
      "admission": "Free",
      "length": "15",
      "awards": "",
      "alerts": "",
      "rating": "5",
      "alignment": "",
      "historic_regist": "",
      "image_thumb": "1",
      "image_header": "1",
      "image_passport": "1",
      "season": "Year-round",
      "hour_mon_start": "2017-10-11 08:00:00",
      "hour_mon_end": "2017-10-11 13:00:00",
      "hour_tue_start": "2017-10-12 08:00:00",
      "hour_tue_end": "2017-10-12 13:00:00",
      "hour_wed_start": "2017-10-13 08:00:00",
      "hour_wed_end": "2017-10-13 13:00:00",
      "hour_thu_start": "2017-10-14 08:00:00",
      "hour_thu_end": "2017-10-14 13:00:00",
      "hour_fri_start": "2017-10-15 08:00:00",
      "hour_fri_end": "2017-10-15 13:00:00",
      "hour_sat_start": "2017-10-16 08:00:00",
      "hour_sat_end": "2017-10-16 13:00:00",
      "hour_sun_start": "2017-10-17 08:00:00",
      "hour_sun_end": "2017-10-17 13:00:00",
      "options_buttons": "0",
      "options_icons": "0"
    }
  },
  {
    "error": false,
    "uid": 2,
    "poi": {
      "active": 1,
      "name_long": "new poi",
      "name_short": "New",
      "subtitle": "thats the quicks",
      "address": "new york",
      "city": "new city",
      "state": 55,
      "zip": "745000",
      "highway": "national",
      "community": 5,
      "phone": "1616516516",
      "email": "newpoi@gmail.com",
      "website": "newpoi.com",
      "facebook": "new poi",
      "twitter": "new poi651651",
      "latitude": "165465.154",
      "longitude": "65146545616",
      "altitude": "15151",
      "timezone": "ct",
      "categories": "51",
      "parking": "vault parking",
      "services": "fakjhfsaKJHLKASDASDASASDSADASASDSADASHJGJHVASHCVN",
      "tours": "LKHGDHSAVDHASJDBASKJ",
      "admission": "DADASDASDASCASC",
      "length": "DFJHBVAJDBVASBCJHABSCHJ",
      "awards": "DASSACASCSACA",
      "alerts": "ADSASDSADSAD",
      "rating": "151",
      "alignment": "XJBJHASBCJHBASXCSFDSCSDSDCDSCSCDS",
      "historic_regist": "KHBJASBHDABS",
      "image_thumb": "",
      "image_header": "",
      "image_passport": "",
      "season": "",
      "hour_mon_start": "2017-10-16 04:37:49",
      "hour_mon_end": "0000-00-00 00:00:00",
      "hour_tue_start": "0000-00-00 00:00:00",
      "hour_tue_end": "0000-00-00 00:00:00",
      "hour_wed_start": "0000-00-00 00:00:00",
      "hour_wed_end": "0000-00-00 00:00:00",
      "hour_thu_start": "0000-00-00 00:00:00",
      "hour_thu_end": "0000-00-00 00:00:00",
      "hour_fri_start": "0000-00-00 00:00:00",
      "hour_fri_end": "0000-00-00 00:00:00",
      "hour_sat_start": "0000-00-00 00:00:00",
      "hour_sat_end": "0000-00-00 00:00:00",
      "hour_sun_start": "0000-00-00 00:00:00",
      "hour_sun_end": "0000-00-00 00:00:00",
      "options_buttons": "0",
      "options_icons": "0"
    }
  }
]
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
SB Dhera
  • 1
  • 2
  • function getAllpoi(){ global $results_poi; $results_poi = array(); $stmt = $this->conn->prepare("SELECT * FROM poi"); if ($stmt->execute()){ $poi =$stmt->get_result(); while($row = $poi->fetch_array(MYSQLI_ASSOC)){ $results_poi[] = $row; } $stmt->close(); return $results_poi; } else{ echo "Poi is not found"; } } – SB Dhera Oct 17 '17 at 12:48
  • Can you please show example how you want to see this data? – usman ikram Oct 17 '17 at 12:49
  • edit your question with that code in it, don't put it in the comments – sniperd Oct 17 '17 at 12:51
  • Both your starting point and your goal are extremely underspecified. – Don't Panic Oct 17 '17 at 12:52
  • How is the code related to the JSON? Please try to explain more what you're trying to do. Is the JSON an example of what you already have, or what you're trying to produce? Can you show an example of how the function is being called and how you're actually generating output? And again, please edit the question to show this. Comments are not permanent, and code in comments is generally illegible. – Don't Panic Oct 17 '17 at 13:02

2 Answers2

-1

To return object or array as json object you simply use json_encode. Simply in return put json_encode($results_poi)

EDIT

Since there are down votes i will explain my answer. From what i see there is no json_encode in function. Function returns list of associative arrays. After converting this with json_encode the result will be list of objects in json format

S-C
  • 24
  • 5
  • yes i did this so can u please check the url http://rt66ug.com/route-api/getpoi.php – SB Dhera Oct 17 '17 at 12:57
  • This url presents list of objects in json format. From what i read in question you got your result already. If you want to format it beautifuly just add correct headers in result controller – S-C Oct 17 '17 at 13:03
-1

Put this code before any echo output on your page

header('Content-Type: application/json');
malu ugo
  • 1
  • 1