Advice needed on JavaScript, PHP, SQL and Objects

Discussion in 'Web Design and Development' started by macfaninpdx, Oct 4, 2007.

  1. macfaninpdx macrumors regular

    Joined:
    Mar 6, 2007
    #1
    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.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
  3. iBert macrumors regular

    iBert

    Joined:
    Jul 14, 2004
    #3
    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!
     
  4. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    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.
     
  5. CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #5
    I would just use an array. It's quick and simple. And a cute little loop will get your data and you can sort as well.
     
  6. macfaninpdx thread starter macrumors regular

    Joined:
    Mar 6, 2007
    #6
    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.
     

Share This Page