Filtering multiple results in MySQL result array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Zach
Forum Newbie
Posts: 2
Joined: Mon Jan 25, 2010 7:29 pm

Filtering multiple results in MySQL result array

Post by Zach »

Greets,

I'm currently working on a music related website. I need to list all albums by an artist, but the problem is, each song is an individual record in the database. So I'm doing a query to fetch all songs, and I need to just take one of each name out of the album field. If I haven't already confused you enough, here's my attempt at an example:

Code: Select all

 
 
|-----Album-----|-----Artist-----|-----Song-----|
| Blue               | Band              | Song 1           |
| Blue               | Band              | Song 2           |
| Blue               | Band              | Song 3           |
| Blue               | Band              | Song 4           |
| Green             | Band              | Song 1           |
| Green             | Band              | Song 2           |
| Green             | Band              | Song 3           |
| Green             | Band              | Song 4           |
|------------------------------------------------|
 
 
Ok, so there's an example of what the table looks like. Here's the PHP I have written:

Code: Select all

 
<?php
$artist = $_GET['artist']; // Band name retrieved from $_GET
$q = "SELECT * FROM songlist WHERE Band='$artist'";
$r = mysqli_query($db, $q); // db link from included file
 
$row = mysqli_fetch_array($r);
 
 ?>
 
Or it's something along those lines..

It all comes down to this.. how do I make an array that contains just the album names without duplicates? So $albums[] will contain Blue, Green instead of Blue, Blue, Blue, Blue, Green .... etc
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Filtering multiple results in MySQL result array

Post by AbraCadaver »

You can do this in the query:

Code: Select all

$q = "SELECT DISTINCT `Albums` FROM songlist WHERE Band='$artist'";
Or, to do it the way you have shown it, if you what to keep all of the columns in the array, then something like this should work:

Code: Select all

while($row = mysqli_fetch_array($r)) {
    $name = $row['Album'];
    $albums[$name][] = $row;
}
If you just want the albums, then:

Code: Select all

$albums = array();
while($row = mysqli_fetch_array($r)) {
    if(!in_array($row['Album'], $albums)) {
        $albums[] = $row['Album'];
    }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Zach
Forum Newbie
Posts: 2
Joined: Mon Jan 25, 2010 7:29 pm

Re: Filtering multiple results in MySQL result array

Post by Zach »

ok thanks! I'll give that a go.
Post Reply