0

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 />";
} ?>
4
  • 1
    I would return an object. You could use an array as well. Commented Jun 4, 2015 at 20:31
  • 3
    If you can, you should stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's really not hard. Commented Jun 4, 2015 at 20:31
  • 1
    You should put quotes around the string indexes of your arrays, e.g. $row['artist_name']. Don't depend on PHP automatically quoting unrecognized constants. Commented Jun 4, 2015 at 20:35
  • Possible duplicate of Multiple returns from function Commented May 29, 2016 at 4:06

2 Answers 2

4

Objects or arrays are the way to do it

return array('artists' => $artists, 'songtoid' => $songtoid, 'artisttoid' => $artisttoid);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your reply @machavity. I tried this solution in my code but the output has gone all weird. For example, a song (song_id 455) with artist (artist_id 18) returns the result: 0 (Array) 1 (455) 2 (18) Do you know what's gone wrong?
Try my code above and do a var_dump() of the returned value and see if that works
All the array does is hold your data for the return. You still have to take it and output it correctly. Like I said, var_dump() it and then output it
Is it possible to ask for an example of using the var_dump() and outputting it? I had a look on PHP.net but am unsure where exactly to place it. Sorry to be a pain. :/
var_dump(getArtistsBySongId($id)); It does its own output
|
1

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();

3 Comments

Thanks for you reply @Francisco Pacheco. However, the above array return example doesn't seem to work in my code, would you be able to give an example of how to return an object?
To return an object, firstly you need a class: class MyClass { public $artists; public $songtoid; public $artisttoid; public function __construct($arg1, $arg2, $arg3){ $this->artists = $arg1; $this->songtoid = $arg2; $this->artisttoid = $arg3; } After this, you can return the object: return new MyClass($artists, $songtoid, $artisttoid);
I editted the answer, now it has the code to return an object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.