Jump to content


ORDER BY works only for one column at a time

order by bug order not working

  • Please log in to reply
15 replies to this topic

#1 Rodrigo Furtado

Rodrigo Furtado

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 21 January 2013 - 12:34 PM

Currently, it's not possible to order by multiple columns with grocery crud, but code igniter has this functionality, so it must be something really easy to correct :)

Lets say we have this table

id | company_name | segment | relevance
1 | bbbbbbbbbbbb | industry | 50
2 | cccccccccccccc | industry | 100
3 | aaaaaaaaaaaa | commerce | 25

4 | dddddddddddd | goverment | 10

And we try to order like this:

$crud->order_by('segment','asc');
$crud->order_by('relevance','desc');
$crud->order_by('company_name','asc');

It 'll only order by the LAST order_by you put and totally ignore the rest.

AMEN ?

#2 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 27 January 2013 - 06:48 PM

I believe you can use your own model and from there set the ordering

check here, http://www.grocerycr...tions/set_model

____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#3 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 27 January 2013 - 07:14 PM

I think You can use your parameters as string: $this->db->order_by('title desc, name asc');

#4 davedriesmans

davedriesmans

    Member

  • Members
  • PipPip
  • 13 posts

Posted 04 July 2013 - 08:58 AM

Rodrigo, did you found the solution?

do you mind sharing? looking for the same...



#5 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 04 July 2013 - 09:58 AM



I think You can use your parameters as string: $this->db->order_by('title desc, name asc');

 

 

victor is absolutely right!!! Yes you can order like this with multiple fields!!!

Sorry for the disorientation and making it harder than it was to solve it!


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#6 davedriesmans

davedriesmans

    Member

  • Members
  • PipPip
  • 13 posts

Posted 04 July 2013 - 10:33 AM

victor is absolutely right!!! Yes you can order like this with multiple fields!!!
Sorry for the disorientation and making it harder than it was to solve it!

yeah, but it doesn't work (and wasn't expecting it) from the controller



#7 victor

victor

    grocery CRUD Hero

  • Advanced Member
  • PipPipPip
  • 967 posts
  • LocationMinsk

Posted 04 July 2013 - 11:52 AM

$crud-> order_by(....

#8 davedriesmans

davedriesmans

    Member

  • Members
  • PipPip
  • 13 posts

Posted 04 July 2013 - 01:57 PM

ah, it's actually     

$crud->order_by('title asc, name');

 without the last asc



#9 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 04 July 2013 - 08:01 PM

It should work, check here, http://ellislab.com/...ord.html#select

scroll down till you get to the line, 

$this->db->order_by();


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#10 rothkj1022

rothkj1022

    Newbie

  • Members
  • Pip
  • 6 posts
  • LocationMinneapolis, MN

Posted 05 July 2013 - 03:58 PM

It should work, check here, http://ellislab.com/...ord.html#select

scroll down till you get to the line, 

$this->db->order_by();

 

Thanks for the reference, but davedriesmans is right, it does not work.  When using multiple order_by calls, it only sets the first.  If using a string, it always appends " asc" to the end, but that won't work if the last sort in your query needs to be desc.



#11 rothkj1022

rothkj1022

    Newbie

  • Members
  • Pip
  • 6 posts
  • LocationMinneapolis, MN

Posted 05 July 2013 - 04:12 PM

A few more comments:

 

To correct myself, when using multiple order_by calls, it only sets the *last* order_by, not the first.

 

I was able to workaround this bug by just adding my table's id as a final parameter when passing the multiple sorts as a string, like this:

$crud->order_by('sortcol1 asc, sortcol2 desc, id');

 

There must be some client-side sorting variable being persisted by a cookie/session variable.  When trying to test my order_by results I was running into the data sort not changing when I refreshed.  I disabled cookies and tried again, and that did the trick.



#12 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 05 July 2013 - 08:39 PM

A few more comments:

 

To correct myself, when using multiple order_by calls, it only sets the *last* order_by, not the first.

 

I was able to workaround this bug by just adding my table's id as a final parameter when passing the multiple sorts as a string, like this:

$crud->order_by('sortcol1 asc, sortcol2 desc, id');

 

There must be some client-side sorting variable being persisted by a cookie/session variable.  When trying to test my order_by results I was running into the data sort not changing when I refreshed.  I disabled cookies and tried again, and that did the trick.

 

 

Post your code and the structure of any tables please . There is something strange there.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#13 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 06 July 2013 - 06:17 AM

Well, what theme you are using? Are you using flexgrid or datatables, the solution is working and achievable in flexgrid but not in datatables. What in datatable it dose is collects all the records to play around with and based on the value from cookie, it will reset the order to the same. I have experienced the same. I will recommend not to use datatable in this case. If you looking for the same interface like it, you can alter flexgrid css for the same. Of course ... both has different requirements and hence will need some skill to duplicate the same from datatables to flexgrid, but it is quite possible as i myself have achieved the same for few of my projects.

 

If you using flexgrid and still having the trouble then its something that needs to be looked into for sure.. but i never faced that issue with flexgrid for sure.



#14 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 06 July 2013 - 09:04 AM

O! I need to check this!!!

 

What in datatable it dose is collects all the records to play around with and based on the value from cookie, it will reset the order to the same. I have experienced the same. 


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#15 Hemant Randive

Hemant Randive

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 20 November 2015 - 11:55 AM

I am using grocery crud datatables theme i have tried flexigrid too but its sorting of table by two columns not work in it. I want to sort the table by two columns in grocery crud view



#16 aronsereny

aronsereny

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 02 February 2016 - 07:05 PM

here is what works:

 

order by two columns:

1)

   $crud->order_by('last_name, start_date');  same as $crud->order_by('last_name, start_date', 'ASC');

 

2)

   $crud->order_by('last_name, start_date', 'DESC');

 

current;y one cannot have different directions per column (i.e. "order by c1 ASC, c2 DESC" is not possible)

 

would be nice to be able to do:

 

   $crud->order_by('last_name, start_date', 'ASC, DESC');   (will translate to "order by last_name ASC, start_date DESC")







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users