Tutorials

PHP Custom List Order

by Crayon Violent on Dec 3, 2008 6:40:28 PM

So you have some tabular data printed out in your browser. You can even change the order of the information by clicking on the column name at the top of your list. But can you make your own custom list order?

Or maybe you're deciding to make a content management system (CMS). You're making your features modular, so it's easier to add/remove modules. How do you go about displaying them in a custom order in the browser?

Or to illustrate with simple numbers: If you have a list ordered 1 2 3 4 5 and you're wanting to somehow make it 1 3 2 5 4 or 1 2 3 5 4 or 1 5 2 4 3 or whatever else, then you've come to the right place.

Just want to say up front that yes, I am aware that there are several ajax or other "web 2.0" type methods, frameworks, etc.. that offer this sort of thing. You can drag and drop rows and it's flashy and no, that's not what you're going to get out of this tutorial. This is straight php. No bells and whistles and warm fuzzy little kitties to jump up and down and purr about what a great job you're doing.

Comments

Very nice tutorial CV!.

1. Thomas Johnson on Dec 4, 2008 9:46:21 AM

Nice Tutorial, I'd like to see how this could be adapted to work with a system where rows can also be deleted. So;
1, 2, 3, 4, 5 could change to 1, 2, 3, 5, 4

then something could be removed 1, 3, 5, 4

then it could re-order without first doing this 1, 2, 5, 4

2. Gareth Evans on Dec 7, 2008 8:09:46 PM

Okay, here you go. Add this code somewhere before the codeblock that selects the data to be displayed:

and then inside your while loop that lists the data, add another table cell next to the name or wherever:

Basically what that does is first check to see if the delete link was clicked. If so, it takes the id and deletes the row that has the id.

Then, all the rows and columns are selected. Okay here's the thing about that: You need another unique column in order to do this. In that code ^ I use the name column because it's the only other column there is in this code, and the example data happens to be unique. Ideally, you would use a real unique column like the primary key or account_id type column or whatever.

Basically the idea is to then loop through each row and use a regular + 1 inc'ed var and update the usort column where name = current name.... and that's why you really need to use your primary key column instead of some column that can have duplicate data.

Again, this code example only has 2 columns to work with, and they both happen to be unique. Ideally, when you're getting into adding and deleting rows, you're going to have a separate column containing a unique id, as primary key, auto_ince'd or whatever. So in your real code, you would be passing that id and basing your delete off that, not the usort column. And ideally, you would be looping through the data and basing the update off that same unique id column in the where clause.

3. Crayon Violent on Dec 7, 2008 10:48:05 PM

That's fantastic thank you. Will be implementing something similar in my CMS

4. Gareth Evans on Dec 8, 2008 7:34:55 PM

Hi, great tutorial that you wrote.
I have a few questions.
How can i add another line to the table? Can you explain how to count the number of rows so the new one will be like total + 1?
In case of use a unique id as you mentioned how should it look on your code? What the variable $r['name'] means?
Thanks a lot for your work.

5. alapimba on Jan 7, 2009 6:23:16 AM

alapimba,

Since the numbers are supposed to be unique increments of 1 to x, there are several ways you can find out the highest number. For instance,

You can...

- SELECT usort FROM info ORDER BY usort DESC LIMIT 1
- SELECT MAX(usort) FROM info
- SELECT count(*) FROM info

Add 1 to the result and you're good to go.

re: $r['name']

$r is an associative array that holds the current row fetched from the result source. 'name' is the name of the element for $r. You can read my basic ]database handling tutorial for more info, particularly page 11

6. Crayon Violent on Jan 7, 2009 9:12:57 AM

Crayon just finished my backend thanks for your tutorial!

Thanks a lot to make this tutorial.

7. alapimba on Jan 9, 2009 1:59:32 PM

Great Script CV, I have added it to one of my projects (had to make a few tweaks, but thats to be expected..) But it was better than the solution i had

Great Job *5xThumbs Up*

8. MadTechie on Jan 26, 2009 9:02:28 PM
Login or register to post a comment.