3

I've been staring at google, stack, and other sites for 4 days trying to get my head around this, so I appreciate any help you can offer... I'm a n00b, so apologies if I misuse any terms.

What I'm trying to do:

I'm trying to build a band website just for practice. I want to create a block of code that stores multiple pieces of information about a song, then use that data in a music player. Each song will have its own player that will show the title of the song, a piece of art for the song, and allow you to play the song itself.

To keep the site loading time down, and to avoid repetitive code, I'd like to use a loop to make it so I have one chunk of code for the player(s), then create a separate iteration of the player for each song.

I think I can handle the loop aspect, but I can't figure out which data type to use for storing multiple pieces of info about the song. Would this be an array, a function, something else?

I'm not looking for someone to code this for me, I just want to know what direction head in.

Here's a rough sketch of what I mean, without proper syntax obviously:

$song(crashing) {  
  $songTitle = "Crashing";  
  $songPath = "crashingSong.php";  
  $songArtwork = "crashingArt.php";  
}

$song(Juliana) {  
  $songTitle = "Juliana";  
  $songPath = "julianaSong.php";  
  $songArtwork = "julianaArtwork.php";  
}

$player {
  echo "<div class=\"titleHead\">" . $songTitle . "</div>";
  echo "<div class=\"link\">" . $songPath . "</div>";
  echo "<div class=\"artwork\">" . $songArtwork . "</div>";
}

Then I would have a loop that would iterate the player for each song.

Help?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
smadams
  • 41
  • 4

8 Answers8

4

You can store this info several ways. The first being a muli dimensional array, each song can have an element in an array, then contain a sub-array with the song data.

<?php

    //Store songs as multidemensional array
    $songs = array(
        'crashing' => array(
            'songTitle' => 'Crashing',
            'songPath' => 'crashingSong.php',
            'songArtwork' => "crashingArt.php"
        ),
        'Juliana' =>  array(
            'songTitle' => 'Juliana',
            'songPath' => 'julianaSong.php',
            'songArtwork' => "julianaArtwork.php"
        )
    );

    //Now iterate through each song
    foreach($songs as $title => $data) {

        echo "<div class=\"titleHead\">" . $data['songTitle'] . "</div>";
        echo "<div class=\"link\">" .  $data['songPath'] . "</div>";
        echo "<div class=\"artwork\">" . $data['songArtwork'] . "</div>";

    }   

?>

The second way would be to create a class with the songs and add some methods to help save and retrieve those songs.

<?php

class Songs {

    private $songs = array();

    // method declaration
    public function addSong($songTitle, $songPath, $songArtwork) {

        $this->songs[] = array(
            'songTitle' => $songTitle,
            'songPath' => $songPath,
            'songArtwork' => $songArtwork
        );

    }

    public function getSongs() {
        return $this->songs;
    }

}


//Initilaize songs class
$songRef = new Songs();

//Add SOngs
$songRef->addSong('Crashing', 'crashingSong.php', 'crashingArt.php');
$songRef->addSong('Juliana', 'julianaSong.php', 'julianaArtwork.php');

$songs = $songRef->getSongs();

//Now iterate through each song
foreach($songs as $key => $data) {

    echo "<div class=\"titleHead\">" . $data['songTitle'] . "</div>";
    echo "<div class=\"link\">" .  $data['songPath'] . "</div>";
    echo "<div class=\"artwork\">" . $data['songArtwork'] . "</div>";

}

?>
Goku Nymbus
  • 571
  • 2
  • 11
2

An associative array of arrays will do the job.

Something like:

$songs = array(
   'crashing' => array('Title' => "Crashing",  
                       'Path' => "crashingSong.php",  
                       'Artwork' => "crashingArt.php"
                  ), //<repeat for more songs>
    ); 

Then you can loop through $songs. And you would access the title like $songs['crashing']['Title'].

However, this is a perfect time to leverage object oriented programing. You could have a song class with attributes like title, path, etc and a method called renderPlayer(). This is a little involved to handle in an SE answer, but you can Google around for tons of information on OOP PHP.

Actually Joe's answer is a great start at a song class definition.

Dan
  • 10,614
  • 5
  • 24
  • 35
0

You are probably best defining a class to hold your information. See: http://php.net/manual/en/language.oop5.php

You could store it in an array and just always have index 0 be the song title, etc.

You could also store it in an associative array where the indexes are strings instead of integers. Then is could be $song['songTitle']

The collection of bands could be stored the same way, jus nested.

LavaSlider
  • 2,494
  • 18
  • 29
0

If the use case is as simple as what is described you could just use an array:

$songInfo = array( 
      'Crashing' => array('Title' => 'Crashing', 'Path' => "crashingSong.php", 'Artwork' => 'crashingArt.php')  
);

And then you can access those fields by e.g.:

$songInfo['Crashing']['Path'] 

If you intend to do anything more complicated, you should probably create a class.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

You can store all that information in what's called a class.

<?php 

class Song {
  private $Title;
  private $path;
  private $Artwork;
}

function Display(){
  echo "<div class=\"titleHead\">" . $this->DisplayTitle(). "</div>
        <div class=\"link\">" . $this->DisplayPath(). "</div>
        <div class=\"artwork\">" .$this->DisplayArtWork() . "</div>
       ";

function DisplayTitle() {
  echo $this->Title;
}

function DisplayPath(){
  echo $this->Path
}

function DisplayArtWork(){
  echo $this->Artwork
}

?>

A class is a "re-useable".

This is a sample class with some functions built in so you can see how they can be leveraged.

Joe Swindell
  • 684
  • 1
  • 7
  • 18
  • 5
    `var $Title`? PHP5 was introduced over 10 years ago. Also: `$this->Path` won't work, as your functions are not part of any class definition. Methods and functions don't `echo`, they `return`. And one last thing: _please_ check [the coding standards](http://www.php-fig.org), and apply them – Elias Van Ootegem Dec 29 '14 at 16:51
  • It's a simple adaption to OPS question. http://stackoverflow.com/questions/15300942/is-it-bad-practice-to-echo-out-functions-in-php – Joe Swindell Dec 29 '14 at 16:58
  • 3
    But it's ***broken code***, and it contains all sorts of bad practices (not specifying access modifier, using the 10-year-old, PHP-4 style `var` keyword, methods containing `echo`...). It's just bad code. I'm not trying to be mean here, just pointing out that this is a bad answer, IMO, for the reasons I mention. I commented, because this was the highest scoring answer when I saw it, and to give you the opportunity to _edit_ it. I didn't down-vote even... Mind you: care to explain why you -1 my answer? – Elias Van Ootegem Dec 29 '14 at 17:00
  • You were correct that I shouldn't use var. I've fixed that. Why do you assume I -1 your answer? – Joe Swindell Dec 29 '14 at 17:06
  • 1
    1 => your code is still broken (the functions are not methods, and they still echo instead of return). 2 => I happened to see your rep go down by 3 points, at around the time I got a -1, which lead me to believe you got a -1 on your answer (-2 rep), and down-voted yourself.... – Elias Van Ootegem Dec 29 '14 at 17:08
  • Functions can echo all they want. Read the link I posted about whether or not it's bad practice. I don't down vote good answers! Especially, people more knowledgeable then me on the subject. – Joe Swindell Dec 29 '14 at 17:10
  • But you use the return value of a function that returns nothing but echoes. That might work, if all functions are called in the right order, but it's a huge bug risk if other callers use that code. And your code would still fatal (and notice + warn on hhvm) if you run it. Using echo in functions that are not concerned with rendering data `display` in your answer makes them unusable in other contexts. You are limiting the reusability of your code without any benefit. – Rangad Dec 29 '14 at 17:13
  • Read the first comment below the question: echo is considered bad practice. If you want to, check any of my answers on code review, where I explain why this is... – Elias Van Ootegem Dec 29 '14 at 17:16
0

You can use array for save your data (or a file .xml, .json). For interact you can try (simple example, sorry):

$music = array(
    'songTitle'   => "Crashing",   
    'songPath'    => "crashingSong.php", 
    'songArtwork' => "crashingArt.php"
);

foreach( $music as $key => $value ){
    echo $key . $value;
}
Felipe
  • 1
  • 1
0

At first, you'll only really need an associative array: a single variable, grouping some data together, with easy to remember/read/understand names:

$song = array(
    'artist' => 'Foo',
    'Title'  => 'Bar',
    'Album'  => 'Foobar',
);//and so on

Pretty soon, though, you'll find that programming by array is not the best way to create maintainable code. Not by a long shot. So what you'd probably want to do is create a class:

class Song
{
    protected $artist = null;
    protected $title = null;
    protected $album = null;
    public function setArtist($artist)
    {
        $this->artist = $artist;
        return $this;
    }
    public function getArtist()
    {
        return $this->artist;
    }
    //add getters and setters for other properties here
}

To avoid having to write:

$song = new Song();
$song->setArtist('foo')
    ->setTitle('bar');//and so on

all of the time, you'll want to add a constructor:

public function __construct(array $data)
{
    foreach ($data as $key => $value)
    {
        $this->{'set'.ucfirst($key)}($value);//<-- of course, check if method exists first
    }
}

As an added bones, you can create classes that represent an album (containing an array of songs), or an Artist class, that in turn contains an array of songs, and an array of albums. That way, your objects can be grouped together easily.

Once you've done that, it's a simple matter of coupling those Model classes to a relational database schema (where an Album record references an artist record (foreign keys)), and each song references an album, so all the data is nicely linked together.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

I did it in about 10mins so its not perfect sure, but i Hope you know read UML (i obviously didn't made it in pure UML but that's a starting base) :)

UML for Song and SongManager Classes

PS : $_DMB is a variable where you have to stock your database manager (pdo or a wrapper)

Bobot
  • 1,118
  • 8
  • 19
  • for sure in the setters functions declaration you have to add the $data var or you will set nothing :D – Bobot Dec 29 '14 at 17:00