I'm creating an app that parses json feeds into a listactivity. I've read that new items are added at the bottom of a listview and this is the case in my app. I'm trying to remedy this by sorting the output of my json by id in descending order. Don't know if this will fix my problem or I need to remedy it in my java code. Part one of my question is, will changing the order of output from the database change the order in which the feeds are displayed in the app? If so, I've tried to modify my php script by following the examples on http://php.net/manual/en/function.usort.php but the order doesn't change. Part two, What am I doing wrong in the following php script?
<?php
require("config.inc.php");
$query_params=null;
//initial query
$query = "Select * FROM feeds";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["feed"] = array();
foreach ($rows as $row) {
$post = array();
$post["id"] = $row["id"];
$post["name"] = $row["name"];
$post["status"] = $row["status"];
//update our repsonse JSON data
array_push($response["feed"], $post);
}
function cmp($a, $b)
{
if ($a->id == $b->id) {
return 0;
}
return ($a->id > $b->id) ? -1 : 1;
}
$a = array();
usort($a, "cmp");
// echoing JSON response
echo json_encode($response, JSON_NUMERIC_CHECK);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>