Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

How to format a json response in php

$response = [];

if ($con) {
  $sql = "select * from admission_view";
  $result = mysqli_query($con, $sql);
  if ($result) {
    $x = 0;
    while ($row = mysqli_fetch_assoc($result)) {
      $response[$x]['id'] = $row['id'];
      $response[$x]['name'] = $row['name'];
      $response[$x]['isActive'] = $row['isActive'];
      $response[$x]['branch']['id'] = $row['branchId'];
      $response[$x]['branch']['name'] = $row['branch'];
      $response[$x]['customerGroup']['id'] = $row['customerGroupId'];
      $response[$x]['customerGroup']['name'] = $row['customerGroup'];
      $response[$x]['customerGroup']['orderNo'] = $row['orderNo'];
      $x++;
    }
    echo json_encode($response, JSON_PRETTY_PRINT);
  }
} else {
  echo "Connection error";
}
 
PREVIOUS NEXT
Tagged: #How #format #json #response #php
ADD COMMENT
Topic
Name
7+5 =