Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macfaninpdx

macrumors regular
Original poster
Mar 6, 2007
198
0
I am working on an HTML/JavaScript project where I want to query a mySQL database and hand the query results back to my code for parsing. Normally I would use arrays and such (cause that's really what I know how to do), but I think that using objects would be so much cleaner in JavaScript. Here is what it will basically do:
* JavaScript will call a PHP form to query the DB (asynchronously)
* PHP will return the results, maybe as many as 5000 results with 10-20 fields each.
* JavaScript will store the result in an (object? / array?) and use it to build a list. It will also be used for sorting, filtering and randomly accessing data from the list.

I know how to do the first two steps, but then what? I have read about JSON, and believe this is how it should be done. And I am using PHP5, so I can hand a JSON formatted string back to the JS. But what does the object look like?

Let me use a music analogy as an example. Suppose I have the following fields in my database: title, album, year, artist and genre. Would my object be a "song" with each field being an attribute? Then how do I build a playlist - is this an array of objects? Or would my object be a playlist, with each song being another object? Is this possible?

I am new to OOP, so any help is, well, helpful.
 
why have the client do all the work with the data?

I'll suggest, use the server all you can for data manipulation. Make an XML of the final result and just use JavaScript to present it to the user.

With objects a lot of things are possible, like your last assumption. Yes playlist can be an object so can song. Try to think of the attributes of each separetly at first, don't go linking them yet. You can see that song is part of a playlist so you might want to have playlist be a parent and song be it's child, meaning that song can access or have the same attributes as playlist. You'll have to read on heritage and OOP, try to look for Java OOP tutorials I remember them being some of the easiest to grasp OOP concepts.

Hope this helps!
 
Suppose I have the following fields in my database: title, album, year, artist and genre. Would my object be a "song" with each field being an attribute? Then how do I build a playlist - is this an array of objects? Or would my object be a playlist, with each song being another object? Is this possible?

I am new to OOP, so any help is, well, helpful.

Both of those approaches (array, containing object) would work fine depending on needs. You could even do a hybrid approach: a containing object with some metadata (say, the playlist name) and an array of contents.
 
Thanks for the advice. I took a look at some of the information at the links posted by angelwatt and iBert, and ther eis some really good infromation there. I think I will try making an array for the playlist, and each entry in the array (song) will be an object.

I am going to do some testing to see if I can get the maniuplation done that I want without the speed penalty that usually comes with walking arrays.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.