View Full Version : Datagrid Order By

05-20-2008, 10:17 PM
I apologize if I should have perhaps posted this in the mysql forum. I Wasn't sure which one to post it in.

I'm using a datagrid that lets you set the a default column, and it will pull the data from your sql table and place it in a nice sortable grid. It seems to work very well thus far.

The datagrid is actually from here: http://stefangabos.blogspot.com

Now, I'm trying to modify the code some so that I can change the way the grid loads the first time, as I want it specify the ORDER BY to list in a specific order, and not simple ASC or DESC. From what I can tell, I believe I need to modify this section of the code.

// if the data needs to be sorted
if ($this->sortField != "") {

// append the sort string to the SQL
$this->source .= "
ORDER BY " . $this->sortField .
(strtolower($this->sortDirection) == "desc" ?
" DESC" :
) .
(trim($this->secondarySortColumns) != "" ? ", " . (trim(substr($this->secondarySortColumns, 0, 1)) == "," ? substr(trim($this->secondarySortColumns), 1) : trim($this->secondarySortColumns)) : "");


I've tried to manually change it, but it seems I can't get it work. And I'm trying to ORDER the grid on the page load to something like this:

ORDER BY field(state,'waiting','initial','playing','finished'), last_move DESC

05-20-2008, 10:47 PM
ok, as I try to digest all of the datagrid code, I've learned that the grid can be called from either an array or from an sql query. I'm currently calling it from an array, as it was the better approach.

So, in this datagrid code, there is the following to sort the array

// if the data needs to be sorted
if ($this->sortField != "") {
// sort the array
array_multisort(${"zebraDataGrid_" . $this->sortField}, strtolower($this->sortDirection) == "desc" ? SORT_DESC : SORT_ASC, $this->source);

Now is it possible to sort the array in specific order?

05-21-2008, 02:33 AM
As an update, I've learned how to use the array_mulitsort to sort the multidimensional array as below.

But is there a way that you can sort the multi deminsional array in a specific order, that is neither alphanumeric nor ASC, DESC? Example, where you sort by 'waiting', 'playing', 'finished', 'initial'

array_multisort(${"zebraDataGrid_Status"}, strtolower($this->sortDirection) == "asc" ? SORT_DESC : SORT_ASC,
${"zebraDataGrid_Last_Move"}, strtolower($this->sortDirection) == "desc" ? SORT_DESC : SORT_ASC, $this->source);

05-21-2008, 06:11 AM

05-21-2008, 03:44 PM
I'm not sure it was easier or not, but I figured it was simple to just add another element to the array to define the order, and then just sort on the new field. something like below:

case 'waiting': $row['Status_Order'] = 1;
case 'initial': $row['Status_Order'] = 2;
default: $row['Status_Order'] = 3;

Now I was I able to define the sort order, and I used array_multisort to sort the two fields in the order I wanted them in.