Jhawins Jhawins - 1 month ago 5
PHP Question

Sorting table contents with php?

I have a table full of band listings for our Musician Finder program. We want to sort them by genre while still only having one .html or .php file containing all the information.

How would I even start to do this? Preferably with php and avoiding JS because most of our users have noscripts.
Here is a link to the test page HERE. Any suggestions would be great or even just an example of where someone else has done it.

And here is the table on paste bin.

Thanks

Also, avoiding SQL is a priority to my boss.

EDIT: So apparently this isn't feasible in php. I'd be willing to use javascript. Or Jquery. If this was just a simple table I'd be fine but obviously mine is more detailed and I don't know where to start and haven't found anything on google. If anyone wants to kindly suggest anything about this that'd be great but honestly nobody comes on here to read about how someone else doesn't like that I asked a question on stackoverflow. Lol.

EDIT AGAIN: Ok so I was looking more... And why can't I just make all my

<tr>
's
have
display:block;
per class in css, then use a JS function to change all but the right class to
display:none;
? This would be extremely simple.

Answer

Using an existing table plugin in JavaScript would probably be the best solution for you since it would avoid the round trip back to the server to do something that JavaScript can do quite easily. But seeing that you say that JavaScript is not your first option then you can do it in PHP if you choose to store all the data in a PHP array.

So you start by putting all the data into PHP arrays. Then write a function that will sort the array by whichever field you wish. There are many sorting algirithms available for this.

Display the table of data in its default order and add links that will let the user select a different column to sort. In each link you set the name of the column to sort by like this:

   <a href="mypage.php?sort=name">Sort By Name</a>

You can then simply use the parameter "sort" to decide how to sort your array before you echo it out into an HTML table.