Page 1 of 1

Need help

Posted: Thu Dec 24, 2009 12:23 pm
by dannon
Right, I have loads of fields in my table for my game, I am making a highscores for my game and for example some one searches for their name and all the stats comes up

I have

Code: Select all

 
$totalinfo ="SELECT * FROM skillsoverall where playerName = '$player' ORDER BY xp DESC";
$totalinfo2 = mysql_query($totalinfo) or die ("Could not connect to players database.");
$totalinfo3 = mysql_fetch_array($totalinfo2);
 
How can I make it so it will show how many fields down '$player' is ?

Re: Need help

Posted: Thu Dec 24, 2009 12:30 pm
by manohoo
Need more info. Show the table structure, and explain what you mean by "fields"... are you referring to table fields, player fields, game fields....

Also show the result of that query you are running, so that we can have a better idea of what's going on.

Re: Need help

Posted: Thu Dec 24, 2009 12:36 pm
by dannon
Ok,

Table name: skills

Fields : playerName, totalxp, lvl, attacklvl, attackxp, strengthlvl, strengthxp, defencelvl, defencexp, hitpointslvl, hitpointsxp, kills.

And all it has in it Player1 and all the stats and skills are set 123
And Player2 and all the stats and skills are set 321

I have another table for the players information like password, email and all that crap

Re: Need help

Posted: Thu Dec 24, 2009 1:12 pm
by manohoo
How can I make it so it will show how many fields down '$player' is ?
You still have not explained what you mean by "fields"

Re: Need help

Posted: Thu Dec 24, 2009 1:30 pm
by dannon
I just want to know how many fields down the player who has the most xp is, and make it print out on my website.
Image

Re: Need help

Posted: Thu Dec 24, 2009 1:51 pm
by manohoo
I don't have a straight answer for you. In MySQL you can do this:

set @i = 0;
select playerName, @i:=@i+1 as myrow from skills

capture the above in a PHP variable and you should be in the right track
... and don't forget to sort the rows properly, the sample you provided does not sort by field XP, as your original post indicated

Re: Need help

Posted: Thu Dec 24, 2009 3:16 pm
by dannon
So.. I clicked SQL button in myphpadmin and I put this in:

set @i = 0;
select @i:=@i+1 as myrow from skills

and clicked go.

Something this came up :

myrow
1
2
3

So I went to my PHP code and added:

Code: Select all

$rankinfo = "SELECT @i:=@i+1 AS myrow FROM skills WHERE playerName = '$player' ORDER BY xp DESC";
$rankinfo2 = mysql_query($rankinfo) or die ("Could not connect to players database.");
$rankinfo3 = mysql_fetch_array($rankinfo2);
And on my site I added this:

echo " <tr>\n";
echo " <th scope=\"row\">", $rankinfo3['myrow'], "</th>\n";
echo " </tr>\n";

but nothing came up.. I know I did something wrong.. can some one help me out please?

Re: Need help

Posted: Thu Dec 24, 2009 3:39 pm
by manohoo
try this for now, just to make sure that you get something from the database:

Code: Select all

$rankinfo = "SELECT playerName, @i:=@i+1 AS myrow FROM skills ORDER BY xp DESC";

Re: Need help

Posted: Thu Dec 24, 2009 4:59 pm
by dannon
Nothing happens

If I put this in
echo " <tr>\n";
echo " <th scope=\"row\">", $rankinfo['playerName'], "</th>\n";
echo " </tr>\n";

an S comes up

Re: Need help

Posted: Thu Dec 24, 2009 6:40 pm
by manohoo
Let's look at the contents of $rankinfo3. Do this:

Code: Select all

 
$rankinfo = "SELECT playerName, @i:=@i+1 AS myrow FROM skills ORDER BY xp DESC";
$rankinfo2 = mysql_query($rankinfo) or die ("Could not connect to players database.");
$rankinfo3 = mysql_fetch_array($rankinfo2);
echo "<pre>";
var_dump($rankinfo3);
 
What's the output?

Re: Need help

Posted: Fri Dec 25, 2009 4:09 pm
by dannon

Code: Select all

array(4) {
  [0]=>
  string(3) "Phq"
  ["playerName"]=>
  string(3) "Phq"
  [1]=>
  NULL
  ["myrow"]=>
  NULL
}
 

Re: Need help

Posted: Sat Dec 26, 2009 8:15 am
by dannon
I think i need to add

Code: Select all

set @i = 0;
somewhere and somehow into my php file

Re: Need help

Posted: Mon Dec 28, 2009 12:27 am
by manohoo
As an alternative, you can rank the players in PHP, as opposed to MySQL:

Code: Select all

 
$rankinfo = "SELECT playerName, XP FROM skills WHERE playerName = '$player' ORDER BY xp DESC";
$rankinfo2 = mysql_query($rankinfo) or die ("Could not connect to players database.");
 
$i=0; // rank
while($row = mysql_fetch_array($rankinfo2))
  {
  echo "rank: ".$i++." player: ". $row['playerName']." XP: ".$row['XP']."<br />";
  }
 

Re: Need help

Posted: Wed Dec 30, 2009 9:32 am
by dannon
thanks