sorting records in php/mysql
I have function like this which show database records in default order i would like to show it descending order. Just let me where do i need to write commmand to to sort the records.
Code:
function query_split($sqlu) {
$sql = str_replace('#__', DB_PREFIX, $sqlu);
if ( (xc_request('page')) && (xc_request('page') !== '1') && (is_numeric(xc_request('page'))) ) {
$page = ((int)xc_request('page') - 1);
$this->page = ((int)xc_request('page'));
$page_sql = ($page * MAX_DISPLAY_SEARCH_RESULTS);
} else {
$page = '0';
$this->page = '1';
$page_sql = '0';
}
$pos_to = strlen($sql);
$pos_from = strpos($sql, ' from', 0);
$pos_group_by = strpos($sql, ' group by', $pos_from);
if (($pos_group_by < $pos_to) && ($pos_group_by != false)) $pos_to = $pos_group_by;
$pos_having = strpos($sql, ' having', $pos_from);
if (($pos_having < $pos_to) && ($pos_having != false)) $pos_to = $pos_having;
$pos_order_by = strpos($sql, ' order by', $pos_from);
if (($pos_order_by < $pos_to) && ($pos_order_by != false)) $pos_to = $pos_order_by;
//let's add one time calling for numrows
$reviews_count_query = @mysql_query("select count(*) as total " . substr($sql, $pos_from, ($pos_to - $pos_from)));
$reviews_count = @mysql_fetch_object($reviews_count_query);
$query_num_rows = $reviews_count->total;
@mysql_free_result($reviews_count_query);
$resource = @mysql_query ($sql . " limit " . $page_sql . ", " . MAX_DISPLAY_SEARCH_RESULTS );
$before = ($page * MAX_DISPLAY_SEARCH_RESULTS);
if ($query_num_rows < $before) {
$this->batch_from = '0';
} else {
$this->batch_from = $before;
}
if ($query_num_rows < (($page * MAX_DISPLAY_SEARCH_RESULTS) + MAX_DISPLAY_SEARCH_RESULTS)) {
$this->batch_to = $query_num_rows;
} else {
$this->batch_to = ($page * MAX_DISPLAY_SEARCH_RESULTS) + MAX_DISPLAY_SEARCH_RESULTS;
}
$this->batch_size = $query_num_rows;
if (!$resource) {
$this->eroarea = mysql_error();
die ($this->Error());
return 0; // error
}
$this->num_rows = $query_num_rows;
return $resource;
}
thanks in advance
Re: sorting records in php/mysql
Just add 'ORDER BY' to your SQL statement.
php Code:
//eg
$sql = "SELECT * FROM YourTable ORDER BY ID DESC";