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.

Re: Echoing Rankings in order with numbers

Postby Epiales » Wed Oct 08, 2014 10:11 pm

also noticed the above also removes the players ID that it's supposed to get for the attack and replaces it with the number 1 ID for ALL players. So when you hit the attack it only takes you to user 1. So I have some more work to figure this all out.
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 Winawer » Thu Oct 09, 2014 12:57 am

Code: Select all
if ($row['id'] = true) {


This line doesn't do what you think it does.
Winawer
 
Posts: 180
Joined: Wed Aug 17, 2011 12:53 am
Location: Finland
Has thanked: 0 time
Been thanked: 1 time

Re: Echoing Rankings in order with numbers

Postby KyleMassacre » Thu Oct 09, 2014 6:02 am

You can remove that if() all together because it's in your while() which anything inside your while() must be true in order to access that block of code
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 sniko » Fri Oct 10, 2014 5:35 am

Or just do it all within MySQL

Code: Select all
SET @position = 0;
SELECT
     username,
     family,
     strength,
     id,
     @position := @position + 1 AS position
FROM users
ORDER BY strength ASC


Now for a little demonstration...

Assume we have the following table, holding the following data;

Code: Select all
mysql> select * from users_power;
+---------+-------+-------------+
| user_id | name  | total_power |
+---------+-------+-------------+
|       1 | joe   |          21 |
|       2 | harry |          76 |
|       3 | cena  |          37 |
+---------+-------+-------------+
3 rows in set


This will bring back a result set like;

Code: Select all
mysql> SET @position = 0; select name, @position := @position + 1 AS position from users_power order by total_power DESC;
Query OK, 0 rows affected

+-------+----------+
| name  | position |
+-------+----------+
| harry |        1 |
| cena  |        2 |
| joe   |        3 |
+-------+----------+
3 rows in set
Image


Code: Select all
class Life extends Bitch {
  //...
}
sniko
 
Posts: 23
Joined: Fri Jan 17, 2014 9:56 am
Has thanked: 0 time
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby MikuzA » Fri Oct 10, 2014 7:26 am

Epiales wrote: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>'


What you could do is use the pagination-variable in use here..
instead of $i = 0;
put $i = $pn - 1; (if you add the $i++; after the '$outputList . =' Then you can have $i = $pn;

And as Winawer mentioned,
'    if ($row['id'] = true) {'
I guess your intention is not to define the $row['id'] here.. I believe this breaks your attack-links.


[EDIT] Hope my statement is understandable, looks like a mess of code when I read my own post :D
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
MikuzA
 
Posts: 395
Joined: Thu Aug 08, 2013 3:57 am
Location: Helsinki, Finland
Has thanked: 0 time
Been thanked: 21 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Sat Oct 11, 2014 10:17 am

MikuzA wrote:
Epiales wrote: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>';


What you could do is use the pagination-variable in use here..
instead of $i = 0;
put $i = $pn - 1; (if you add the $i++; after the '$outputList . =' Then you can have $i = $pn;

And as Winawer mentioned,
'    if ($row['id'] = true) {'
I guess your intention is not to define the $row['id'] here.. I believe this breaks your attack-links.


[EDIT] Hope my statement is understandable, looks like a mess of code when I read my own post :D



Okay, I tried this:

Code: Select all
$outputList '';
$i=$pn 1;

while(
$row mysql_fetch_array($sql2)){
$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);
    
    if(
$row['family'] == NULL){
    
$row['family'] = "No Family";
}
 
    
$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>'


It counts in order on the first page, but starts with number 2 on the second page instead of carrying over the last number from the first.
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 » Sat Oct 11, 2014 10:45 am

sniko wrote:Or just do it all within MySQL

Code: Select all
SET @position = 0;
SELECT
     username,
     family,
     strength,
     id,
     @position := @position + 1 AS position
FROM users
ORDER BY strength ASC


Now for a little demonstration...

Assume we have the following table, holding the following data;

Code: Select all
mysql> select * from users_power;
+---------+-------+-------------+
| user_id | name  | total_power |
+---------+-------+-------------+
|       1 | joe   |          21 |
|       2 | harry |          76 |
|       3 | cena  |          37 |
+---------+-------+-------------+
3 rows in set


This will bring back a result set like;

Code: Select all
mysql> SET @position = 0; select name, @position := @position + 1 AS position from users_power order by total_power DESC;
Query OK, 0 rows affected

+-------+----------+
| name  | position |
+-------+----------+
| harry |        1 |
| cena  |        2 |
| joe   |        3 |
+-------+----------+
3 rows in set


Using u're method I get the following:
Code: Select all
 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 352


What I used:

Code: Select all
$sql2 = mysql_query("SET @position = 0; SELECT username, family, strength, id, @position := @position + 1 AS position FROM users ORDER BY strength DESC $limit");


And the line with the error is the while statement

Code: Select all
while($row = mysql_fetch_array($sql2)){
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 MikuzA » Sat Oct 11, 2014 3:42 pm

Epiales wrote:
MikuzA wrote:
Epiales wrote: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>'


What you could do is use the pagination-variable in use here..
instead of $i = 0;
put $i = $pn - 1; (if you add the $i++; after the '$outputList . =' Then you can have $i = $pn;

And as Winawer mentioned,
'    if ($row['id'] = true) {'
I guess your intention is not to define the $row['id'] here.. I believe this breaks your attack-links.


[EDIT] Hope my statement is understandable, looks like a mess of code when I read my own post :D



Okay, I tried this:

Code: Select all
$outputList = '';
$i=$pn - 1;

while(
$row = mysql_fetch_array($sql2)){
$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);
    
    if
($row['family'] == NULL){
    $row['family'] = "No Family";
}
 
    $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>';


It counts in order on the first page, but starts with number 2 on the second page instead of carrying over the last number from the first.


Ah sorry,

try
$i = ($pn*$itemsPerPage)-5

$pn = (page number) 1,2,3,4,5
$itemsPerPage = 5

$i = 1*5-5 = 0 ( Will list 1,2,3,4,5)
$i = 2*5-5 = 5 ( Will list 6,7,8,9,10) etc.

Or something similiar
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
MikuzA
 
Posts: 395
Joined: Thu Aug 08, 2013 3:57 am
Location: Helsinki, Finland
Has thanked: 0 time
Been thanked: 21 times

Re: Echoing Rankings in order with numbers

Postby sniko » Mon Oct 13, 2014 6:59 am

Epiales wrote:
sniko wrote:Or just do it all within MySQL

Code: Select all
SET @position = 0;
SELECT
     username,
     family,
     strength,
     id,
     @position := @position + 1 AS position
FROM users
ORDER BY strength ASC


Now for a little demonstration...

Assume we have the following table, holding the following data;

Code: Select all
mysql> select * from users_power;
+---------+-------+-------------+
| user_id | name  | total_power |
+---------+-------+-------------+
|       1 | joe   |          21 |
|       2 | harry |          76 |
|       3 | cena  |          37 |
+---------+-------+-------------+
3 rows in set


This will bring back a result set like;

Code: Select all
mysql> SET @position = 0; select name, @position := @position + 1 AS position from users_power order by total_power DESC;
Query OK, 0 rows affected

+-------+----------+
| name  | position |
+-------+----------+
| harry |        1 |
| cena  |        2 |
| joe   |        3 |
+-------+----------+
3 rows in set


Using u're method I get the following:
Code: Select all
 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\login2\mafiawarskingdom.php on line 352


What I used:

Code: Select all
$sql2 = mysql_query("SET @position = 0; SELECT username, family, strength, id, @position := @position + 1 AS position FROM users ORDER BY strength DESC $limit");


And the line with the error is the while statement

Code: Select all
while($row = mysql_fetch_array($sql2)){


Try executing those in two different mysql_query()'s

  • mysql_ cannot perform multiple queries.
  • Don't use mysql_, it's deprecated
Image


Code: Select all
class Life extends Bitch {
  //...
}
sniko
 
Posts: 23
Joined: Fri Jan 17, 2014 9:56 am
Has thanked: 0 time
Been thanked: 6 times

Re: Echoing Rankings in order with numbers

Postby Epiales » Mon Oct 13, 2014 5:55 pm

Yah, I haven't rewrote the code for mysqli yet, and I've tried all the suggestions above and nothing works. I'll stop on this awhile and find a fix later, after I have written it in mysqli... Thanks all!
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

Previous

Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron

x