开发者

Unexpected output when comparing database column to variable

I have code which retrieves information about players from a MySQL database. I want to apply a special case to the HTML output if their ranking changes. I want it to look like this: http://i27.tinypic.com/f406tz.png

But i cant get it to be like i want, instead it prints the rank on every row:

$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>


$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.


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>


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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜