Sorting Database columns using codeigniter |
[eluser]jason97673[/eluser]
So I am new to useing codeigniter and there isnt as much documentation on this as there is just plain PHP without a framework. Well I am making a simple site (for now) that has many fields and is grabbing the records from a database. I want to be able to click on the column headers and sort them. I want to beable to have them be clicked and have it sorted DESC or ASC. Using regular PHP I can do this but not using the codeigniter framework. Ill show you the site. http://sykotic-designz.org When you visit the site it is a simple table with many fields and for now only 2 records. I want to beable to click name and have it sort from A-Z(then if possible have it sort the opposite direction if clicked again). Or if you click CMP, ATT, CMP % etc, have those sorted from higher to lower(then the opposite direction if possible when clicked again). My code right now in the view is Code: <table border="1"> The code in the controller is Code: class FirstDown extends Controller { Thanks for any help.
[eluser]Asinox[/eluser]
jason97673 u need Javascript for sorting. http://www.google.com.do/search?rlz=1C1G...javascript
[eluser]jason97673[/eluser]
Well I am using data from a database, I dont believe database data can be sorted using javascript. The values arent written in stone, they are in the DB, otherwise I think javascript ould do it but I dont think javascript works here?
[eluser]jason97673[/eluser]
Yes I have no problems sorting, but I am trying to get the headers to be links that when clicked sort by that specific field in the database.
[eluser]Nicholai[/eluser]
What about something like this: Code: function index($sort = NULL , $direction = NULL) Then you can pass the sort field and direction back to the controller again to reload the page with sorted results. Code: <?php echo anchor('FirstDown/index/Name/asc','Name) ?> I'm a new guy and speculating a bit without testing, and you'll need some logic to figure out ascending vs descending, but maybe it will get you closer to what you want.
[eluser]CroNiX[/eluser]
Really a javascript solution would be best here. There are many that do this and it makes no difference where the data comes from. If its in an html table, it can be sorted easily...
[eluser]ramabodhi[/eluser]
i have been working diligently on getting this to work properly in one of my apps, and I've almost got it done, heres what i have so far hopefully some of the more experienced people can build on it: This is what your controller should look like, minus all the validation and any other special stuff you need for your specific app Code: class Search extends Controller Here is the Search Model Code: class Search_Model extends Model Then your results will be something like this Code: <?php The beauty of this is that your sorting isn't restricted to the database, you can tweak and adjust your results as you please before running them through the sort function. Now i've got one issue, on some of my pages all of my css is disappearing when i add the 5th uri string (the sort order).. everything sorts fine, but i lose my css.. any input on the whole scheme, and any advice on solving the css problem would be appreciate
[eluser]ramabodhi[/eluser]
thanks to Daniel Moore I have solved the uri/css problem (was using relative path) Now i can verify that this method of sorting database results with links in the table headers works. I will try to put this all into a library in the next couple days. |
Welcome Guest, Not a member yet? Register Sign In |