Displaying a Playlist from a Dropdown List

I sorta volunteered to help someone improve their website, but am not sure where to begin?! (Oops!) :slight_smile:

The simple version goes like this…

Each week the website in discussion displays a “playlist” of music that will be played for a show.

The problem is that after the show, the list is changed and so there is no history.

Certain people want to be able to go back and see what was played last week, two weeks ago, or two months ago.

So I suggested that the “Playlists” be put in a database - simple table likely - and allow users to select a given week and then have website display the given week.

Doing this will help the webmaster more easily manage “Playlists” and it will allow users to see the archived lists without taking up too much space on the website.

There is a radio station in Chicago that has example what I am suggesting located here.

How would you tackle this if you were using PHP and MySQL?

(Since I am still learning both, I’m not sure where to begin?!)

Debbie

How will people enter new playlists into the database?
Don’t forget to build an admin function for that.

See how you go with this to get you started.

Playlists class. Accesses database and returns array of lists
(you can extend this to then list songs for a known playlist)


<?php

class Playlists
{
	protected $db;
	
	
	/**
	* CONSTRUCTOR
	* @param object $db mysqli connection
	*/	
	public function __construct($db) {
		$this->db = $db;
	}
	
	
	/**
	* Get names and dates of most recent playlists
	* @param int $num
	* @return array
	*/
	public function get_recent_playlists($num = 10) {
		$num = (int)$num;
		if($num == 0) {
			$num = 10;
		}
		
		$query = "SELECT id, name, date FROM `playlists` ORDER BY date DESC LIMIT $num";
		$result = $this->db->query($query);
		if(!$result) {
			return false;
		}
		$playlists = array();
		while($row = $result->fetch_assoc()) {
			$playlists[] = $row;
		}
		
		return $playlists;
	}
}
?>

Use code. Uses the above class to create a select box listing the playlists


<?php
//Connect to MySQL
//do this in a common include
$db = new mysqli('localhost', 'my_username', 'password', 'my_database_name');
if($db -> connect_error) {
	exit("Unable to connect to database");
}

//create Playlists object and pass in MySQLi connection
$playlist_obj = new Playlists($db);

//Get array of 10 recent lists
if($lists = $playlist_obj -> get_recent_playlists()) {
	$select = "<select name='playlist'>\
";
	
	foreach($lists as $l) {
		$id = $l['id'];
		$date = date('n.j.Y', strtotime($l['date']));
		$name = htmlspecialchars($l['name']);
		
		$select .= "<option value='$id'>$date $name</option>\
";
	}
	
	echo $select, "</select>\
";
}
else {
	echo "<p>Playlists not available</p>";
}
?>

The presentation part (iterating the array of playlists to display the select) is fairly down and dirty procedural. It just happens to get that data from the get_recent_playlists() method. So you can put the Playlists class in another file and include it which keeps it out of the way.

I tend to use OOP as well.

If you would like a quick and simple demo of a class (object) hopefully this demo code will give you an idea of how classes work.

If you use the KISS principle and remember that classes basically consist of properties and methods to use/manipulate those properties then you should pick up at least the basics of OOP’ing pretty quickly.

 
<?php
 
class Book {
 
 //class properties
    private $title;
    private $author;
 
    /*------------------------------------------------------------
    ****   Class Methods   *****
    ------------------------------------------------------------*/
    public function __construct($newTitle, $newAuthor) {
     $this->title = $newTitle;
        $this->author = $newAuthor;
    }
 
    public function setTitle($newTitle) {
     $this->title = $newTitle;
    }
 
    public function setAuthor($newAuthor) {
     $this->author = $newAuthor;
    }
 
    public function getTitle() {
     return $this->title;
    }
 
    public function getAuthor() {
     return $this->author;
    }
 
} //end of class.  The above code would normally be stored in a separate php file
 
//testing code
 
//first create 2 new books
$mybook1 = new Book('OOP is Great','Fred Smith');
$mybook2 = new Book('I like OOP','Sam Jones');
 
//now display the current values stored in each book
echo 'Book 1 title = '.$mybook1->getTitle().'<br />';
echo 'Book 1 author = '.$mybook1->getAuthor().'<br />';
echo 'Book 2 title = '.$mybook2->getTitle().'<br />';
echo 'Book 2 author = '.$mybook2->getAuthor().'<br /><br />';
 
//now change some values in each book
$mybook1->setTitle('OOP is Great - 2nd Edition');
$mybook2->setAuthor('John Smith');
 
//now display the current values stored in each book
echo 'Book 1 title = '.$mybook1->getTitle().'<br />';
echo 'Book 1 author = '.$mybook1->getAuthor().'<br />';
echo 'Book 2 title = '.$mybook2->getTitle().'<br />';
echo 'Book 2 author = '.$mybook2->getAuthor().'<br /><br />';
?>
 

Mike,

Thank you for the effort. (Wow, I wasn’t expecting OOP?! I was just looking for down-n-dirty procedural code!)

I have to go to bed shorty and will be traveling for the next several days, but will do my best to digest your response and try to implement it.

I can see that it is going to take a lot to get me to understand OOP.

(It always makes sense when I read books, but then when I get to real-world examples and looking at other people’s code I just freeze…) :confused:

Debbie

Thanks for the reply, but I am comfortable with the database part.

What I need help with is the PHP part, like…

  • Setting up the drop-down list showing the “Show Weeks”
  • Taking the selected week and querying the database
  • Displaying the Playlist

Debbie

The first thing you would need to do before any coding is to build the database. You don’t want to be half way through coding your website and then realise…oooopss!!..my database structure is wrong.

In its simplest form you could have 2 tables similar to this

 
tblSongs
-----------
songID
title
 
tblPlaylists
-------------
playListID
name
songID
showDate

where songID in tblPlaylists must first exist in tblSongs.

Once your database is set up, then you can start coding to update and display records in the database as required.

For those too shy to visit my link above, here is what the output would look like… (Different content - obviously - but similar format.)

[COLOR=“green”][SIZE=“2”]ROCK & ROLL ROOTS FOR 08.29.10
Out in the Country

Joe Walsh - In the City
Foghat - Fool For the City
The Beach Boys - Then I Kissed Her
Bruce Springsteen - Blinded By the Light
Manfred Mann - Spirit in the Night
Neil Young - Are You Ready for the Country
Elton John - Country Comfort
The Mamas and The Papas - Go Where You Wanna Go
Fleetwood Mac - Monday, Monday
James Taylor - Country Road
Steve Miller Band - Going to the Country
[/SIZE][/COLOR]

Debbie