Page 1 of 1

Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:01 am
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.

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:10 am
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;
}

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:34 am
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:

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:36 am
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.

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:50 am
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

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:51 am
by KyleMassacre
Put the $i = 0; right above your while()

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 1:58 am
by Epiales
KyleMassacre wrote:Put the $i = 0; right above your while()
That was simple enough, thx :mrgreen:

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 2:00 am
by Epiales
Image

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 2:07 am
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?

Re: Echoing Rankings in order with numbers

Posted: Thu Oct 09, 2014 2:21 am
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>';