<?php
include 'connection.php';
$array = array();  
$query = "SELECT * FROM category order by position";  
if (isset($_GET['topic_id'])) {
    if(is_numeric($_GET['topic_id']))
    $query = "SELECT * FROM category where topic_id='".$_GET['topic_id']."' order by position";
    else
    $query = "SELECT * FROM category order by position";
}
else{
$query = "SELECT * FROM category order by position";
}
$result = mysqli_query($mysqli , $query);  
while($row = mysqli_fetch_assoc($result))  
{  
    $array[] = $row;  
    }  
    echo json_encode($array)

?>