tags:

views:

105

answers:

3

This is really getting on my nerves, cant figure this one out

I want it to look like this: http://i27.tinypic.com/f406tz.png

But i cant get it to be like i want, it gets all f*ed up. It prints the rank on every row.

If three players have same rank, i only wanna print the rank name for one. the rest replace with  

$old_rank = '';
while ($g = mysql_fetch_object($q)) {

if ($g->rankname != $old_rank) {
    echo "<tr><td>$g->rankname</td>\n";
    $old_rank = "<tr><td>&nbsp;</td>\n";
}

echo " <td>$g->name</td></tr>\n";

}

what i want:

<tr>
<td>One</td>
<td>Kraven the Hunter</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Kull the Conqueror</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Zazi The Beast</td>
</tr>

<tr>
<td>Vice-leader</td>
<td>Igos du Ikana</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Saint Sinner</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Midvalley the Hornfreak</td>
</tr>.......................

What i get:

<tr><td>One</td>
<td>Tester</td></tr>
<tr><td>One</td>
<td>Kraven the Hunter</td></tr>
<tr><td>One</td>

<td>Kull the Conqueror</td></tr>
<tr><td>One</td>
<td>Zazi The Beast</td></tr>
<tr><td>Vice-Leader</td>
<td>Midvalley the Hornfreak</td></tr>
<tr><td>Vice-Leader</td>
<td>Saint Sinner

</td></tr>
<tr><td>Vice-Leader</td>
<td>Igos du Ikana</td></tr>
+6  A: 

$old_rank is never equal to $g->rankname because the way you are setting $old_rank, it will contain HTML tags, and the $g->rankname that you get from the DB will never have HTML tags.

Try changing your if statement to something like this:

if ($g->rankname != $old_rank) {
    echo "<tr><td>$g->rankname</td>\n";
    $old_rank = $g->rankname;
} else {
    echo "<tr><td>&nbsp;</td>\n";
}

It prints the rank name if it's a new rank name, else it prints empty space.

yjerem
A: 

I prefer breaking things up a bit more than that. Keeping things separate makes it easier to modify. This should work.

$old_rank = '';
while ($g = mysql_fetch_object($q)) {
    echo '<tr>' . "\n";
    echo '<td>';
    if ($g->rankname != $old_rank) {
        $old_rank = $g->rankname;
        echo $old_rank;
    } else {
        echo '&nbsp;';
    }
    echo '</td>';
    echo '<td>' . $g->name . '</td>' . "\n";
    echo '</tr>' . "\n";
}
Stephen
A: 

The following (notwithstanding typos) separates out the display logic from the database loop. This has the advantages: - You don't need to depend on the order of the results returned - You don't need to maintain dodgy logic (like 'old_rank') - You can display them more nicely (with a rowspan for repeated ranks

I believe the total code is more compact too.

// fill ranks array
$ranks = array();
while ( $g = mysql_fetch_object($q) ) {
    if ( !in_array($g->rankname, $ranks) ) {
        $ranks[htmlentities($g->rankname)] = array();
    }
    $ranks[$g->rankname][] = htmlentities($g->name);
}

// do other program logic here

// end of program
?>

<!-- display the page -->
<table>
    <tr>
        <th>Rank</th><th>Users</th>
    </tr>

    <?php foreach($ranks as $rankName => $userList): ?>
        <tr>
            <td rowspan="<?php echo (string)sizeof($userList); ?>">
                <?php echo $rankName; ?>
            </td>
            <td> <?php echo implode('</td></tr><tr><td>', $userList); ?> </td>
        </tr>
    <?php endforeach; ?>
</table>
Jhong