General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 18th, 2006, 05:15 AM
method method is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 54 method User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 45 m 12 sec
Reputation Power: 4
Arrow How to store selected songs ids in db?

Hi all i have a db for my music collection. It has one table with the following feilds on it:

Code:
ID|filename|artist|album|title|track|comments|genre|  seconds|filesize|bitrate


I already created pages that with checkboxes that user select indvidual songs and by click of play button it goes and plays those songs for me.

Now i created another button next to play that it supposed to write the selected songs to db and save the ids of slected songs.Note: i use song ids to pull indvidual songs from db to be played by my song player!

My problem is i do not how to design a few tables that keep track of selected songs for each user along with the name of playlist for each user!! In processs i do not want to modify my current table i just want to add more tables to help me achive what i want. I be happy if an expert tell me how i can create tables that store play list(selected songs)info for each user and later i be able to query them easily in order to pull out playlist of each user.Thanks

Reply With Quote
  #2  
Old February 18th, 2006, 11:05 AM
Madpawn Madpawn is offline
My beat is correct.
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 339 Madpawn User rank is Private First Class (20 - 50 Reputation Level)Madpawn User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 3 m 33 sec
Reputation Power: 4
Well, I know you don't want modify your existing table, but your should normalize it.

This is what I'd do (all id field's are auto_incrementing primary keys: all *_id fields are foreign keys referring to the id field of the table represented by *; fore example, artist_id is an FK to the ARTIST table's id):

Table ARTIST
id
name

Table ALBUM
id
artist_id
name

Table SONG
id
artist_id
album_id
filename
title
track
comment
genre
filesize
bitrate

And that should take care of your general song DB. You could also break title and genre out into their own tables. I probably wouldn't bother with genre, but title's worth considering, since you can conceivably have a lot of repeated information there.

Anyway, on the playlists:

Table USER:
id
username
<other user-specific info>

Table PLAYLISTS:
id
user_id
name

Table PLAYLIST_SONGS
playlist_id
song_id
playlist_order

Note that PLAYLIST_SONGS doesn't have an id; all three columns are part of the PK (unless you want to allow repeated songs in the same playlist). playlist_order holds an integer to list the order in which the songs should be played.

Now you can pretty easily pull anyone's playlist. Say I (user id 55) want to run my 'rainy day' playlist:

Code:
SELECT
 u.username
,p.name
,s.filename
,s.title
FROM
 user AS u
INNER JOIN
 playlist AS p
ON
 u.id = p.user_id
INNER JOIN
 playlist_songs AS ps
ON
 p.id = ps.playlist_id
INNER JOIN
 songs AS s 
ON
 s.id = ps.song_id
WHERE
 u.id = 55
AND
 p.name = 'rainy day'
ORDER BY
 ps.song_order
  ASC


You can expand this query to list artist and album information, of course, though I think there are script-side ways to read that sort of information from id3 tags that may be preferable.
Comments on this post
Icon agrees: Normalize and create a many-to-many (MxN) table for the playlist
__________________
"A pawn is the most important piece on the chessboard -- to a pawn"


Last edited by Madpawn : February 19th, 2006 at 11:44 AM.

Reply With Quote
  #3  
Old February 19th, 2006, 04:02 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2005
Posts: 632 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 45 m 4 sec
Reputation Power: 3
If your playlist can contain doubles (..) you might still want to add a unique (primary) key to the playlist table to be able to delete specific records.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > How to store selected songs ids in db?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway