SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Displaying a Playlist from a Dropdown List

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

    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

  2. #2
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For those too shy to visit my link above, here is what the output would look like... (Different content - obviously - but similar format.)


    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


    Debbie

  3. #3
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
     
    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.

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kalon View Post
    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

    Code:
     
    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.
    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

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 Code:
    <?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'>\n";
        
        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>\n";
        }
        
        echo 
    $select"</select>\n";
    }
    else {
        echo 
    "<p>Playlists not available</p>";
    }
    ?>

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,529
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    See how you go with this to get you started
    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...)



    Debbie

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
     
    <?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 />';
    ?>

  9. #9
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    How will people enter new playlists into the database?
    Don't forget to build an admin function for that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •