0

I am working on the application to get a response from the server in JSON format I need the response in this format

{
  "data" :[
{

  "cat_id" : "1",
  "post_id" : "2",

  "sticker_info":[


   ], 

  "text_info" : [
    {

      "font_family" : "arial.otf",
      "text" : "MY NAME",
      "text_id" : "1",
      "txt_color" : "#000000",
      "txt_height" : "7.6",
      "txt_order" : "1",
      "txt_rotation" : "0",
      "txt_width" : "96.5",
      "txt_x_pos" : "70.2",
      "txt_y_pos" : "4.7"

    },

what i tried so for

$sql = "SELECT text_id,my_text,text_colour,x_position,y_position,width,height,rotation,text_order,font_family,cat_id,post_id  FROM text_info where cat_id='$cat_id'";
				    
$result = $conn->query($sql);
//store the entire response
$response = array();
//the array that will hold the titles and links
$posts = array();
while($row=$result->fetch_assoc()) //mysql_fetch_array($sql)
{ 
$text_id = $row['text_id'];
$my_text = $row['my_text'];
$text_colour = $row['text_colour'];
$x_position= $row['x_position'];
$y_position= $row['y_position'];
$width = $row['width'];
$height = $row['height'];
$rotation = $row['rotation'];
$text_order = $row['text_order'];
$font_family = $row['font_family'];
$cat_id = $row['cat_id'];
$post_id = $row['post_id'];
$text_info[]=array('text_id'=> $text_id, 'my_text'=> $my_text,'text_colour'=> $text_colour);
$output = array('text_information' => $text_info);


//each item from the rows go in their respective vars and into the posts array
$posts[] = array('text_id'=> $text_id, 'my_text'=> $my_text,'text_colour'=> $text_colour, 'x_position'=> $x_position,'y_position'=> $y_position, 'width'=> $width,'height'=> $height, 'rotation'=> $rotation,'text_order'=> $text_order, 'font_family'=> $font_family,'cat_id'=> $cat_id, 'post_id'=> $post_id
); 
 array_push($posts,$output);
} 
//the posts array goes into the response
    $data1=json_encode($posts);
	$response['error'] = false; 
	$response['message'] = 'Data Retrived successfull'; 
	$response['data'] = $data1;
  
  and the output from the above code  is 
 [
 {
 "text_id":"1"
 ,"my_text":"25"
 ,"text_colour":"#682e2e"
 ,"x_position":"42.5"
 ,"y_position":"17.5"
 ,"width":"57.7"
 ,"height":"29.4"
 ,"rotation":"0"
 ,"text_order":"3"
 ,"font_family":"arial.ttf"
 ,"cat_id":"1"
 ,"post_id":"1"
 },
 {"text_information":[
 {"text_id":"1"
 ,"my_text":"25"
 ,"text_colour":"#682e2e"
 }
 ]
 },

how to get the required result I tried a lot but can't find the solution I want the tex_information to be inside the array not in an object format that is shown in top my actual requirement.


2
  • I would build the complete array in php prior to encode into json. you seem to mix both (encode then put into the array) Commented Jul 15, 2018 at 7:42
  • did you get the solution?. Commented Jul 15, 2018 at 8:19

2 Answers 2

2

This will resolve your issue. add following array in your while loop.

$text_info[]= ['text_id'=> $text_id, 
        'my_text'=> $my_text,
        'text_colour'=> $text_colour, 
        'x_position'=> $x_position,
        'y_position'=> $y_position, 
        'width'=> $width, 
        'height'=> $height, 
        'rotation'=> $rotation, 
        'text_order'=> $text_order, 
        'font_family'=> $font_family];


//each item from the rows go in their respective vars and into the posts array
$posts['data'][] = [
    'cat_id' =>$cat_id,
    "post_id" => "2",
    "sticker_info"=>[], 
    'text_info' => $text_info

]; 
        print_R(json_encode($posts));

Output:

    {"data":[{
       "cat_id":"1",
        "post_id":"2",
        "sticker_info":[],
        "text_info":[{
           "text_id":1,
            "my_text":"MY NAME", 
            "text_colour":"Yello",
            "x_position":"70.2", 
            "y_position":"4.7", 
            "width":"96.5", 
            "height":"7.6", 
            "rotation":"0", 
            "text_order":"1", 
            "font_family":"arial.otf"
        }]
     }]
}
Sign up to request clarification or add additional context in comments.

5 Comments

@MushtaqRahim did you try my solution?
yes i try it give the respons but not what is expected.
@MushtaqRahim But your Output & my output looks like same.
check out this "text_info":{....} it is object not array i need like this "text_info":[....]
actually i need like this "text_info":[ { "txt_width":"57.7", "txt_height":"29.4", ....... }, { "txt_width":"61.3", "txt_height":"24.4", .... }, upto so on i think you got my point what i am talking about.
0

Try this code

$output = array();

while($row=$result->fetch_assoc()) //mysql_fetch_array($sql)
{ 

    $posts=array();

    $text_id = $row['text_id'];

    $my_text = $row['my_text'];

    $text_colour = $row['text_colour'];

    $x_position= $row['x_position'];

    $y_position= $row['y_position'];

    $width = $row['width'];

    $height = $row['height'];

    $rotation = $row['rotation'];

    $text_order = $row['text_order'];

    $font_family = $row['font_family'];

    $cat_id = $row['cat_id'];

    $post_id = $row['post_id'];


    $sticker_info[]=array('key'=>'value','key1'=>'value','key2'=>'value');

   $text_info[]=array('text_id'=> $text_id,'font_family'=> $font_family,'text'=> $my_text,'txt_color'=> $text_colour,'txt_height'=> $height,'txt_order'=> $text_order,'txt_rotation'=> $rotation,'txt_width'=> $width,'txt_x_pos'=> $x_position,'txt_y_pos'=> $y_position);

    $posts['data' = array('cat_id'=>$cat_id,'post_id'=>$post_id,'sticker_info'=>$sticker_info,'text_info'=>$text_info);

   $output[] = $posts;
}
$output['error'] = false; 

$output['message'] = 'Data Retrived successfull'; 

echo json_encode($output);
?>

5 Comments

syntax error at this line $posts['data' = array('cat_id'=>$cat_id,'post_id'=>$post_id,'sticker_info'=>$sticker_info,'text_info'=>$text_info);
i know php a little bit.
closing the array at the end] but still unespected '='
yea working great i have corrected the synatax error thanks.
but i need a little change instead of {"data":{..... i need to {"data":[ array instead of object thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.