Page 1 of 1
Storing page layout..
Posted: Wed Nov 07, 2007 2:16 am
by arpowers
Hey guys,
whats the best way to store a page layout...
meaning I have a bunch of independent elements that I want to load into a page, but I want to let the user control the order of said elements... how would I best keep track of this in a database?
thanks
ap
Ordering
Posted: Wed Nov 07, 2007 4:42 am
by willearp
Hi arpowers,
It rather depends on how these page elements are stored - I take it there is one or more tables storing this information? (Post more info if you want)
But I think you just need an ordering field, if the objects are all in one table then simply add an unsigned integer field and set a numeric order for this.
To update it in the database through a CMS application, you will have to build controls to move items up and down, and reset the order, this can be done by putting a link next to each item with an up or down button (Or both), and sending a get string back to your script with the ID of the item, and if both links used, the direction of travel.
Within your application, retrieve the IDs of all the items in the database, ordered by this ordering field, then iterate through this data until you find the ID you want to move, pushing each ID into a new array along the way. Dependant on the direction of travel, you will then need to skip over the item you are moving, push the next ID into the new array, and then push in the one you are moving. The other way round you will have to look ahead, and push the item you are moving into the new array 1 position before its current position then skip over it. After this swap, continue adding the rest of the IDs into the new array.
Now to update the database with the new order - you could simply swap the two items that have changed position, but this causes problems when you add new items, or if the order has not been set in the first place - it is better to update every record with a new order number. So take you new array, and iterate through updating the database where IDfield=IDnum (the value of the array item), and set the ordering field to the array index. And hey presto!, your database now has a field you can order by, and any contained errors or duplicates in the ordering numbers are solved.
Note any new items you add will now appear at the top of the list, as they will have an order number of 0, but when getting the items from the DB, you could have a sub order of the ID field to make the unordered fields appear at the top in the order they were added. To get them to appear at the bottom, you will have to have the highest number in the ordering field as the first.
Hope this helps!
Will Earp
Posted: Wed Nov 07, 2007 7:41 am
by CoderGoblin
The most obvious answer is via a templating system, either integrated with a cms or separate. The most well known of which I can think of is Smarty. Another thing to look at, if you haven't already, is something called Model-View-Controller (MVC) Architecture.
Posted: Wed Nov 07, 2007 12:48 pm
by arpowers
fantastic answer Will!,
very insightful and well thought out solution
I will do exactly what you said...
Once, I get it done I'll report back on how it turns out..
Have you done this before?
on another note I am using smarty, and its great, but it doesn't have any capabilities regarding user defined layout.. (unless I'm missing something:)
thanks again!
Andrew