Top Players Scroll

Talk about game designs and what goes behind designing games.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Top Players Scroll

Post by Epiales »

I came across some code on the web while searching for the ability to scroll the top 10 players or however many you want to scroll. I did some changes to the code to work with the coding here for the games. It displays one name at a time from the database, starting with player ranked 1, and then fades in/out and then displays the next ranked player and so forth. It doesn't take up much room, which is what I wanted. I didn't want a list that showed all the top 10 at one time, as it takes more room. This way, it's only one name at a time, but still does its purpose.

Here is the code below... I only put 4 users in, but you can copy and paste the code for any additional users you want. Just follow the same outline to get certain members from database.

scroll.php or place it in your main document head/page:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mafia Wars Kingdom</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/style.css">

<style type="text/css" media="screen">
#showlist {
    background-color:black;
    width:150px;
    height:auto;
    border:#000000 solid 2px;
    text-align:center;
    vertical-align: top;
}

#showlist p {
    display:none;
    color:red;
}
</style>

<style type="text/css" media="print">
#showlist {
    display:none;
}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    $("#top15").load("toplist.php",function(){
        playerlist  = $("p.top15");
        var topplayers = Math.floor( Math.random() * playerlist.length );
        playerlist.eq(topplayers).addClass("list");
        fadeIt();
    })
});

function fadeIt(){
    $(".list").fadeIn(600).delay(5000).fadeOut(600, function(){
        $(this).removeClass("list");
        $(this).next("p.top15").addClass("list");
        if($(".list").length==0) $("p.top15:first").addClass("list");
        fadeIt();
    });
}
</script>

</head>
<body>

<div id="showlist"><center><font color="white">Top Players</font></center>
   <div id="top15">
</div>
</div>

</body>
</html>
PHP file that holds the coding to get users from database:

Code: Select all

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mafia Wars Kingdom</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/style.css">

<?php /*?> DATABASE CONNECTION <?php */?>

<?php
$db_conx = mysqli_connect("localhost", "root", "password", "database name");
// Evaluate the connection
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}// else {
//echo "Successful database connection!!!";
//}
?>
<?php /*?> PLAYER ONE DATABASE CODE <?php */?>
<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 1"; 
$user_query = mysqli_query($db_conx, $selectplayer);

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $get_player = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_player);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
    
    $firstname = $row["username"];
    $player1 = $row['username'] +1;
?>
<p class="top15">    
<?php $player1 .= "  $firstname"; 
echo $player1;    ?>
</p>

<?php /*?> PLAYER TWO DATABASE CODE <?php */?>
<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 2,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $get_player = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_player);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
    
    $firstname = $row["username"];
    $player2 = $row['username'] +2;
?>
<p class="top15">    
<?php $player2 .=  "  $firstname"; 
echo $player2; ?>
</p>

<?php /*?> PLAYER THREE DATABASE CODE <?php */?>
<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 3,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    
    $get_player = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_player);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
    
    $firstname = $row["username"];
    $player3 = $row['username'] +3;
?>
<p class="top15">    
<?php $player3 .=  "  $firstname"; 
echo $player3; ?>
</p>

<?php /*?> PLAYER FOUR DATABASE CODE <?php */?>
<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 4,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);

while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $get_player = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_player);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
    
    $firstname = $row["username"];
    $player4 = $row['username'] +4;
?>
<p class="top15">    
<?php $player4 .=  "  $firstname"; 
echo $player4; ?>
</p>

<?php /*?> ALL THE ENDING BRACKET THINGIES TO CLOSE IT ALL OUT <?php */?>
<?php
}}}}

 ?>
EXAMPLE:

Image

I hope someone finds it useful :mrgreen:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Top Players Scroll

Post by Callan S. »

Thanks, Epiales - High score showing code is always useful, as there are many ways to do it and high scores are always good to show! :D
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Top Players Scroll

Post by MikuzA »

Hello,

Had a hard time understand how that's built, you have 8 queries to users table if you have a 'top4' list?

I'm just wondering why it's built as it is, I might be missing something here..
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Top Players Scroll

Post by Epiales »

MikuzA wrote:Hello,

Had a hard time understand how that's built, you have 8 queries to users table if you have a 'top4' list?

I'm just wondering why it's built as it is, I might be missing something here..
And I have it done that way b/c I don't know if there is another way to cram it all into one query because I'm calling the difference player numbers separately.

$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 1";
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 2,1";
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 3,1";
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 4,1";

Those are the four I thought I had to call each one separately because of calling individual numbers?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Top Players Scroll

Post by Epiales »

Okay, I see what u're showing me lol... I was tired and just threw it together quickly... is this any better? Is there a way to even consolidate it more?

Code: Select all

<?php
 $player = '';
 $player2 = '';
 $player3 = '';
 $player4 = '';
?>
<?php /*?> PLAYER ONE DATABASE CODE <?php */?>

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 1";
$user_query = mysqli_query($db_conx, $selectplayer);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $firstname = $row["username"];
    $player = $row['username'] +1;
?>
<p class="top15">    
<?php $player .= "  $firstname"; 
echo $player;    ?>
</p>

<?php /*?> PLAYER TWO DATABASE CODE <?php */?>

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 2,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $firstname = $row["username"];
    $player2 = $row['username'] +2;
?>
<p class="top15">    
<?php $player2 .= "  $firstname"; 
echo $player2;    ?>
</p>

<?php /*?> PLAYER THREE DATABASE CODE <?php */?>

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 3,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $firstname = $row["username"];
    $player3 = $row['username'] +3;
?>
<p class="top15">    
<?php $player3 .= "  $firstname"; 
echo $player3;    ?>
</p>

<?php /*?> PLAYER FOUR DATABASE CODE <?php */?>

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit 4,1"; 
$user_query = mysqli_query($db_conx, $selectplayer);
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {

    $firstname = $row["username"];
    $player4 = $row['username'] +4;
?>
<p class="top15">    
<?php $player4 .= "  $firstname"; 
echo $player4;    ?>
</p>
<?php /*?> ALL THE ENDING BRACKET THINGIES TO CLOSE IT ALL OUT <?php */?>
<?php
}}}}
?>
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Top Players Scroll

Post by MikuzA »

Ah, ok, now I understand the reason of your multi-query.

That works yes, but I see alot of work when you would like to change the player amount.

So to make it more easier, I would suggest a loop.
Since every value can be predicted you could throw it inside a loop and just use query the database once.

Something like this to get top 5, note that I haven't tested the below code, just an example on how it could be approached.

Code: Select all

$limiter = 5; // the 'limit 5' for the query

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit ".$limiter;
$user_query = mysqli_query($db_conx, $selectplayer);

$loopyloop = 1;
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    $player = $row['username'] +$loopyloop;
    $player .= "  ".$firstname;
   echo "<p class=\"top15\">";
   echo $player;
   echo "</p>";
   $loopyloop++;
}
Also, I had difficulties to understand the reason behind

Code: Select all

$player = $row['username']+1;
Like if my username would be 'mikuza', mikuza+1 = ?
Is the username a Integer-value? Shouldn't be just like $player = "1";

Or did I analyse this code correctly that the $player variable should contain "2 Thomas" (as in your picture)?

Oh and I see that you have single row queries in all inside each others while-loops, I guess that's not needed right?
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Top Players Scroll

Post by Epiales »

MikuzA wrote:Ah, ok, now I understand the reason of your multi-query.

That works yes, but I see alot of work when you would like to change the player amount.

So to make it more easier, I would suggest a loop.
Since every value can be predicted you could throw it inside a loop and just use query the database once.

Something like this to get top 5, note that I haven't tested the below code, just an example on how it could be approached.

Code: Select all

$limiter = 5; // the 'limit 5' for the query

<?php
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit ".$limiter;
$user_query = mysqli_query($db_conx, $selectplayer);

$loopyloop = 1;
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    $player = $row['username'] +$loopyloop;
    $player .= "  ".$firstname;
   echo "<p class=\"top15\">";
   echo $player;
   echo "</p>";
   $loopyloop++;
}
Also, I had difficulties to understand the reason behind

Code: Select all

$player = $row['username']+1;
Like if my username would be 'mikuza', mikuza+1 = ?
Is the username a Integer-value? Shouldn't be just like $player = "1";

Or did I analyse this code correctly that the $player variable should contain "2 Thomas" (as in your picture)?

Oh and I see that you have single row queries in all inside each others while-loops, I guess that's not needed right?
I worked with u're code and this works fine:

Code: Select all

<?php
$limiter = 5;
$selectplayer ="SELECT username, id FROM users ORDER BY strength DESC limit ".$limiter;
$user_query = mysqli_query($db_conx, $selectplayer);

$loopyloop = 1;
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
    $username = $row['username'];
    $player = $row['username'] +$loopyloop;
    $player .= "  ".$username;
   echo "<p class=\"top15\">";
   echo $player;
   echo "</p>";
   $loopyloop++;
}
?>
Had to add the username there....For some reason, running it as is only gave the user ID and not their name. Only way I could get their name was to add what I did. Also, the

Code: Select all

$player = $row['username']+1;
The +1 and so on was for the ranking. The code worked, but you simplified it better lol... :oops:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Top Players Scroll

Post by MikuzA »

Simplified yes, but that's not always good in learning perspective.
Let us say the original was a version 1 to understand how to get it to work.
And my example was just doing the same thing but avoiding code repetition.

Since PHP allows people to create various code that end up with the same result, this is a good example that multiple rows give the same answer as a few rows.

There could be scenarious when something just cannot be looped, usually if something isn't, I change it to loopable.

Loop instead of repeat, that makes the code readable and fresh when you look it again after 3-4 years :)
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Top Players Scroll

Post by Epiales »

MikuzA wrote:Simplified yes, but that's not always good in learning perspective.
Yeah, I didn't update the original post, as I liked the idea of being able to see the start and end result. Thanks :)
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Game Design”