Reputation: 87
I have the following function that aims to fetch all credits from an artist for a particular song using the ID from the url ($id), and a foreach
statement that displays the information on the Web page. At the moment it's displaying the artist names fine, but the IDs aren't being displayed. How would I go about returning the ID information so it's displayed as well?
function getArtistsBySongId($id)
{
$query = "SELECT * FROM `Credit_To_Artist` AS c2a
INNER JOIN `Credits` AS cr ON cr.credit_id = c2a.credit_id
INNER JOIN `Artist` AS a ON a.artist_id = c2a.artist_id
LEFT OUTER JOIN `Song` AS s ON s.song_id = c2a.song_id
LEFT OUTER JOIN `Project` AS p ON p.project_id = s.project_id
WHERE c2a.song_id = $id";
$res = mysql_query($query);
$artists = Array();
$artisttoid = Array();
$songtoid = Array();
while( $row = mysql_fetch_array($res) ) {
$artist = $row[artist_name];
$credit = $row[credit_name];
$songcr = $row[song_id];
if(!array_key_exists($artist, $artists) ) {
$artists[$artist] = Array();
$artisttoid[$artist] = $row[artist_id];
$songtoid[$songcr] = $row[song_id];
}
$artists[$artist][] = $credit;
}
return $artists;
return $songtoid;
return $artisttoid;
}
I've used include's in the code because I'm still green to PHP and find it easier to understand.
<table border="0" cellspacing="5" cellpadding="5" class="cdinfo" width="100%;">
<tr>
<?php
if (getArtistsBySongId($id) == NULL) {
echo "<th style='font-size: 13px'>Credits:</th>";
echo "<td style='font-size: 13px'>There are currently no artists linked to this song.</td>";
} else {
include 'songs/getsongcredits.php';
}
?>
</tr>
</table>
songs/getsongcredits.php
<?php foreach (getArtistsBySongId($id) as $artist => $creditarr) {
$credits = implode( ", ", $creditarr );
echo "<a href='star.php?id={$artisttoid[$artist]}'>{$artist}</a> ({$credits})<br />";
} ?>
Upvotes: 0
Views: 178
Reputation: 26
I recomend you to return an object with each item you want in attributes. To return an object, firstly you need a class:
class MyClass {
private $artists;
private $songtoid;
private $artisttoid;
public function __construct($arg1, $arg2, $arg3){
$this->artists = $arg1;
$this->songtoid = $arg2;
$this->artisttoid = $arg3;
}
public function getArtists(){return $this->artists;}
public function getSongtoid(){return $this->songtoid;}
public function getArtisttoid(){return $this->artisttoid;}
}
In your function
function getArtistsBySongId(){
...
return new MyClass($artists, $songtoid, $artisttoid);
}
Also you can return an associative array like this
return array(
"artists"=>$artists,
"songtoid"=>$songtoid,
"artisttoid"=>$artisttoid
);
Or, if you want, you can return an array (as Machavity answered) and read the result using list()
function getArtistsBySongId(){
...
return array($artists, $songtoid, $artisttoid);
}
list($artists, $songtoid, $artisttoid) = getArtistsBySongId();
Upvotes: 1
Reputation: 31654
Objects or arrays are the way to do it
return array('artists' => $artists, 'songtoid' => $songtoid, 'artisttoid' => $artisttoid);
Upvotes: 4