Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Datagrid Order By

    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.

    PHP 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->secondarySortColumns01)) == "," 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:

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

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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

    PHP Code:
    // 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?

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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'

    PHP Code:
    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); 

  • #4
    Regular Coder
    Join Date
    Sep 2006
    Location
    Vermont, USA
    Posts
    154
    Thanks
    0
    Thanked 6 Times in 6 Posts
    Active PHP/MySQL application developer available for immediate work.
    syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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:

    PHP Code:
    switch($row['Status']){
        case 
    'waiting':     $row['Status_Order'] = 1;
                            break;
        case 
    'initial':     $row['Status_Order'] = 2;
                            break;
        default:             
    $row['Status_Order'] = 3;
                            break;

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •