开发者

Image to the first row

开发者 https://www.devze.com 2023-02-14 21:11 出处:网络
Running a mysql query that returns users with their daily highscores. This is the php file that displays the highscore:

Running a mysql query that returns users with their daily highscores.

This is the php file that displays the highscore:

<table border="0" width="100%"><?php echo highscore()?>
    </table>

This is the php that handles the mysql query (highscore function):

echo '<tr>';
    echo '<td>'.$user.'</td><td align="right">'.$score.'</td>';
    echo '</tr>';

The mysql query that I have results in the user with th开发者_Python百科e highest score getting on top of the table that you see above.

Now I would like to add a crown to the user that has the most points.

The question is how to add an image(crown.png) to the first row which is the user with the highest score.

Thanks in advance.


I assume you order users by score DESC.

$set = 0;
while() {
    echo '<tr';
    echo '<td>';
    if(!$set) {
        echo '<img src="crown.png" alt="crown" />';
    }

    // ... the rest of the code

    $set = 1;
}


if (empty($notfirst)){
  echo "crown";
  $notfirst=1;
}


i only see a quick and dirty solution, given the code you provide.

I guess you call this code in a loop?

echo '<tr>';
echo '<td>'.$user.'</td><td align="right">'.$score.'</td>';
echo '</tr>';

so just declare a variable before you start the loop:

$first = true;

then inside your loop, change your code to:

echo '<tr>';
echo '<td>'.$user.'</td><td align="right">'.$score.'</td><td>'.$first?'<img src=\"crown.png\" />':''.'</td>';
echo '</tr>';
$first = false;


If you wanted to do this sans PHP you could make your table and use CSS3's new :first-child pseudo-selector to add the crown as a background image to the first row.

table tr:first-child { background: url("crown.gif") no-repeat; }
0

精彩评论

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