Jump to content

Add a total in a scoreboard


scratch1

Recommended Posts

Hi All

Newbie question.

 

I'm trying to add a total of the $scores in a scoreboard at the bottom of the table.

 

Can anyone help please ?

 

This is what i have so far

function leaderboard()

{

$scores = get_scores();

$num_scores = count( $scores );

usort( $scores, 'cmp_scores' );

 

$HTML = '<table id="entries">' . "\n";

$HTML .= '<thead>' . "\n";

$HTML .= '<tr>' . "\n";

$HTML .= ' <th>BDM</th>' . "\n";

$HTML .= ' <th>Attendees</th>' . "\n";

$HTML .= '</tr>' . "\n";

$HTML .= '</thead>' . "\n";

$HTML .= '<tbody>' . "\n";

 

for ( $i = 0; $i < $num_scores; $i++ )

{

$HTML .= '<tr>' . "\n";

$HTML .= ' <td class="player">' . str_format( $scores[$i]['name'] ) . '</td>' . "\n";

$HTML .= ' <td class="player">' . $scores[$i]['points'] . '</td>' . "\n";

$HTML .= '</tr>' . "\n";

}

 

$HTML .= '</tbody>' . "\n";

 

$HTML .= '<tfoot>' . "\n";

$HTML .= '<tr>' . "\n";

$HTML .= ' <th colspan="2"></th>' . "\n";

$HTML .= '</tr>' . "\n";

$HTML .= '</tfoot>' . "\n";

 

$HTML .= '</table>' . "\n";

 

return $HTML;

 

Everything functions beautifully - apart from the total I need to show in the 'tfoot' section.

 

Thanks in advance

:D

Link to comment
Share on other sites

From your question, maybe this answer that you looking for

 

[cut]

$sum = 0; // dummy variable to calc total score

for ( $i = 0; $i < $num_scores; $i++ )
   {
      $HTML .= '<tr>' . "\n";
      $HTML .= '   <td class="player">' . str_format( $scores[$i]['name'] ) . '</td>' . "\n";
      $HTML .= '   <td class="player">' . $scores[$i]['points'] . '</td>' . "\n";
      $HTML .= '</tr>' . "\n";
      $sum += $scores[$i]['points']; //every loop, add the current score to dummy variable
   }

   $HTML .= '</tbody>' . "\n";

   $HTML .= '<tfoot>' . "\n";
   $HTML .= '<tr>' . "\n";
   $HTML .= '   <th colspan="2">' . $sum . '</th>' . "\n"; //output the result

[cut].....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.