[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:
$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:
$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 ;)
 
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:
$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:
$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.
 
Last edited:
Think this should work
PHP:
<?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>";
}
?>
 
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 **** 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.
 
PHP:
<?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>"; 
} 
?>
 
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:
$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:
$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:
PHP:
<?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:

php.net said:
mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both.
mysql_fetch_assoc() - Fetch a result row as an associative array.
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:
 
Last edited:
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 **** 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:
 
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 **** 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...
 
Last edited:
Eh, mine is way more multi-compatible, and sometimes gives more intuitive results.

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:
 
Eh, mine is way more multi-compatible, and sometimes gives more intuitive results.



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:

I'm not leaving because of you. I got helpful anwers from you, which hepled me. Suddendly just this guy called jMerliN came here and started flaming on me. I may not be the best PHP coder, but what can I do? Learn some PHP, which I am trying to do.

Anyway, I think that PHP is not my thing, I'll probably leave it for a while, and start doing things with HTML which I know more than PHP.

So thanks for people who helped me on this. I got helpful answers from you, and this script started getting better and better, as it works now as I wanted it to work.
 
Make a Array >_>"

PHP:
<?
echo "<table width=\"100%\">";
echo "<tr>";
echo "<td>Rank</td>";
echo "<td>Name</td>";
echo "<td>Job</td>";
echo "</tr>";
$jobs = array('200' => 'Jobhere', '210' => 'OtherJob');
$count = 1;
$sql = mysql_query("SELECT * FROM `character` ORDER BY EXP DESC LIMIT 25");
while($row = mysql_fetch_array($sql)){
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$jobs[$row['Job']]."</td>";
echo "</tr>";
$count++;
}
echo "</table>";
?>

Just fast writing and from scratch.
idk the jobs, tables, etc..
Hope its clear

Will output like this ;

Rank Name Job
1 Sayuta JobHere
2 Counterweight OtherJob
 
Just fast writing and from scratch.
idk the jobs, tables, etc..
You gave the same answer as 10 other people..
You and everyone else.. Learn PHP & SQL.. Better yet, learn good programming practices :rolleyes:

It's not easy. Learn the difference between quotes & apostrophes, when to use which, and why. Learn to indent properly, or in a fashion in which others can read through. Limit the amount of functions when possible; If you have repeating echo statements, compact it into a single one, and white-space is your friend. Also, try to anticipate the formatting of your HTML. You want to be able to read it both in your PHP code, and in the source code of the browser.

Start by reading this article.
:thumbup1:
 
Back