Re: [PHP][Help] Ranking Page
Hello guys,
Even though this problem is solved, please take a look at the OOPHP5 way of doing this solution, I'd recommend coding games in OOP, I'm currently developing a game also, check my introduction topic to see more of it.
PHP Code:
<?php
class Ranking
{
#instance variables here =]
protected $connection;
protected static $jobs = array(1 => "Mercenary", 2 => "Acrobat", 3 => "Assist", 4 => "Magician",
6 => "Knight", 7 => "Blade", 8 => "Jester", 9 => "Ranger",
10 => "RingMaster", 11 => "BillPoster", 12 => "Psykeeper",
13 => "Elementor");
public function __construct()
{
#lets create the connection stream =]
$this->connection=new MySQLi("localhost", "username", "password", "database");
}
public function __destruct()
{
#close the connection as we're no longer using it ;)
$this->connection->close();
}
protected function FindJob($jobId)
{
#has the value been pre-stored in the array? o_O
if(array_key_exists((int)$jobId, (array)self::$jobs))
{
return (string)self::$jobs[$jobId];
}
}
public function DisplayStats()
{
#lets assign the query to a variable
$sql="SELECT * FROM characters ORDER BY level DESC";
$query=$this->connection->query($sql);
#has the query got any rows? o_O
if($query->num_rows > 0)
{
#loop out teh results
while($sqlData=$query->fetch_array(MYSQL_ASSOC))
{
#print the results, no need to use more memory by adding them
#into seperate variable ;)
echo "Name: ".$sqlData["name"]." | Job: ".$this->FindJob($sqlData["job"]).
" | Level: ".$sqlData["level"]." | Gender: ".$sqlData["gender"].
" | Penya: ".$sqlData["money"]."<br />\n";
}
#free the query up from memory =]
$query->free();
}
}
}
?>
<?
#lets do the magic
$ranking=new Ranking();
$ranking->DisplayStats();
?>
I'm new here, hope this helps,
Shaun
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
Shauny_B
Hello guys,
Even though this problem is solved, please take a look at the OOPHP5 way of doing this solution, I'd recommend coding games in OOP, I'm currently developing a game also, check my introduction topic to see more of it.
PHP Code:
<?php
class Ranking
{
#instance variables here =]
protected $connection;
protected static $jobs = array(1 => "Mercenary", 2 => "Acrobat", 3 => "Assist", 4 => "Magician",
6 => "Knight", 7 => "Blade", 8 => "Jester", 9 => "Ranger",
10 => "RingMaster", 11 => "BillPoster", 12 => "Psykeeper",
13 => "Elementor");
public function __construct()
{
#lets create the connection stream =]
$this->connection=new MySQLi("localhost", "username", "password", "database");
}
public function __destruct()
{
#close the connection as we're no longer using it ;)
$this->connection->close();
}
protected function FindJob($jobId)
{
#has the value been pre-stored in the array? o_O
if(array_key_exists((int)$jobId, (array)self::$jobs))
{
return (string)self::$jobs[$jobId];
}
}
public function DisplayStats()
{
#lets assign the query to a variable
$sql="SELECT * FROM characters ORDER BY level DESC";
$query=$this->connection->query($sql);
#has the query got any rows? o_O
if($query->num_rows > 0)
{
#loop out teh results
while($sqlData=$query->fetch_array(MYSQL_ASSOC))
{
#print the results, no need to use more memory by adding them
#into seperate variable ;)
echo "Name: ".$sqlData["name"]." | Job: ".$this->FindJob($sqlData["job"]).
" | Level: ".$sqlData["level"]." | Gender: ".$sqlData["gender"].
" | Penya: ".$sqlData["money"]."<br />\n";
}
#free the query up from memory =]
$query->free();
}
}
}
?>
<?
#lets do the magic
$ranking=new Ranking();
$ranking->DisplayStats();
?>
I'm new here, hope this helps,
Shaun
Well I dont know what is OOPHP5. I'll prob read more about it later.
So for now, I would need some help again with this.
Code:
<?php
//Script made by Aitsihia.
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
function job($job) {
switch ($job) {
case 1:
$jb = 'Mercenary';
break;
case 2:
$jb = 'Acrobat';
break;
case 3:
$jb = 'Assist';
break;
case 4:
$jb = 'Magician';
break;
case 6:
$jb = 'Knight';
break;
case 7:
$jb = 'Blade';
break;
case 8:
$jb = 'Jester';
break;
case 9:
$jb = 'Ranger';
break;
case 10:
$jb = 'RingMaster';
break;
case 11:
$jb = 'BillPoster';
break;
case 12:
$jb = 'Psykeeper';
break;
case 13:
$jb = 'Elementor';
break;
return($jb);
}
}
$query = mysql_query("SELECT * FROM characters ORDER BY level DESC");
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
$job = job($row['job']);
$level = $row['level'];
$gender = $row['gender'];
$money = $row['money'];
echo "$job | $name | $level";
}
?>
This is how the script looks right now, but I dont really like it how it lookss.
Whatever I try to do, I do not get it to show it in order by Name, Job, Level.
I did try to change the ending to echo "$name | $job | $level"; but it just wont work, it still shows them order by job first.
Also, when theres alot of characters, it looks really messy, so how would it be possible to get it into a table?
|--------------------------------------|
| Name | Job | Level | Penya|
|--------------------------------------|
| Char | Job | Level | Penya |
And so on, hundreds of players on it. It would make it easier to read.
Re: [PHP][Help] Ranking Page
Hello,
This is a quick put together from me, untested but should work, if not you can see how to output results from the datasource into a table. Hope I've been of some help to you with the following solution:
PHP Code:
<?php
//Script made by Aitsihia.
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
function job($job) {
switch ($job) {
case 1:
$jb = 'Mercenary';
break;
case 2:
$jb = 'Acrobat';
break;
case 3:
$jb = 'Assist';
break;
case 4:
$jb = 'Magician';
break;
case 6:
$jb = 'Knight';
break;
case 7:
$jb = 'Blade';
break;
case 8:
$jb = 'Jester';
break;
case 9:
$jb = 'Ranger';
break;
case 10:
$jb = 'RingMaster';
break;
case 11:
$jb = 'BillPoster';
break;
case 12:
$jb = 'Psykeeper';
break;
case 13:
$jb = 'Elementor';
break;
return($jb);
}
}
# make sure that if you do want to alter them the way they're ordered use the ASC or DESC
$query = mysql_query("SELECT `id`, `name`, `job`, `level`, `gender`, `money` FROM `characters` ORDER BY `name`, `job`,`level` ASC");
if(mysql_num_rows($query) > 0) {
# here we start the table...
echo "<table>\n";
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
$job = job($row['job']);
$level = $row['level'];
$gender = $row['gender'];
$money = $row['money'];
# we print results out in the table tags...
echo "<tr>\n";
echo "\t\t<td>$job</td><td>$name</td><td>$level</td>\n";
echo "</tr>\n";
}
# here we end the table
echo "</table>";
}
?>
Also take a look into styling languages such as css for the table.
for example:
Code:
table
{
vertical-align:top;
text-align:left;
border:0;
}
Out of interest may I see what game you're developing? I maybe interested in helping out. I take a huge interest in browser based mmorpg's :laugh:
Thanks,
Shaun
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
Shauny_B
Hello,
This is a quick put together from me, untested but should work, if not you can see how to output results from the datasource into a table. Hope I've been of some help to you with the following solution:
PHP Code:
<?php
//Script made by Aitsihia.
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
function job($job) {
switch ($job) {
case 1:
$jb = 'Mercenary';
break;
case 2:
$jb = 'Acrobat';
break;
case 3:
$jb = 'Assist';
break;
case 4:
$jb = 'Magician';
break;
case 6:
$jb = 'Knight';
break;
case 7:
$jb = 'Blade';
break;
case 8:
$jb = 'Jester';
break;
case 9:
$jb = 'Ranger';
break;
case 10:
$jb = 'RingMaster';
break;
case 11:
$jb = 'BillPoster';
break;
case 12:
$jb = 'Psykeeper';
break;
case 13:
$jb = 'Elementor';
break;
return($jb);
}
}
# make sure that if you do want to alter them the way they're ordered use the ASC or DESC
$query = mysql_query("SELECT `id`, `name`, `job`, `level`, `gender`, `money` FROM `characters` ORDER BY `name`, `job`,`level` ASC");
if(mysql_num_rows($query) > 0) {
# here we start the table...
echo "<table>\n";
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
$job = job($row['job']);
$level = $row['level'];
$gender = $row['gender'];
$money = $row['money'];
# we print results out in the table tags...
echo "<tr>\n";
echo "\t\t<td>$job</td><td>$name</td><td>$level</td>\n";
echo "</tr>\n";
}
# here we end the table
echo "</table>";
}
?>
Also take a look into styling languages such as css for the table.
for example:
Code:
table
{
vertical-align:top;
text-align:left;
border:0;
}
Out of interest may I see what game you're developing? I maybe interested in helping out. I take a huge interest in browser based mmorpg's :laugh:
Thanks,
Shaun
This seems like not to work correcly. Problems I have with this script is that I wont see the Job names, and neither I can see the Table, which is not that big problem since it seems to be a bit more clear than the old one.
It would also be nice if the table could be visible.
Re: [PHP][Help] Ranking Page
Why not try learn a bit php, it's not very dificult.
And in ranking where is the jobid?
Re: [PHP][Help] Ranking Page
There's some ugly code up there.. Functions where there's no need for them.. OOP for a single step, one-time-use system.. Yuck! You people need to learn procedural before moving on to more advanced things. It's usually the best option, but since nobody here actually went through the process properly, or it seems like it, there's all these weird redundant methods popping in and out.
There's a simple answer to this: Put the name of the job in the database instead of the useless number. Might as well do a find & replace query for your database once, so you don't have to do all this redundant coding thereafter.
PHP Code:
$jobs = new array('','Mercenary', 'Acrobat', 'Assist', 'Magician',
'Knight', 'Blade', 'Jester', 'Ranger',
'RingMaster', 'BillPoster', 'Psykeeper', 'Elementor');
$err = (string) null; $msg = (string) null;
for($i=1;$i<count($jobs);$i++)
{
$q = mysql_query('UPDATE `characters` SET `job` = "'.$jobs[$i].'" WHERE `job` = "'.$i.'"') or $err.='<p>'.mysql_error().'</p>';
$msg .= 'Changed all jobs from <strong>`'.$i.'` to `'.$jobs[$i].'`</strong> (Affected `'.mysql_num_rows($q).'` rows.)<br />';
}
echo $err.$msg;
You'll see a few messages when you run this query. They should all be good, but if you get any errors, tell me and I'll see what I can do. The success messages should tell you what the query does, so live by that as far as these queries go.
Run that code one time, then get rid of it. Change any instance of a number in the job field. That will leave less gaps to check in security as well.
Be sure to change the code that gives the user their job, it needs to be the name rather then a number from now-on. It makes sense if you think about it. That should make things much easier for the long-run :thumbup1:
Hope that helps ;)
Oh and from now on to get the job from the db, you can grab it along-side all the other data; like this:
PHP Code:
$query = mysql_query('SELECT `id`, `name`, `job`, `level`, `gender`, `money` FROM `characters`');
while($row = mysql_fetch_assoc($query))
{
echo 'Name: '.$row['name'].' | Job: '.$row['job'].' | Level: '.$row['level'].' | Gender: '.$row['gender'].' | Penya: '.$row['money'].'<br />';
}
Well you can sort and display the information however you like, I'm just showing that you can just grab $row['job'] from the db without any special redundant functions every-time. It makes all the difference, trust me.
Good luck ;)
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
s-p-n
There's some ugly code up there.. Functions where there's no need for them.. OOP for a single step, one-time-use system.. Yuck! You people need to learn procedural before moving on to more advanced things. It's usually the best option, but since nobody here actually went through the process properly, or it seems like it, there's all these weird redundant methods popping in and out.
There's a simple answer to this: Put the name of the job in the database instead of the useless number. Might as well do a find & replace query for your database once, so you don't have to do all this redundant coding thereafter.
PHP Code:
$jobs = new array('','Mercenary', 'Acrobat', 'Assist', 'Magician',
'Knight', 'Blade', 'Jester', 'Ranger',
'RingMaster', 'BillPoster', 'Psykeeper', 'Elementor');
$err = (string) null; $msg = (string) null;
for($i=1;$i<count($jobs);$i++)
{
$q = mysql_query('UPDATE `characters` SET `job` = "'.$jobs[$i].'" WHERE `job` = "'.$i.'"') or $err.='<p>'.mysql_error().'</p>';
$msg .= 'Changed all jobs from <strong>`'.$i.'` to `'.$jobs[$i].'`</strong> (Affected `'.mysql_num_rows($q).'` rows.)<br />';
}
echo $err.$msg;
You'll see a few messages when you run this query. They should all be good, but if you get any errors, tell me and I'll see what I can do. The success messages should tell you what the query does, so live by that as far as these queries go.
Run that code one time, then get rid of it. Change any instance of a number in the job field. That will leave less gaps to check in security as well.
Be sure to change the code that gives the user their job, it needs to be the name rather then a number from now-on. It makes sense if you think about it. That should make things much easier for the long-run :thumbup1:
Hope that helps ;)
Oh and from now on to get the job from the db, you can grab it along-side all the other data; like this:
PHP Code:
$query = mysql_query('SELECT `id`, `name`, `job`, `level`, `gender`, `money` FROM `characters`');
while($row = mysql_fetch_assoc($query))
{
echo 'Name: '.$row['name'].' | Job: '.$row['job'].' | Level: '.$row['level'].' | Gender: '.$row['gender'].' | Penya: '.$row['money'].'<br />';
}
Well you can sort and display the information however you like, I'm just showing that you can just grab $row['job'] from the db without any special redundant functions every-time. It makes all the difference, trust me.
Good luck ;)
Well I think if I would do that, the server wouldnt open anymore, since its not only about the numbers. Theres C++ files, and I belive that inside them are those numbers for each job.
I'm not sure though, but thats what I believe. I anyways try to use this script and see what it does.
EDIT: Okay, so I first tried to use this, I got this.
( $jobs = new array('','Mercenary', 'Acrobat', 'Assist', 'Magician', 'Knight', 'Blade', 'Jester', 'Ranger', 'RingMaster', 'BillPoster', 'Psykeeper', 'Elementor'); $err = (string) null; $msg = (string) null; for($i=1;$i
'; $msg .= 'Changed all jobs from `'.$i.'` to `'.$jobs[$i].'` (Affected `'.mysql_num_rows($q).'` rows.)
'; } echo $err.$msg; )
Okay, then I added the <?php and ?> tags, and it gave me this.
Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in C:\www\CMS\change.php on line 3.
Re: [PHP][Help] Ranking Page
Think this should work
PHP Code:
<?php
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
$extract = mysql_query("SELECT * FROM characters ORDER BY level ASC");
$numrows = mysql_num_rows($extract);
$job = array(
'1' => 'Mercenary',
'2' => 'Acrobat',
'3' => 'Assist',
);
while ($row = mysql_fetch_assoc($extract))
{
echo "Name: ".$row['name']." | Job: ".$job[$row['job']]." | Level: ".$row['level']." | Gender: ".$row['gender']." | Penya: ".$row['money']." <br>";
}
?>
Re: [PHP][Help] Ranking Page
This thread is stupid. It's like
"how I print out string?"
"here's a way"
"still not work for me"
"here's another way"
"nope, I can't get this letter 't' to go away for some reason"
"here, this should work"
"yeah i still can't get it to work"
This isn't the "We'll code your shit for you" board, go learn PHP and gtfo until you actually try to solve the problem yourself. This is one of the simplest things you'll ever do with PHP, if you can't do it, you're going to run into a LOT more trouble trying to do other things. So do yourself and all of us a favor and go read: http://www.php.net/manual/en/langref.php.
This is where a mod should lock this thread.
Re: [PHP][Help] Ranking Page
PHP Code:
<?php
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
$extract = mysql_query("SELECT * FROM characters ORDER BY level ASC");
$numrows = mysql_num_rows($extract);
$job = array(
'1' => 'Mercenary',
'2' => 'Acrobat',
'3' => 'Assist',
);
while ($row = mysql_fetch_array($extract))
{
echo "Name: ".$row['name']." | Job: ".$job[$row['job']]." | Level: ".$row['level']." | Gender: ".$row['gender']." | Penya: ".$row['money']." <br>";
}
?>
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
Aitsihia
Well I think if I would do that, the server wouldnt open anymore, since its not only about the numbers. Theres C++ files, and I belive that inside them are those numbers for each job.
I'm not sure though, but thats what I believe. I anyways try to use this script and see what it does.
EDIT: Okay, so I first tried to use this, I got this.
( $jobs = new array('','Mercenary', 'Acrobat', 'Assist', 'Magician', 'Knight', 'Blade', 'Jester', 'Ranger', 'RingMaster', 'BillPoster', 'Psykeeper', 'Elementor'); $err = (string) null; $msg = (string) null; for($i=1;$i
'; $msg .= 'Changed all jobs from `'.$i.'` to `'.$jobs[$i].'` (Affected `'.mysql_num_rows($q).'` rows.)
'; } echo $err.$msg; )
Okay, then I added the <?php and ?> tags, and it gave me this.
Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in C:\www\CMS\change.php on line 3.
You should know it needs php tags!
Oh sorry about the parse error, I've been doing too much JavaScript lately.
Since you think the job numbers are needed in the server, I would not change them. Whoever developed the server is a moron or something. I wouldn't dare tell you to change the server files..
If you do want to test it to see what it'll do, then use this.
PHP Code:
$jobs = array('','Mercenary', 'Acrobat', 'Assist', 'Magician', 'Knight', 'Blade', 'Jester', 'Ranger', 'RingMaster', 'BillPoster', 'Psykeeper', 'Elementor');
$err = (string) null;
$msg = (string) null;
If you need to revert back after you try it, replace the $jobs array with this:
PHP Code:
$jobs = array(''=>0,'Mercenary'=>1, 'Acrobat'=>2, 'Assist'=>3, 'Magician'=>4, 'Knight'=>5, 'Blade'=>6, 'Jester'=>7, 'Ranger'=>8, 'RingMaster'=>9, 'BillPoster'=>10, 'Psykeeper'=>11, 'Elementor'=>12);
Then run the query again.. There is a risk it may corrupt your database, use caution and don't optimize the key parts of the script. ;)
Edit:
Quote:
Originally Posted by
ProKiller002
PHP Code:
<?php
mysql_connect("-","-","-") or die(mysql_error());
mysql_select_db("-") or die(mysql_error());
$extract = mysql_query("SELECT * FROM characters ORDER BY level ASC");
$numrows = mysql_num_rows($extract);
$job = array(
'1' => 'Mercenary',
'2' => 'Acrobat',
'3' => 'Assist',
);
while ($row = mysql_fetch_array($extract))
{
echo "Name: ".$row['name']." | Job: ".$job[$row['job']]." | Level: ".$row['level']." | Gender: ".$row['gender']." | Penya: ".$row['money']." <br>";
}
?>
Hey, you just copied HackHeaven's functionality exactly, but changed fetch_assoc to fetch_array. You do know that the insignificant, little bit you changed only made it worse, right? mysql_fetch_array(), the way you implimented it loads a numeric array as well as an associative array. Not that the performance is reduced, it's just the same exact thing- but with an extra, unneeded and unused functionality :huh:
Quote:
Originally Posted by php.net
The best you could do with that one line would be putting equal value. If you decided to load as rows, you'd be doing more work, and would save an insignificant amount on the SQL end, but would most-likely lose on the extra work you did via PHP.
My point is, post things more useful. If the answer is there already, don't post the same thing right after.
Hold on, I think I know what the problem is.
- This goes out to 80% of you by the way..
If you don't know what a function does, before you try to post the function you like, research the function you don't know about ;)
Don't assume what you know is superior to someone else, unless you've researched it and understand what the other person is doing.
To research,
1.) Goto http://www.google.com and type the function in the box by the search button.
- - - Note that you may also have to include the language, ex: [ mysql_fetch_assoc php ] (Search)
2.) Click the Search Button.
3.) Find the listing that best applies to you; [Look for php.net]
Lol sorry, couldn't help myself :laugh:
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
jMerliN
This thread is stupid. It's like
"how I print out string?"
"here's a way"
"still not work for me"
"here's another way"
"nope, I can't get this letter 't' to go away for some reason"
"here, this should work"
"yeah i still can't get it to work"
This isn't the "We'll code your shit for you" board, go learn PHP and gtfo until you actually try to solve the problem yourself. This is one of the simplest things you'll ever do with PHP, if you can't do it, you're going to run into a LOT more trouble trying to do other things. So do yourself and all of us a favor and go read:
http://www.php.net/manual/en/langref.php.
This is where a mod should lock this thread.
Aggred:thumbup1:
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
s-p-n
To
research,
1.) Goto
http://www.google.com and type the function in the box by the search button.
- - - Note that you may also have to include the language, ex: [ mysql_fetch_assoc php ] (Search)
2.) Click the Search Button.
3.) Find the listing that best applies to you; [Look for php.net]
Lol sorry, couldn't help myself :laugh:
Easier:
http://php.net/manual-lookup.php
DONE.
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
jMerliN
This thread is stupid. It's like
"how I print out string?"
"here's a way"
"still not work for me"
"here's another way"
"nope, I can't get this letter 't' to go away for some reason"
"here, this should work"
"yeah i still can't get it to work"
This isn't the "We'll code your shit for you" board, go learn PHP and gtfo until you actually try to solve the problem yourself. This is one of the simplest things you'll ever do with PHP, if you can't do it, you're going to run into a LOT more trouble trying to do other things. So do yourself and all of us a favor and go read:
http://www.php.net/manual/en/langref.php.
This is where a mod should lock this thread.
As I understand the meaning of this section, is to ask for help. I did ask help for my script, and I get it working somehow. And what can I do for that I am such a noob, who just can't get this script to work.
But now I feel like I don't want to ask help in a single thing from here RageZone anymore. All I get is flaming, since I don't know enough PHP to get my own scripts to work.
I think you should add a new sticky thread "Asking for a help is not allowed".
I'll go to find better forums to ask help on.
Congratulations, you got rid of me...
Re: [PHP][Help] Ranking Page
Quote:
Originally Posted by
jMerliN
Eh, mine is way more multi-compatible, and sometimes gives more intuitive results.
Quote:
Originally Posted by
Aitsihia
As I understand the meaning of this section, is to ask for help. I did ask help for my script, and I get it working somehow. And what can I do for that I am such a noob, who just can't get this script to work.
But now I feel like I don't want to ask help in a single thing from here RageZone anymore. All I get is flaming, since I don't know enough PHP to get my own scripts to work.
I think you should add a new sticky thread "Asking for a help is not allowed".
I'll go to find better forums to ask help on.
Congratulations, you got rid of me...
We weren't trying to get rid of you.. Well I wasn't.. I think some people trying to help you aren't helping anything.. Also, I think you should get better at PHP.
If you choose to take it as an insult, that's fine. Don't leave because of us though. Again, we're only trying to help improve your skills- with constructive criticism. People should pay for this stuff! :thumbup1: