JSON equivalence

0

Good, what would a JSON like this be like but decoded from PHP?

JSON

{
"lists": [
    {
        "title": "do",
        "defaultStyle": "list-danger",
        "items": [
            {
                "title": "beforeItemDelete is never called",
                "description": "even in your \"Event handling\" example"
            }
        ]
    },
    {
        "title": "doing",
        "defaultStyle": "list-info",
        "items": [
            {
                "title": "Function to get all list info (lists, items) as object"
            }
        ]
    },
    {
        "title": "done",
        "defaultStyle": "list-success",
        "items": [
            {
                "title": "List style change event"
            }
        ]
    }
]
}

PHP

// conexión

  $sql = "SELECT id, title, description, dueDate, done FROM list";

  $result = $mysqli->query($sql);
  $arreglo = array();

while ($data = $result->fetch_assoc()) {
  $arreglo["lists"]["items"] = $data;
}

 echo json_encode($arreglo);


// desconexión

Would you show me the correct structure equivalent to the JSON? I've tried PHP and I think it shows it correctly, but since I'm not functioning properly, this great doubt arises. Thank you !!

    
asked by Cifu 09.05.2017 в 13:18
source

1 answer

2

After knowing the schema of the database it has been possible to implement a customized solution:

$sql = "
  SELECT
    id,
    title,
    description,
    DATE_FORMAT(dueDate, '%Y-%m-%d') dueDate,
    done
  FROM todolist
";
$result = $mysqli->query($sql);
if ($result === false) {
  die($mysqli->error);
}
$arreglo = [
  'lists' => [
    0 => [
      'title' => 'Tareas',
      'defaultStyle' => 'list-success',
      'items' => [],
    ],
  ],
];
while ($data = $result->fetch_assoc()) {
  /* El título es obligatorio en el registro */
  $elemento = [
    'title' => $data['title']
  ];
  /* Si la descripción no es nula la agregamos */
  if (is_null($data['description']) === false) {
    $elemento['description'] = $data['description'];
  }
  /* Si la fecha de vencimiento no es nula la agregamos también */
  if (is_null($data['dueDate']) === false) {
    $elemento['dueDate'] = $data['dueDate'];
  }
  /* Si done es cualquier otra cosa menos un 0, la tarea está hecha */
  if ($data['done'] != 0) {
    $elemento['done'] = true;
  }
  /* Agregamos al listado el elemento que acabamos de crear */
  $arreglo["lists"][0]["items"][] = $elemento;
}
/* Enviamos al navegador la información */
header('Content-Type: application/json');
echo json_encode($arreglo);

Original reply

That information resembles this one in PHP:

$arreglo = [
  'lists' => [
    [
      'title' => 'do',
      'defaultStyle' => 'list-danger',
      'items' => [
        [
          'title' => "beforeItemDelete is never called",
          'description' => "even in your \"Event handling\" example",
        ],
      ],
    ],
    [
      'title' => 'doing',
      'defaultStyle' => 'list-info',
      'items' => [
        [
          'title' => "Function to get all list info (lists, items) as object",
        ],
      ],
    ],
    [
      'title' => 'done',
      'defaultStyle' => 'list-success',
      'items' => [
        [
          'title' => "List style change event",
        ],
      ],
    ],
  ],
];
echo json_encode($arreglo, JSON_PRETTY_PRINT);

Its execution generates the following result:

{
    "lists": [
        {
            "title": "do",
            "defaultStyle": "list-danger",
            "items": [
                {
                    "title": "beforeItemDelete is never called",
                    "description": "even in your \"Event handling\" example"
                }
            ]
        },
        {
            "title": "doing",
            "defaultStyle": "list-info",
            "items": [
                {
                    "title": "Function to get all list info (lists, items) as object"
                }
            ]
        },
        {
            "title": "done",
            "defaultStyle": "list-success",
            "items": [
                {
                    "title": "List style change event"
                }
            ]
        }
    ]
}

Try this code instead of yours:

$sql = "SELECT id, title, description, dueDate, done FROM list";
$result = $mysqli->query($sql);
if ($result === false) {
  die($mysqli->error);
}
$arreglo = [
  'lists' => [
    0 => [
      'items' => [],
    ],
  ],
];
while ($data = $result->fetch_assoc()) {
  $arreglo["lists"][0]["items"][] = $data;
}

echo json_encode($arreglo);

We are filling (I imagine) only the index 0 of the result, so you have to add it to it and not directly create items in lists .

First I generate a base structure (with the 0 index filled) and then I add elements for each result of the SQL query.

    
answered by 09.05.2017 / 13:24
source