What PHP datatype am I using?

I have been looking at google, stack and other sites for 4 days trying to figure this out, so I appreciate any help you can offer ... I am n00b, so I apologize if I overuse any terms.

What I am trying to do:

I am trying to create a team site just for practice. I want to create a block of code that stores several pieces of information about a song, and then use this data in a music player. Each song will have its own player, which will show the song title, artwork for the song and let you play the song itself.

To save site load time and avoid repetitive code, I would like to use a loop to do this, so I have one piece of code for the player (s) and then create a separate iteration of the player for each song.

I think I can handle the looping aspect, but I can't figure out what datatype to use to store multiple pieces of song information. Will it be an array, a function, something else?

I am not looking for someone to code this for me, I just want to know which direction to head.

Here's a rough sketch of what I mean, without the correct syntax:

$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 will have a loop that will iterate over the player for each song.

Help?

+3


source to share


8 answers


You can save this information in several ways. The first one is a dimensional array muli, each song can have an element in the array and then contain a sub-matrix 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 is to create a class with 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>";

}

?>

      

+4


source


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 walk through $songs

. And you will get access to the title for example $songs['crashing']['Title']

.

However, this is the perfect time to use object-oriented programming. You can have a song class with attributes like title, path, etc., as well as a method called renderPlayer()

. This is a bit related to SE's answer, but you can google for a ton of information about OOP PHP.

In fact, Joe's answer is a great start in defining the class of the song.

+2


source


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

You can store it in an array and always have index 0 - song title, etc.

You can also store it in an associative array where the indices are strings instead of integers. Then maybe$song['songTitle']

The stripes collection can be kept the same, jus is nested.

0


source


If the use case is as simple as described, you can simply use an array:

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

      

And then you can access those fields like:

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

      

If you intend to do something more complex, you should probably create a class.

0


source


You can store all this information in what is 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
}

?>

      

The class is "reusable".

This is a sample class with some built-in functions so you can see how you can use them.

0


source


You can use an array to store your data (or .xml, .json file). For interaction, you can try (simple example, sorry):

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

foreach( $music as $key => $value ){
    echo $key . $value;
}

      

0


source


First you only need an associative array: one variable grouping some data together with easy to remember / readable / understandable names:

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

      

Pretty soon, you'll find that array programming is not the best way to create maintainable code. Not at all. So what you 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
}

      

In order not to write:

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

      

all the time you 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
    }
}

      

For added bones, you can create classes that represent an album (containing an array of songs) or a class Artist

that in turn contains an array of songs and an array of albums. This way your objects can be easily grouped.

Once you've done that, it's a simple matter of connecting these model classes to the relational database schema (where the album entry refers to the artist entry (foreign keys)) and each song refers to the album, so all data is well connected.

0


source


I did it in about 10 minutes, so he's not entirely sure, but I hope you know you are reading the UML (I obviously haven't done this in pure UML, but this is the original baseline) :)

UML for Song and SongManager Classes

PS: $ _DMB is a variable where you should store your database manager (pdo or wrapper)

0


source







All Articles