database help

  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

I have a database with 101 columns. 1 cd title 50 of them are cd track names. and the other 50 are for the URL of the sample's of the track...most of the cd's don't have that many tracks or sample's, so they are left empty in the database.

so it's like:

Title | track 1 | track 2 | track 3 .... | Sample 1 | Sample 2 | Sample 3....


not all cd's have 50 tracks, but I have 50 colums for tracks, just in case.

what i need to do, is write code to display the track names which are not empty, and also have track1 have a link to sample1 and track2 have a link to sample2 ... etc....but only if the sample column for that specific track is not empty, or else just dispay the track with no link.

how can i do this?

Thanks for the help!

Sevster
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

Well, for starters, that is a horrible table design, and will cause more headaches than you need (like the one you are experiencing right now).

I would break that table down to 3 seperate tables:
artist, album and track, that look like this:

Code: [ Select ]
CREATE TABLE `artist` (
 `intId` int(11) NOT NULL auto_increment,
 `strName` varchar(100) NOT NULL default 'unknown',
 PRIMARY KEY (`intId`)
) TYPE=MyISAM COMMENT='Artist Info' AUTO_INCREMENT=1 ;


CREATE TABLE `album` (
 `intId` int(11) NOT NULL auto_increment,
 `intArtistId` int(11) NOT NULL default '0',
 `strAlbumTitle` varchar(100) NOT NULL default 'unknown',
 `intYear` mediumint(4) NOT NULL default '1900',
 PRIMARY KEY (`intId`,`intArtistId`)
) TYPE=MyISAM COMMENT='Album info' AUTO_INCREMENT=1 ;


CREATE TABLE `track` (
 `intId` int(11) NOT NULL auto_increment,
 `intAlbumId` int(11) NOT NULL default '0',
 `strTrackTitle` varchar(100) NOT NULL default 'unknown',
 `strTrackLink` varchar(100) NOT NULL default '#',
 PRIMARY KEY (`intId`,`intAlbumId`)
) TYPE=MyISAM COMMENT='Track info' AUTO_INCREMENT=1 ;
  1. CREATE TABLE `artist` (
  2.  `intId` int(11) NOT NULL auto_increment,
  3.  `strName` varchar(100) NOT NULL default 'unknown',
  4.  PRIMARY KEY (`intId`)
  5. ) TYPE=MyISAM COMMENT='Artist Info' AUTO_INCREMENT=1 ;
  6. CREATE TABLE `album` (
  7.  `intId` int(11) NOT NULL auto_increment,
  8.  `intArtistId` int(11) NOT NULL default '0',
  9.  `strAlbumTitle` varchar(100) NOT NULL default 'unknown',
  10.  `intYear` mediumint(4) NOT NULL default '1900',
  11.  PRIMARY KEY (`intId`,`intArtistId`)
  12. ) TYPE=MyISAM COMMENT='Album info' AUTO_INCREMENT=1 ;
  13. CREATE TABLE `track` (
  14.  `intId` int(11) NOT NULL auto_increment,
  15.  `intAlbumId` int(11) NOT NULL default '0',
  16.  `strTrackTitle` varchar(100) NOT NULL default 'unknown',
  17.  `strTrackLink` varchar(100) NOT NULL default '#',
  18.  PRIMARY KEY (`intId`,`intAlbumId`)
  19. ) TYPE=MyISAM COMMENT='Track info' AUTO_INCREMENT=1 ;


This allows you to store each element seperately, yet still keep them linked.
The artist table stores only an auto-incremented ID and the Artist name.

The album table stores an auto-increment Album ID, the ID for the artist it belongs to (the ID from the artist table), and Album Title.

The track table stores the auto-increment track ID, the Album ID (From the album table), the track title and the link for the track.

Using this type of layout, allows you to search for all songs from a particular artist:
Code: [ Select ]
SELECT strTrackTitle, strTrackLink FROM track, album, artist WHERE artist.strName LIKE '%SEARCHSTERING%' and album.intArtistId=artist.intId and track.intAlbumId=album.intId
where SEARCHSTRING is an artist name (or part of one)

Or search for tracks from a particular album:
Code: [ Select ]
SELECT strTrackTitle, strTrackLink FROM track, album WHERE album.strAlbumTitle LIKE '%SEARCHSTERING%' and track.intAlbumId=album.intId

where SEARCHSTRING is an album title (or part of one)

Or to just search for a song by title:
Code: [ Select ]
SELECT strTrackTitle, strTrackLink FROM track WHERE track.strTrackTitle LIKE '%SEARCHSTERING%'

where SEARCHSTRING is an track title (or part of one)

There are other ways to search, those are just a few easy examples.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Ah normalization, you gotta love it
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

I lurve the database normalization :)
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

very interesting....thanks so much for that detailed explanation scienceofspock...i truly appreciate it. i'm going to try your suggestion now and see what happens :)

thanks again,

sevster
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

hmm, this is very nice, but looks like I'm not going to be able to change the way my database is. is there anyway that to do what i need using my current database which I stated above?
  • stephelton
  • Novice
  • Novice
  • User avatar
  • Posts: 20
  • Loc: Texas, USA

Post 3+ Months Ago

why can't you change it? even if you already have lots of data already, you can make a new table, test it, then copy your old information over either manually or using a script. In my experience, all the programming skills in the wolrd can't compensate for a lack of good design.
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

because my problem is only creating the URL links for each track name. everything else works. and my question about creating the URL links still isn't answered if I go through all that trouble and fix the database.
  • ryanb
  • Graduate
  • Graduate
  • ryanb
  • Posts: 226
  • Loc: Oregon, US

Post 3+ Months Ago

You really, really, really should learn relational database design (like ScienceOfSpock suggested); but, if you honestly can't change it, and you want your code to be as bad as the database design, try this:

PHP Code: [ Select ]
$albums = mysql_query('SELECT * FROM albums');
 
if (!$albums) {
 
   die('Unable to find albums in database: '. mysql_error());
 
}
 
 
 
while ($album = mysql_fetch_array($albums)) {
 
   $title = $album['title'];
 
 
 
   echo "<p><b>$title</b><br>\n";
 
 
 
   for ($i=1; $i <= 50; $i++) {
 
      $track = $album['track'. $i];
 
      $sample = $album['sample'. $i];
 
     
 
      if ($track) {
 
         if ($sample) {
 
            echo "<a href=\"$sample\">$track</a><br>\n";
 
         } else {
 
            echo "$track<br>\n";
 
         }
 
      }
 
   }
 
   
 
   echo "</p>\n";
 
}
  1. $albums = mysql_query('SELECT * FROM albums');
  2.  
  3. if (!$albums) {
  4.  
  5.    die('Unable to find albums in database: '. mysql_error());
  6.  
  7. }
  8.  
  9.  
  10.  
  11. while ($album = mysql_fetch_array($albums)) {
  12.  
  13.    $title = $album['title'];
  14.  
  15.  
  16.  
  17.    echo "<p><b>$title</b><br>\n";
  18.  
  19.  
  20.  
  21.    for ($i=1; $i <= 50; $i++) {
  22.  
  23.       $track = $album['track'. $i];
  24.  
  25.       $sample = $album['sample'. $i];
  26.  
  27.      
  28.  
  29.       if ($track) {
  30.  
  31.          if ($sample) {
  32.  
  33.             echo "<a href=\"$sample\">$track</a><br>\n";
  34.  
  35.          } else {
  36.  
  37.             echo "$track<br>\n";
  38.  
  39.          }
  40.  
  41.       }
  42.  
  43.    }
  44.  
  45.    
  46.  
  47.    echo "</p>\n";
  48.  
  49. }
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

that works quite well actually...

why is my database design so bad? I mean sometimes you just have to deal with what kind of database your client already has....so my client had a huge database which he had created in a bad way...so i have to work with it, right? and even if the database is bad, why do you consider this code "just as bad as my database design"?

I'm familiar with programming....especially in c++ and I can tell that even if I was using the database design like the one science of spock suggested, we would still need something like the code you wrote above, which i am very thankful for.


thanks again,

Sevster
  • ryanb
  • Graduate
  • Graduate
  • ryanb
  • Posts: 226
  • Loc: Oregon, US

Post 3+ Months Ago

sevster wrote:
why is my database design so bad?


In short, this design is not very flexible if you need to change something in the future. For example, what happens when you run into a CD with more than 50 tracks? And then what happens if you want to store the length of each track in the database? Well, I wouldn't want to add another 50 columns to a table. If you do relational database design, you only have to add one more column, and the number of tracks per CD is unlimited. Also, all of the unused NULL values make the database file size bigger too IIRC.

sevster wrote:
I mean sometimes you just have to deal with what kind of database your client already has....so my client had a huge database which he had created in a bad way...so i have to work with it, right?


If it was me, I'd definitally work on redoing the database with a cleaner design, even if it takes a day or two, it will be worth it in the end if you have to use it a lot in the future.

sevster wrote:
and even if the database is bad, why do you consider this code "just as bad as my database design"?


Because it also assumes there will be no more than 50 tracks in a CD, but you are right, the code isn't much longer than it would be with a relational database design, it is just not as flexible.

If you don't need to store the artist, etc. in the database, you can simplify ScienceOfSpocks suggestion. Here's my version:

Code: [ Select ]
CREATE TABLE albums (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(200) NOT NULL
);

CREATE TABLE tracks (
ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
AlbumID INT NOT NULL,
Title VARCHAR(200) NOT NULL,
SampleURL VARCHAR(200) NOT NULL
);
  1. CREATE TABLE albums (
  2. ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  3. Title VARCHAR(200) NOT NULL
  4. );
  5. CREATE TABLE tracks (
  6. ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  7. AlbumID INT NOT NULL,
  8. Title VARCHAR(200) NOT NULL,
  9. SampleURL VARCHAR(200) NOT NULL
  10. );


You can then use this code to do the same thing:

PHP Code: [ Select ]
$albums = mysql_query('SELECT * FROM albums');
 
if (!$albums) {
 
    die('Unable to find albums in database: '. mysql_error());
 
}
 
 
 
while ($album = mysql_fetch_array($albums)) {
 
   $albumID = $album['ID'];
 
    $albumTitle = $album['Title'];
 
   
 
    echo "<p><b>$albumTitle</b><br>\n";
 
   
 
    $tracks = mysql_query("SELECT * FROM tracks WHERE AlbumID='$albumID'");
 
    if (!$tracks) {
 
      die('Unable to find tracks in database: '. mysql_error());
 
   }
 
   
 
    while ($track = mysql_fetch_array($tracks)) {
 
        $trackTitle = $track['Title'];
 
        $trackSample = $track['SampleURL'];
 
       
 
      if ($trackSample) {
 
         echo "<a href="$trackSample">$trackTitle</a><br>\n";
 
      } else {
 
         echo "$trackTitle<br>\n";
 
      }
 
    }
 
   
 
    echo "</p>\n";
 
}
  1. $albums = mysql_query('SELECT * FROM albums');
  2.  
  3. if (!$albums) {
  4.  
  5.     die('Unable to find albums in database: '. mysql_error());
  6.  
  7. }
  8.  
  9.  
  10.  
  11. while ($album = mysql_fetch_array($albums)) {
  12.  
  13.    $albumID = $album['ID'];
  14.  
  15.     $albumTitle = $album['Title'];
  16.  
  17.    
  18.  
  19.     echo "<p><b>$albumTitle</b><br>\n";
  20.  
  21.    
  22.  
  23.     $tracks = mysql_query("SELECT * FROM tracks WHERE AlbumID='$albumID'");
  24.  
  25.     if (!$tracks) {
  26.  
  27.       die('Unable to find tracks in database: '. mysql_error());
  28.  
  29.    }
  30.  
  31.    
  32.  
  33.     while ($track = mysql_fetch_array($tracks)) {
  34.  
  35.         $trackTitle = $track['Title'];
  36.  
  37.         $trackSample = $track['SampleURL'];
  38.  
  39.        
  40.  
  41.       if ($trackSample) {
  42.  
  43.          echo "<a href="$trackSample">$trackTitle</a><br>\n";
  44.  
  45.       } else {
  46.  
  47.          echo "$trackTitle<br>\n";
  48.  
  49.       }
  50.  
  51.     }
  52.  
  53.    
  54.  
  55.     echo "</p>\n";
  56.  
  57. }


Yes, it is a little more code, but it is a ton more flexible than the other design.
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

both of you have been very helpful to me. I am greatly appreciative of this. Thank you so much guys.

one other thing I may add to this ...

the code you give me, I am modifying it to meet my needs. besides displaying the cd's, when you click on a specific cd, it opens up a new page and gives you more details about it....this new page, I wrote some code which actually creates a new php page on it's own and then displays it. But it creates one php page per cd, which I know is the wrong way to do it. Other than creating a new page, how can I go about doing what I need?

Thanks again,

Sevster
  • ryanb
  • Graduate
  • Graduate
  • ryanb
  • Posts: 226
  • Loc: Oregon, US

Post 3+ Months Ago

sevster wrote:
the code you give me, I am modifying it to meet my needs. besides displaying the cd's, when you click on a specific cd, it opens up a new page and gives you more details about it....this new page, I wrote some code which actually creates a new php page on it's own and then displays it. But it creates one php page per cd, which I know is the wrong way to do it. Other than creating a new page, how can I go about doing what I need?


I'm not sure I understand what you mean by "creates one php page per cd". Do you mean you wrote a PHP script that creates a new file for every CD? Or each CD is opening in a separate window and you want them to open in the same window? Or something else?

Are the extra CD details stored in the database along with the other 101 columns? :)
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

ryanb wrote:

I'm not sure I understand what you mean by "creates one php page per cd". Do you mean you wrote a PHP script that creates a new file for every CD? Or each CD is opening in a separate window and you want them to open in the same window? Or something else?

Are the extra CD details stored in the database along with the other 101 columns? :)


LOL! :) unfortunately they are....but I can retrieve them...that's not a problem.

I wrote a php script that creates a new file for every cd, yes. they open in the same window....but what I want, is to be able to do the same thing, but without having to create a separate file per cd.
  • ryanb
  • Graduate
  • Graduate
  • ryanb
  • Posts: 226
  • Loc: Oregon, US

Post 3+ Months Ago

If you have an ID field in your database, you can pass the ID of the CD you want to display in the URL. For example:

PHP Code: [ Select ]
$albumID = $album['ID'];
 
$albumTitle = $album['Title'];
 
 
 
echo '<a target="albuminfo" href="albuminfo.php?id='. $albumID .'">'. $albumTitle .'</a>';
  1. $albumID = $album['ID'];
  2.  
  3. $albumTitle = $album['Title'];
  4.  
  5.  
  6.  
  7. echo '<a target="albuminfo" href="albuminfo.php?id='. $albumID .'">'. $albumTitle .'</a>';


And then in the albuminfo.php file, get the CD with that ID in the database and display the info:

PHP Code: [ Select ]
$albumID = $_GET['id'];
 
 
 
$album = mysql_query("SELECT * FROM albums WHERE ID='$albumID'");
  1. $albumID = $_GET['id'];
  2.  
  3.  
  4.  
  5. $album = mysql_query("SELECT * FROM albums WHERE ID='$albumID'");
  • sevster
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 518

Post 3+ Months Ago

very nice! why didn't I think of that :)

thanks man,

Sevster

Post Information

  • Total Posts in this topic: 16 posts
  • Users browsing this forum: No registered users and 87 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.