Need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Need help

Post 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 ?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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.
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post 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
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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"
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post 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
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post 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?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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";
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post 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
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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?
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post by dannon »

Code: Select all

array(4) {
  [0]=>
  string(3) "Phq"
  ["playerName"]=>
  string(3) "Phq"
  [1]=>
  NULL
  ["myrow"]=>
  NULL
}
 
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post by dannon »

I think i need to add

Code: Select all

set @i = 0;
somewhere and somehow into my php file
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Need help

Post 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 />";
  }
 
dannon
Forum Newbie
Posts: 10
Joined: Wed Dec 23, 2009 10:14 am

Re: Need help

Post by dannon »

thanks
Post Reply