-4
function get_data()
{
    $connect = mysqli_connect("localhost", "root", "root", "Database");
    $query = "SELECT * FROM Furniture";
    $result = mysqli_query($connect, $query);
    $categories = array();

    $row = mysqli_fetch_array($result);

{
        $categories[] = array(

        'mapwidth' => "2000",
        'mapheight' => "2000",


       );            
 }

This is what I have so far, but how would I be able to create an array of "categories" so that the json file looks like this,

{
    "mapwidth": "2000",
    "mapheight": "2000",
    "categories": [
    {
        "id": "furniture",
        "title": "furniture",
        "color": "#4cd3b8",
        "show": "false"
    },
    {
        "id": "rooms",
        "title": "Rooms",
        "color": "#63aa9c",
        "show": "true"
    }
    ],

I tried to do this but I was only able to get one row to display but am not sure how to iterate through the database so that every row will display the "id", "title", "color", and "show" within the brackets as seen in the json file. So I pretty much just want the json file to look exactly like the one shown here.

  • Yup, your php has some holes in there... a missing while loop... a whole missing query for the categories data... its gonna be real hard for anyone to help you with this with so many unknowns. – IncredibleHat Jan 25 '18 at 00:46

1 Answers1

0

Try it like this:

$employee_data = array();
$categories = array();
$employee_data["mapwidth"] ="2000";
$employee_data["mapheight"] ="2000";

while($row = $result->fetch_array(MYSQL_ASSOC)) {
        $categories[] = $row;
}
$employee_data["categories"] =$categories;
echo json_encode($employee_data);

Refer to this answer to further improve your code.

USER249
  • 1,080
  • 7
  • 14