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.

Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 8:01 pm

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
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby KyleMassacre » Wed Oct 08, 2014 8:10 pm

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;
}
Skype: KyleMassacre
My NWE Modules
User avatar
KyleMassacre
 
Posts: 572
Joined: Wed Nov 27, 2013 7:42 am
Has thanked: 19 times
Been thanked: 36 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 8:34 pm

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
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby KyleMassacre » Wed Oct 08, 2014 8:36 pm

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.
Skype: KyleMassacre
My NWE Modules
User avatar
KyleMassacre
 
Posts: 572
Joined: Wed Nov 27, 2013 7:42 am
Has thanked: 19 times
Been thanked: 36 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 8:50 pm

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>&nbsp;<img src="images/notification.png" width="30" height="30">&nbsp;<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
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby KyleMassacre » Wed Oct 08, 2014 8:51 pm

Put the $i = 0; right above your while()
Skype: KyleMassacre
My NWE Modules
User avatar
KyleMassacre
 
Posts: 572
Joined: Wed Nov 27, 2013 7:42 am
Has thanked: 19 times
Been thanked: 36 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 8:58 pm

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: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 9:00 pm

Image
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 9:07 pm

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: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 9:21 pm

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

$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. '&nbsp;  &nbsp;  &nbsp; ';

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

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

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '&nbsp;  <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>&nbsp;<img src="images/notification.png" width="30" height="30">&nbsp;<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
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Next

Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x