<?php

header('Content-type: application/json; charset=UTF-8');

include 'conexion.php';
$ORDER = $_GET['ORDER'];

$consulta = "SELECT categorias.id_categoria, categorias.nombre, COUNT(campos.id_campo) AS campo, categorias.orden 
                FROM categorias left JOIN campos ON `id_categoria` = `fk_id_categoria` 
                GROUP by categorias.id_categoria, categorias.nombre 
                ORDER BY $ORDER";

$result = $conexion->query($consulta);

if(!$result){
    echo "Error Query";
}

while($fila = $result -> fetch_array()){
    $response[] = $fila;
}

print json_encode($response, JSON_UNESCAPED_UNICODE);
$result -> close();

?>