Forum Moderators: coopster

Message Too Old, No Replies

Defeating Redundantly Repeating Redundancy

         

skiffdriver

3:35 am on Apr 3, 2010 (gmt 0)

10+ Year Member



I am displaying via PHP/MySQL a table that shows a list of musicians and the instruments they have played on a particular song (from a selection of many songs, with many different musicians playing many different instruments). So far, so good. It shows up on the page as something like this:

Tawny Port - Vocals
Seamus McSeamus - Autoharp
Seamus McSeamus - Flugelhorn
Seamus McSeamus - Concertina
Seamus McSeamus - Backing Vocals
Sven Torvald - Pedal Steel

See the problem? I would like the table to display as something like this: (the periods represent empty space)

Tawny Port - Vocals
Seamus McSeamus - Autoharp
............... - Flugelhorn
............... - Concertina
............... - Backing Vocals
Sven Torvald - Pedal Steel

I tried to use the MySQL count() function to fetch the number of rows that Seamus McSeamus appeared in and then insert that number as a rowspan attribute to the <td> for the musician's name, and got nothing helpful. I assume I'll have to do some kind of string matching in the while loop that generates the table, but I'm kind of at a loss to figure out what that might look like. Is there a PHP or MySQL function that covers this kind of redundancy-stripping? If not, can anyone suggest a starting point for experimentation? If I can figure out where to start, I can probably limp along the rest of the way.

Readie

9:39 am on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can think of a few ways to do this, I think perhaps the best way would be something like this:

<?php

$sql = 'SELECT artist_name, artist_role FROM music_table ORDER BY artist ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);

for($i = 0; $i < $rows; $i++) {
$roles[$i] = array(mysql_result($result, $i, "artist_name"), mysql_result($result, $i, "artist_role"));
}

$output = '<table cellspacing="2">
<tr>
<td><span style="font-weight: bold">Artist</span></td>
<td><span style="font-weight: bold">Role</span></td>
</tr>';

$count = count($roles);

for($i = 0; $i < $count; $i++) {
if($roles[$i][0] === $roles[($i - 1)][0]) {
$art = '-';
} else {
$art = $roles[$i][0];
}
$output .= "\n" . '<tr>' . "\n" . '<td>' . $art . '</td>' . "\n" . '<td>' . $roles[$i][1] . '</td>' . "\n" . '</tr>';
}
$output .= "\n" . '</table>';
echo $output;

?>
That should work, assuming I've made no simple mistake/typo.

rocknbil

6:38 pm on Apr 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Option two: if the artist name is the same as the previous, don't print it.


$artist_list=$name_value=$new_name=NULL;
$sql = "select artist_name, instrument from artists order by artist";
$result = mysql_query($sql);
while ($row=mysql_fetch_array($result)) {
$instrument = $row['instrument'];
$name_value = ($new_name==$row['artist_name'])?'--------':$row['artist_name'] . ' - ';
if ($row['artist_name'] != $new_name) { $new_name = $row['artist_name']; }
$artist_list .= "<li>$name_value $instrument</li>\n";
}
//
if ($artist_list) {
echo "
<h5>Artists:</h5>
<ul>$artist_list</ul>
";
}
else { echo "<p><em>Artist list for this album is not available at this time.</em></p>"; }


So as the artists change and the first time through, the artist name pops up. As long subsequent rows have the same name, it prints the dash lines.

skiffdriver

3:25 am on Apr 4, 2010 (gmt 0)

10+ Year Member



Thanks for the suggestions! I'll try these out (hopefully) tomorrow.