The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Javascript Object List: display and sort- How to?

and i need some help on some web app admin interface i have the task to developp:

i have a long list of object: books with properties like title, author, publication date, subject, similar books list with their title and date of publication.

how do i generate the list books object after getting that list of books after retrieving it through php from a mysql database and then display that list of books in an HTML table?

Then i would like to be able for the user of that admin system to be able to sort the list by title, author, date or even subject?

why all of this should be done in JS instead of php, it's because i have been ordered to do dev this in JS by my boss (i ma an intern)

i would like to have the layout of the functions i have to write to do this three things!
1-generate javascript objects Books (list?) out of php/mysql!
2-display the list of book in html table (one cell with title, author, publication and subject) and the second cell (lits of similar books with title and date of publication)
3-sorting books by title, author, date or subject

This would be much easier if you're generating the table using PHP. Then the sorting of it is done with javascript by generating the objects using the DOM. It's easier if each cell is given a class, e.g. the cells containing the titles have the class "booktitle", the cells containing the subject have the class "booksubject", etc. Are you really sure you're supposed to generate the javascript objects using PHP? You can't create the table with PHP - it has to be javascript?

Most of the code of this app is written in php and the books list etc... are stored in a mysql database and the specifications i have been given were very sharp about the part that said that everything will be done in javascript except adding a new book to the list or removing one... The rest is entirely done with javascript displaying the list of a given number of books (10), then sort those 10 by names, titles etc... all of this should be in JS... i am really new with JS as an OO language so i am quite lost with it... that's why i am trying to get help on functions generating the js list objects books, then display the list and sorting the list...

It's like associative arrays in PHP. You iterate through them using for...in, similar to foreach in PHP.

In order to create the table, you need to create a TABLE element. And then create a TBODY element inside, and then TR and TD elements inside that. For this, you need createElement. So, you need to create the TABLE and TBODY and then iterate through your objects creating TR and TD elements as you go along. Give it a go and then based on that we'll see how it goes.

i'll try and do that it would be helpfull since i am not really good with JS that u give me some coding hints...
list of object generation
displaying the list in html table
and then sorting them by one of their property

It's like associative arrays in PHP. You iterate through them using for...in, similar to foreach in PHP.

In order to create the table, you need to create a TABLE element. And then create a TBODY element inside, and then TR and TD elements inside that. For this, you need createElement. So, you need to create the TABLE and TBODY and then iterate through your objects creating TR and TD elements as you go along. Give it a go and then based on that we'll see how it goes.

Object literal notation representation is it what is often called JSON?

kreelin, I'm not going to do everything for you. The best thing is if you try for yourself and then I can help you with anything you have problems with. Those links are a good starting point and if you are stuck, have a look at the rest of the Javascript guide those links are part of. Also, I'm assuming you know what the structure of an HTML table is like. If not, just google for it and you'll find tons of help.