14.3. Code Example
The music sales website has made a deal with many of the artists on the site to create "mix tapes" of their band's music. Currently, this functionality is restricted to all of the available tracks from only one band. To start creating a variety CD, there are many avenues. The most popular is the option that is available when a visitor is viewing a band's CD page. A link exists to start the process of building a new mix CD. The process sends an ID that corresponds to that specific CD whose band has tracks that will be used.
The first building block of the process is the CD class. Generally, to construct a CD object, the information is retrieved from the database that matches the ID that was requested:
class CD
{
public $band = '';
public $title = '';
public $trackList = array();
public function __construct($id)
{
$handle = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('CD', $handle);
$query = "select band, title, from CDs where id={$id}";
$results = mysql_query($query, $handle);
if ($row = mysql_fetch_assoc($results)) {
$this->band = $row['band'];
$this->title = $row['title'];
}
}
public function buy()
{
//cd buying magic here
var_dump($this);
}
}
This class has the standard public properties of the $band, $title, and $trackList.
The constructor takes the ID in the form of the $id parameter and executes a query against that database. When that specific CD is found, the band and title are assigned to the public properties $band and $title, respectively. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access