PHP MySQL query, Top Players, font size decreases from top to bottom players, how-to?

Discussion in 'PHP' started by Doug the Great, Dec 15, 2007.

  1. #1
    Hello everyone, does anyone know I can accomplish this?

    I am using the phpArcade Script, I have a top players sections and I would like for it to be able to show the top 10 players. And number 1's font being the biggest, and traveling down the list it gets smaller and smaller. How can I accomplish this? This is the code I am working with.

    // This fuction displays the top players at the arcade
    function top_players($max_top_players,$sort_top_players,$base_url,$rewrite)
    {
    
      // Determine the top x amount of players based on points earned
      
      
      if ($sort_top_players == 1)
      {
      $sortby = "points";
      $wording = "points";
      }
      elseif ($sort_top_players == 2)
      {
      $sortby = "gamesplayed";
      $wording = "games";
      }
      else
      {
      $sortby = "points";
      $wording = "points";
      }
      
      $sql_query = "SELECT * from users WHERE userstatus = 1 ORDER BY $sortby DESC LIMIT $max_top_players";
      //store the SQL query in the result variable
      $result = mysql_query($sql_query);
      if(mysql_num_rows($result))
      {
      //output as long as there are still available fields
      while($row = mysql_fetch_array($result))
      {
    
      $topplayername = $row[username];
      $topplayerid = $row[userid];
      $topplayerpoints = $row[points];
      $topplayergames = $row[gamesplayed];
      $topplayeravatar = $row[avatar];
    
     // If the user doesn't have an avatar listed, this is the default avatar
                  if (!$topplayeravatar)
                  $topplayeravatar = $base_url."images/noavatar.gif";
      
    if ($sort_top_players == 2)
      $topplayernumber = $topplayergames;
      else
      $topplayernumber = $topplayerpoints;
    
      if ($rewrite ==1)
      $content.='<a href = "'.$base_url.'profiles/'.$topplayerid.'/'.make_friendly($topplayername).'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';
      else
      $content.='<a href = "'.$base_url.'index.php?action=profile&userid='.$topplayerid.'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';  
      }
      }
      //if no fields exist
      else
      {
    
      $content.='<i>No Top Players Yet</i>';
    
      }
    
    return $content;
    
    }
    PHP:
    Also how can I make every other player have a different alternating background?

    #1 - black background
    #2 - white background
    #3 - black background
    #4 - white background
    ...and so on.
     
    Doug the Great, Dec 15, 2007 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    thats easy man

    just count the total number of players which are displayed-> count query

    then devide count by total number of font sizes you have

    convert that value to int say nn

    then in while start the counter to max font size

    and decrease the counter after loop run nn times use counter to print player name

    Regards

    Alex
     
    kmap, Dec 15, 2007 IP
  3. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For alternating background colors you could modify your while loop like this:
    
    //output as long as there are still available fields
    	$style="top_item";
      while($row = mysql_fetch_array($result))
      {
    
      $topplayername = $row[username];
      $topplayerid = $row[userid];
      $topplayerpoints = $row[points];
      $topplayergames = $row[gamesplayed];
      $topplayeravatar = $row[avatar];
    
     // If the user doesn't have an avatar listed, this is the default avatar
                  if (!$topplayeravatar)
                  $topplayeravatar = $base_url."images/noavatar.gif";
      
    if ($sort_top_players == 2)
      $topplayernumber = $topplayergames;
      else
      $topplayernumber = $topplayerpoints;
    
    	$content.= '<div class="'.$style.'">';
      if ($rewrite ==1)
      $content.='<a href = "'.$base_url.'profiles/'.$topplayerid.'/'.make_friendly($topplayername).'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';
      else
      $content.='<a href = "'.$base_url.'index.php?action=profile&userid='.$topplayerid.'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';  
      	$content.= '</div>';
      
      	if ($style=="top_item") $style="top_alt_item";
      	else $style="top_item";
      
      }
    
    PHP:
    Then you add following code to your css stylesheet file:
    
    div.top_item
    {
    	background-color:#ffffff;
    }
    div.top_alt_item
    {
    	background-color:#000000;
    }
    
    Code (markup):
    In case you get confused here is complete php code:
    
    // This fuction displays the top players at the arcade
    function top_players($max_top_players,$sort_top_players,$base_url,$rewrite)
    {
    
      // Determine the top x amount of players based on points earned
     
     
      if ($sort_top_players == 1)
      {
      $sortby = "points";
      $wording = "points";
      }
      elseif ($sort_top_players == 2)
      {
      $sortby = "gamesplayed";
      $wording = "games";
      }
      else
      {
      $sortby = "points";
      $wording = "points";
      }
     
      $sql_query = "SELECT * from users WHERE userstatus = 1 ORDER BY $sortby DESC LIMIT $max_top_players";
      //store the SQL query in the result variable
      $result = mysql_query($sql_query);
      if(mysql_num_rows($result))
      {
      //output as long as there are still available fields
    	$style="top_item";
      while($row = mysql_fetch_array($result))
      {
    
      $topplayername = $row[username];
      $topplayerid = $row[userid];
      $topplayerpoints = $row[points];
      $topplayergames = $row[gamesplayed];
      $topplayeravatar = $row[avatar];
    
     // If the user doesn't have an avatar listed, this is the default avatar
                  if (!$topplayeravatar)
                  $topplayeravatar = $base_url."images/noavatar.gif";
      
    if ($sort_top_players == 2)
      $topplayernumber = $topplayergames;
      else
      $topplayernumber = $topplayerpoints;
    
    	$content.= '<div class="'.$style.'">';
      if ($rewrite ==1)
      $content.='<a href = "'.$base_url.'profiles/'.$topplayerid.'/'.make_friendly($topplayername).'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';
      else
      $content.='<a href = "'.$base_url.'index.php?action=profile&userid='.$topplayerid.'">'.$topplayername.'</a> ('.$topplayernumber.' '.$wording.')<br />';  
      	$content.= '</div>';
      
      	if ($style=="top_item") $style="top_alt_item";
      	else $style="top_item";
      
      }
      }
      //if no fields exist
      else
      {
    
      $content.='<i>No Top Players Yet</i>';
    
      }
    
    return $content;
    
    }
    
    PHP:
     
    hogan_h, Dec 16, 2007 IP