Echoing Rankings in order with numbers

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Echoing Rankings in order with numbers

Post by Epiales »

Okay, I have a ranking / arena system in the works. Right now when I click the rankings / arena, it shows the users that have the highest strength, but I do not have numbers by them, as I'm not sure how to add that. I need it to show the members in order by strength, but with their ranking numbers in corellation with their strength: ex: 1,2,3,4,5 ect.. How would I write this in the php to echo their rank. I can echo their user ID, but that won't show their position in the rankings.

I'm assuming I'll have to add another row in the database to hold their ranking number and then echo / loop that accordingly, but not sure how to set it up.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Echoing Rankings in order with numbers

Post by KyleMassacre »

No you won't have to edit your database at all. What you can do is something like this:

Code: Select all

$i = 0;
while($someVar = true) {
    $i++;
    echo $i;
}
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Echoing Rankings in order with numbers

Post by Epiales »

KyleMassacre wrote:No you won't have to edit your database at all. What you can do is something like this:

Code: Select all

$i = 0;
while($someVar = true) {
    $i++;
    echo $i;
}
LMAO, I put

Code: Select all

$i = 0;
while($strength >= 1) {
    $i++;
    echo $i;
}
I had to control alt delete to get off firefox :lol: :lol: :lol: :lol: :lol:
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Echoing Rankings in order with numbers

Post by KyleMassacre »

Haha. I LOVE endless loops. I'm sure you have some sort of true statement in there to loop through your leader board, put it there. I was just showing you to set a value to increment up through your results.
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Echoing Rankings in order with numbers

Post by Epiales »

KyleMassacre wrote:Haha. I LOVE endless loops. I'm sure you have some sort of true statement in there to loop through your leader board, put it there. I was just showing you to set a value to increment up through your results.
Okay I tried to go off the ID

Code: Select all

<?php
while($row = mysql_fetch_array($sql2)){

    $get_user = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_user);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
 
    $id = $row['id'];
    $firstname = $row["username"];
    $family = $row["family"];
    $strength = $row["strength"];
    $i = 0;
    if ($row['id'] = true) {
    
     $i++;
}
    $outputList .= '<table border="0" bgcolor="590c02" width="99%"><td width="10%"><b><font color="white">' .$i. '</td><td width="15%"><b><font color="white">' . $firstname . '</td><td align="left" width="15%"><b><font color="white">' . $family  . '</td><td><b><font color="white"><img src="images/strength.png">' .number_format($strength) . '</td><td width="15%"><a href="attack.php?id='.$row['id'].'\"><img src="images/Handgun.png" width="30" height="30"></a> <img src="images/notification.png" width="30" height="30"> <img src="images/mail.png" width="30" height="30"></td><td width="5%"><img src="images/robber.png" width="20" height="20"></td></b></table>';
?>
It echo's nothing but 1's lol... I"m not good with loops
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
KyleMassacre
Posts: 573
Joined: Wed Nov 27, 2013 12:42 pm

Re: Echoing Rankings in order with numbers

Post by KyleMassacre »

Put the $i = 0; right above your while()
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Echoing Rankings in order with numbers

Post by Epiales »

KyleMassacre wrote:Put the $i = 0; right above your while()
That was simple enough, thx :mrgreen:
Nothing fancy, but a work in progress!

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

Re: Echoing Rankings in order with numbers

Post by Epiales »

Image
Nothing fancy, but a work in progress!

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

Re: Echoing Rankings in order with numbers

Post by Epiales »

Only problem there is though, is that it doesn't work with the pagination. So when I click to page two of the rankings, it starts that row at number 1 as well?
Nothing fancy, but a work in progress!

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

Re: Echoing Rankings in order with numbers

Post by Epiales »

Okay, code with pagination.

Code: Select all

$sql = mysql_query("SELECT username, family, strength, id FROM users ORDER BY strength ASC");

$nr = mysql_num_rows($sql); 

if (isset($_GET['pn'])) {  
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);  

} else { 
    $pn = 1;
} 

$itemsPerPage = 5; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {  
    $pn = 1;  
} else if ($pn > $lastPage) {  
    $pn = $lastPage;  
} 

$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
} else if ($pn == $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $sub2 . '">' . $sub2 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $add2 . '">' . $add2 . '</a>  ';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 


$sql2 = mysql_query("SELECT username, family, strength, id FROM users ORDER BY strength DESC $limit"); 

$paginationDisplay = "";  

if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '        ';

    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?rankings=1&pn=' . $nextPage . '"> Next</a> ';
    } 
}

$outputList = '';

    $i = 0; 
    
while($row = mysql_fetch_array($sql2)){

    if ($row['id'] = true) {
    
    $i++;

}    
    $get_user = "SELECT `username` FROM `users` WHERE `id`='".$row['id']."'";
    $user_query1 = mysqli_query($db_conx, $get_user);
    $numrows = mysqli_num_rows($user_query1);
    $rank_name = mysqli_fetch_assoc($user_query1);
 
    $id = $row['id'];
    $firstname = $row["username"];
    $family = $row["family"];
    $strength = $row["strength"];

    $outputList .= '<table border="0" bgcolor="590c02" width="99%"><td width="10%"><b><font color="white">' .$i. '</td><td width="15%"><b><font color="white">' . $firstname . '</td><td align="left" width="15%"><b><font color="white">' . $family  . '</td><td><b><font color="white"><img src="images/strength.png">' .number_format($strength) . '</td><td width="15%"><a href="attack.php?id='.$row['id'].'\"><img src="images/Handgun.png" width="30" height="30"></a> <img src="images/notification.png" width="30" height="30"> <img src="images/mail.png" width="30" height="30"></td><td width="5%"><img src="images/robber.png" width="20" height="20"></td></b></table>';
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”