tags:

views:

43

answers:

2

I have a list of many <dt>/<dd> tags which come in a sort of natural order, but I would also like my visitors to have an option for alphabetical sorting. Is there a way to do this in (X)HTML/PHP with minimal code? My doctype is XHTML 1.0 Transitional. Thanks in advance.

+1  A: 

To sort a definition list using server side script (such as PHP), you'll need to first parse the xhtml into an array or object.

Once you've done this (or if you have the raw data without having to first parse it), you can use php functions such as asort or a bubble sort function.

You might be better off sorting the list on the client side with javascript.

adam
Can you please provide an example in Javascript? I'm fairly new to developing for web, coming from only Java experience.
pg-robban
yepp i would also choose client side
streetparade
Sorry, don't have time but try http://www.w3schools.com/JS/default.asp
adam
A: 

Something like this is a good starting point:

http://www.wrichards.com/blog/2009/02/jquery-sorting-elements/

The trouble is that a dt/dd is a bit trickier because each pair doesn't have an single element wrapping each pair. The code is still good though, and it's a good exercise for you to amend it if you're new to JS!

colinramsay