Jump to content

Get row number???


Vivid Lust

Recommended Posts

Hi, I have this page:

 

http://*domain_removed*/projects/phishfriends/leader.php

 

What it does is gets the user info from the database and orders it in order of phishes and then displays.

 

It also has the rank which goes up everytime.

 

Is there any way in which I can have some script which gets the rank of a certain name????

 

Code:

 

<?php
require("includes/db.php");
$sql = "select * from users order by `s_phish` desc";
$result = mysql_query($sql, $link)
or die(mysql_error());
?>
<center>
<table cellpadding="0" width="50%" cellspacing="0">
<tr>
<td>
Rank
</td>
<td>
Name
</td>
<td>
Phishes
</td>
</tr>
<?php
$rank = 0;
while ($row = mysql_fetch_array($result) AND $rank < 20) {
     $rank++;   // adds 1
     echo "<tr><td>";
     echo $rank;
     echo "</td><td>";
     echo "  <a href='profile.php?id=";
     echo $row['id'];
     echo "'>";
     echo $row['name'];
     echo "</a> ";
     echo "</td><td>";
     echo $row['s_phish']; // put HTML break to end line
     echo "</td></tr>";
     echo "<br />"; 
}
?>
</table>
</center>

 

Thanks for any help in advanced.

 

Edit (Daniel0): Removed domain by request of user.

Link to comment
Share on other sites

I wrote this, just displays 0, any help appreciated

 

<?php
require("includes/db.php");
$sql = "select * from users order by `s_phish` desc";
$result = mysql_query($sql, $link)
or die(mysql_error());
?>
<?php
while ($row = mysql_fetch_array($result)){
$rid = $row['id']; //id of member
}
$rank = 0;
while ($row = mysql_fetch_array($result) AND $rid !== 5 ) {
     $rank++;  

}
echo $rank; 
?>

Link to comment
Share on other sites

You're not explaining yourself clearly. I'm not understanding you, however the example code you posted is not recommended for use. Perhaps you mean

<?php
require("includes/db.php");
$sql = "select * from users WHERE id != 5 order by `s_phish` desc";
$result = mysql_query($sql, $link)
or die(mysql_error());
?>
<?php

echo  mysql_num_rows($results) . ' results returned:<br />';

while ($row = mysql_fetch_array($result))
{
    echo '<pre>'.print_r($row, true).'</pre>';
}

?>

Link to comment
Share on other sites

I want to try and get the rank of a member.

 

Say I had this database:

 

id  score

1    10

2    34

3    3

3    0

 

My mysql query would get that data and order so its like this:

 

id  score

2    34

1      10

3      3

4        0

 

Now, I want to specify a certain ID and find its rank.

 

Say i wanted to find the rank of id 4, the program would return the value 4, if i wanted the rank of id 2, the code would return the value 1

 

This is what im trying to do, and I cant figure it out.

 

I hope this is clearer

And some kind person is willing to help!

Link to comment
Share on other sites

Well you've already ordered by phish .. descending...which is what you want

 

all you need to do is put these results into an array and you'll be able to pull them out by number....or rank if you will.

 

 

Just a modification of wildteens script

while ($row[] = mysql_fetch_assoc($result)) { //If you want to do something during the loop, do it}

Now the person who is ranked second will be here

$row[1]

and his score will be $row[1]['s_phish']

Link to comment
Share on other sites

Hi, doesn't seem to be working quite right,

 

Have a look here: http://onestamp.co.uk/projects/phishfriends/profile.php?id=2

Then click on the name to see what the actual score is

 

I think the problem is because the value is 0, and it ranks them all the same???

 

How can I correct this ;)

if 2 users have same score do they have same rank or not

if not who has beter rank

Link to comment
Share on other sites

I didnt understand what you are trying to say sasa, sorry.

 

Anyone willing to modify the code for me so it does what I said above??

 

$userid = 3;

$sql = "SELECT COUNT(*) + 1 as rank
           FROM users
           WHERE score > (SELECT score FROM users WHERE id = '$userid')";
$res = mysql_query($sql);
if (mysql_num_rows($res) > 0)
{
    echo "User $userid is position " . mysql_result($res, 0, 'rank');
}

Link to comment
Share on other sites

All this is doing, is adding one onto it, which isnt what I want, sorry.

 

Have a look here: http://onestamp.co.uk/projects/phishfriends/profile.php?id=2

Then click on the name to see what the actual score is. All of the people with the same score, have the same rank!

 

I think the problem is because the value is 0, and it ranks them all the same??? I dont want this, how can I fix this???

 

Thanks

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.