Job Class Script

This is guide mostly for website tools and code, for example CMS or registration scripts.
There can also be tools.

Moderators: osRose dev team, ospRose dev team, osiRose dev team, Moderators

Job Class Script

Postby Rifke on Thu Aug 09, 2007 3:21 pm

You might have seen this in an other CMS system but that used a row based script. Wich isn't dynamic when using in any other scrip. So I've rewritten the 'script', to an much more dynamic one.

  1.  
  2. <?php
  3. function JobClass($ClassID)
  4. {
  5.     if ($ClassID == 0)
  6.         return "Visitor";
  7.     if ($ClassID == 111)
  8.         return "Soldier";
  9.     if ($ClassID == 121)
  10.         return "Knight";
  11.     if ($ClassID == 122)
  12.         return "Champion";
  13.     if ($ClassID == 211)
  14.         return "Muse";
  15.     if ($ClassID == 221)
  16.         return "Mage";
  17.     if ($ClassID == 222)
  18.         return "Cleric";
  19.     if ($ClassID == 311)
  20.         return "Hawker";
  21.     if ($ClassID == 321)
  22.         return "Raider";
  23.     if ($ClassID == 322)
  24.         return "Scout";
  25.     if ($ClassID == 411)
  26.         return "Dealer";
  27.     if ($ClassID == 421)
  28.         return "Bourgeois";
  29.     if ($ClassID == 422)
  30.         return "Artisan";
  31. }
  32. ?>
  33.  


You use it as
  1.  
  2. // You retrieved the classID from the database.
  3. $ClassIDSQL = "SELECT classid FROM characters WHERE id='".$CharID."' ";
  4. $ClassIDQRY = mysql_query($ClassIDSQL);
  5. if ( $ClassIDQRY === false ) {
  6.     echo 'Sorry we could not retrieve your character, please try again later. If you keep having this problem contact an Administrator.';
  7. } else {
  8.     $AssocArray = mysql_fetch_assoc($ClassIDSQL);
  9.     echo 'Your character's job is: '.JobClass($AssocArray['classid']). ';
  10. }
  11.  
  12.  


All code provided is under the GNU General Public License
Last edited by Rifke on Mon Dec 03, 2007 9:36 pm, edited 2 times in total.
Rifke
Pero pero
Pero pero
 
Posts: 719
Joined: Thu Aug 09, 2007 3:01 pm
Location: Belgium

Re: Job Class Script

Postby 0x586e on Mon Dec 03, 2007 4:47 am

Hi. Mine :

  1.  
  2. function GetJob($AId)
  3. {
  4.     $JobArray = array('0'=>'Visitor','111'=>'Soldier','121'=>'Knight','122'=>'Champion','211'=>'Muse','221'=>'Mage','222'=>'Cleric','311'=>'Hawker','321'=>'Raider','322'=>'Scout','411'=>'Dealer','421'=>'Bourgeois','422'=>'Artisan');
  5.     return $JobArray[ $AId ];
  6. }
  7.  


Use :

  1.  
  2. $MyJob = 311;
  3. echo 'You are '.GetJob($MyJob);
  4. // You are Hawker
  5.  


:)
0x586e
Jelly Bean
Jelly Bean
 
Posts: 13
Joined: Mon Dec 03, 2007 4:43 am

Re: Job Class Script

Postby Jckf on Mon Dec 03, 2007 1:09 pm

0x586e wrote:Hi. Mine :

  1.  
  2. function GetJob($AId)
  3. {
  4.     $JobArray = array('0'=>'Visitor','111'=>'Soldier','121'=>'Knight','122'=>'Champion','211'=>'Muse','221'=>'Mage','222'=>'Cleric','311'=>'Hawker','321'=>'Raider','322'=>'Scout','411'=>'Dealer','421'=>'Bourgeois','422'=>'Artisan');
  5.     return $JobArray[ $AId ];
  6. }
  7.  


Use :

  1.  
  2. $MyJob = 311;
  3. echo 'You are '.GetJob($MyJob);
  4. // You are Hawker
  5.  


:)

You don't really need the function here. The array would be enough.
  1.  
  2. // Define array, then use it like
  3. echo 'This character is a ' . $classes[$classid];
  4.  
User avatar
Jckf
Antares
Antares
 
Posts: 338
Joined: Wed Nov 28, 2007 1:09 am
Location: Norway

Re: Job Class Script

Postby lmame on Mon Dec 03, 2007 1:42 pm

It depends :)

On my scripts I put it into a function (or class) just because I use it in several different PHP scripts, so it's just a "include "functions.php" and a call to the function.
Easier than copy / paste same code in half dozen scripts :)
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Job Class Script

Postby Jckf on Mon Dec 03, 2007 2:10 pm

lmame wrote:It depends :)

On my scripts I put it into a function (or class) just because I use it in several different PHP scripts, so it's just a "include "functions.php" and a call to the function.
Easier than copy / paste same code in half dozen scripts :)

Yes. But you could have that array defined with your function (in the same include).
User avatar
Jckf
Antares
Antares
 
Posts: 338
Joined: Wed Nov 28, 2007 1:09 am
Location: Norway

Re: Job Class Script

Postby lmame on Mon Dec 03, 2007 2:34 pm

Yes of course, but in fact usually in those kind of "array functions" (NPC dialogs, jobs, whatever) I always test if the key exists (never rely on user data) and in some occasions I log errors so a function is always used :)
Moreover I use often the same names for temporary variables, with functions I don't have to bother if I already used it or not and the software I use automatically list the functions as native php ones so it's easier for me to work that way.
The world is full of love and peace ^_^
Image
User avatar
lmame
Admin
Admin
 
Posts: 8997
Joined: Mon Aug 06, 2007 4:42 pm
Location: July City

Re: Job Class Script

Postby 0x586e on Mon Dec 03, 2007 6:22 pm

It was in a function because it was a part of a global CMS class, that's why i had it in function and just removed the keyword "public" at the beggining.

And having a function instead of using a global array is way more comprehensive and handy.

Like, i can use the function by calling it in the same class $this->GetJob($JobID), it looks better for me.
0x586e
Jelly Bean
Jelly Bean
 
Posts: 13
Joined: Mon Dec 03, 2007 4:43 am

Re: Job Class Script

Postby Jckf on Mon Dec 03, 2007 6:52 pm

0x586e wrote:It was in a function because it was a part of a global CMS class, that's why i had it in function and just removed the keyword "public" at the beggining.

And having a function instead of using a global array is way more comprehensive and handy.

Like, i can use the function by calling it in the same class $this->GetJob($JobID), it looks better for me.

Thats a good reason =)
User avatar
Jckf
Antares
Antares
 
Posts: 338
Joined: Wed Nov 28, 2007 1:09 am
Location: Norway

Re: Job Class Script

Postby 0x586e on Mon Dec 03, 2007 7:02 pm

Thanks Jckf

I personally mainly use Oriented Object script, in every languages that I use.
You'll see it, i'm gonna post a Status Checker written in php just now.

:)
0x586e
Jelly Bean
Jelly Bean
 
Posts: 13
Joined: Mon Dec 03, 2007 4:43 am

Re: Job Class Script

Postby Rifke on Mon Dec 03, 2007 7:25 pm

OOP or a function is beter, so the function will always be cleaner than just the variable.
Rifke
Pero pero
Pero pero
 
Posts: 719
Joined: Thu Aug 09, 2007 3:01 pm
Location: Belgium


Return to PHP / Web Guides, Scripts and tools.

Who is online

Users browsing this forum: No registered users and 6 guests

cron