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 />";
} ?>
Mitch
  • 75
  • 7
  • 1
    I would return an object. You could use an array as well. – ficuscr Jun 04 '15 at 20:31
  • 3
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jun 04 '15 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. – Barmar Jun 04 '15 at 20:35
  • Possible duplicate of [Multiple returns from function](http://stackoverflow.com/questions/3451906/multiple-returns-from-function) – miken32 May 29 '16 at 04:06

2 Answers2

4

Objects or arrays are the way to do it

return array('artists' => $artists, 'songtoid' => $songtoid, 'artisttoid' => $artisttoid);
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 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? – Mitch Jun 04 '15 at 20:54
  • Try my code above and do a `var_dump()` of the returned value and see if that works – Machavity Jun 04 '15 at 21:56
  • 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 – Machavity Jun 04 '15 at 23:04
  • 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. :/ – Mitch Jun 04 '15 at 23:36
  • `var_dump(getArtistsBySongId($id));` It does its own output – Machavity Jun 05 '15 at 00:32
  • This is well outside the scope of the question. You really need to look around, as plenty of tutorials exist for something this basic – Machavity Jun 05 '15 at 01:47
  • OK, appreciate your time anyway. – Mitch Jun 05 '15 at 02:01
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();
  • 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? – Mitch Jun 04 '15 at 21:23
  • 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); – Francisco Pacheco Jun 05 '15 at 13:13
  • I editted the answer, now it has the code to return an object – Francisco Pacheco Jun 05 '15 at 13:25