Jump to content
MakeWebGames

Global func name


peterisgb

Recommended Posts

Hi, i'm trying to make names in my game universal.

Eg have a code like this

 

if($r['display_pic']) {
$display = "<img src='{$r['display_pic']}' width='17' height='17' />"; } 
else { $display = "<img src='images/nopic.png' width='15' height='15' />"; }
if($r['user_level'] == 1) { $rankcolour = "#ffffff"; }
if($r['user_level'] == 2) { $rankcolour = "#06519b"; }
if($r['user_level'] >= 3) { $rankcolour = "#49b536"; }
if($ir['donatordays']) { 
$d = "<img src='donator.gif' alt='Donator: {$r['donatordays']} Days Left' />"; 
$rankcolour = "red"; }
$user = $r['username'];
$usernamei = "<font color='$rankcolour'>$display $user $d</font>";

 

in the global_func file.

Then put anywhere on the site $usernamei like useronline/userlist,

This don't work, but how or what do i need to do to get this to work.

Thanks in Advance

Pete

Link to comment
Share on other sites

  • <font> is deprecated
  • Why put extra workload on yourself? Why not rename the $ir['username'] value?

 

   if ($r['display_pic']) {
       $display = "<img src='{$r['display_pic']}' width='17' height='17' />";
   } else {
       $display = "<img src='images/nopic.png' width='15' height='15' />";
   }
   if ($r['user_level'] == 1) {
       $rankcolour = "#ffffff";
   }
   if ($r['user_level'] == 2) {
       $rankcolour = "#06519b";
   }
   if ($r['user_level'] >= 3) {
       $rankcolour = "#49b536";
   }
   if ($ir['donatordays']) {
       $d = "<img src='donator.gif' alt='Donator: {$r['donatordays']} Days Left' />";
       $rankcolour = "red";
   }
   $user = $r['username'];
   $r['username'] = "<span style=\"color:$rankcolour;\">$display $user $d</span>";

 

You could even adapt this into an array. Also, please tidy your code.

I've noticed we're using $r and not $ir. McCodes, IIRC, uses $r when it's for a user other than the person viewing the page - so this may be why you're not getting the results you're after.

Link to comment
Share on other sites

well thats the thing, i want it to show other users, userlist/usersonline. so in my case $r is needed.

or it would be

peterisgb 1 1,000,000

peterisgb 1 1,000,000.......

which isnt what i'm looking for. it needs to show each user by itself. using the $ir already works. i want it to be used for all usernames.

Link to comment
Share on other sites

well thats the thing, i want it to show other users, userlist/usersonline. so in my case $r is needed.

or it would be

peterisgb 1 1,000,000

peterisgb 1 1,000,000.......

which isnt what i'm looking for. it needs to show each user by itself. using the $ir already works. i want it to be used for all usernames.

Why not add something like this into global_func

function doFormatUsername( &$r ) {

    //Indexed by user_level.
    $arrColours = array("1" => "FFFFFF",
                     "2" => "CC0000");

if(array_key_exists('user_level', $r) ) {
   $r['username'] = "<span style=\"color:#". $arrColours[$r['user_level']] ."\">". $r['username'] ."</span>"; 
}

}

 

Then call that function where ever $ir or $r is defined.

 

...
$r = $db->fetch_row();
doFormatUsername($r);
Link to comment
Share on other sites

Super simple way:

 

echo '<span class="player-'. $r['user_level'] .'">'. $r['username'] .'</span>';

 

More complex, but generally better way:

 

<?php
function format_username($user) {
   if(in_array($user['user_level'], array(2,3)) {
      // staff?
      $return = 'staff type-'. $user['user_level'];
   } else {
       // regular player
       $return = ($user['donator']) ? 'player donator' : 'player';
   }
   return '<span class="'. $return .'">'. $user['username'] .'</span>';
}

// to use it..
echo format_username($ir);

 

It would make it easier to format staff and regular player usernames slightly different using this method. You would just need to setup the CSS classes after adding this

Link to comment
Share on other sites

Super simple way:

 

echo '<span class="player-'. $r['user_level'] .'">'. $r['username'] .'</span>';

 

More complex, but generally better way:

 

<?php
function format_username($user) {
   if(in_array($user['user_level'], array(2,3)) {
      // staff?
      $return = 'staff type-'. $user['user_level'];
   } else {
       // regular player
       $return = ($user['donator']) ? 'player donator' : 'player';
   }
   return '<span class="'. $return .'">'. $user['username'] .'</span>';
}

// to use it..
echo format_username($ir);

 

It would make it easier to format staff and regular player usernames slightly different using this method. You would just need to setup the CSS classes after adding this

 

  • Your first option will require you to find all occurrences of the username output, and replace it.
    • Why spend time doing that, when you can write a function and call it once (see below point)

    [*]Your second solution is a little like the first

    • Repeated calls on that one function
      • Tip: Try passing it in as a reference, so you only need to call the function once.

       

     

 

Also, you've said "More complex, but generally better way" but didn't explain in full why to support your claim. If you think you did by the last sentence, you didn't. It would still require CSS (like the first solution) - especially in the example given.

Edited by sniko
Link to comment
Share on other sites

  • Your first option will require you to find all occurrences of the username output, and replace it.
    • Why spend time doing that, when you can write a function and call it once (see below point)

    [*]Your second solution is a little like the first

    • Repeated calls on that one function
      • Tip: Try passing it in as a reference, so you only need to call the function once.

       

     

 

Also, you've said "More complex, but generally better way" but didn't explain in full why to support your claim. If you think you did by the last sentence, you didn't. It would still require CSS (like the first solution) - especially in the example given.

Find & replace isn't hard to do.

Parsing the data by serial is a bit of a hack in this case, more of an afterthought and if you wanted to use the raw username field it's then not available. Sure you could use your example and store something like $r['formatted_username'] but then your first argument is then invalid.

Anyway, if you have the time to go through every file and add doFormatUsername($r); then are you too lazy to just change the name output to incorporate a format function.

Your argument saying that it would still need style is irrelevant because you clearly don't understand the purpose of HTML. Tip: it's not for styling data.

Link to comment
Share on other sites

Find & replace isn't hard to do.

Parsing the data by serial is a bit of a hack in this case, more of an afterthought and if you wanted to use the raw username field it's then not available. Sure you could use your example and store something like $r['formatted_username'] but then your first argument is then invalid.

Anyway, if you have the time to go through every file and add doFormatUsername($r); then are you too lazy to just change the name output to incorporate a format function.

Your argument saying that it would still need style is irrelevant because you clearly don't understand the purpose of HTML. Tip: it's not for styling data.

I'm unsure what you mean by "if you wanted to use the raw username field it's then not available." - Do you mean it'll have extra characters in it, and may frick some stuff up? If so, I would be very disappointed if anything used the username as a key/index.

Going through each file is something that's useless to do, if you can change the username is one place, and call the function once - assuming that a username isn't a key/index (as it should be numeric and something that doesn't change, ie: userid)

You said you would need to style it, so that was my counter argument.

In essence, I was offering a solution that only needed a function to be called once, and writes to an array once (which is perfectly fine to do (manipulation of data) assuming this data isn't used as a key/index - which it shouldn't be) instead of going through 200+ files to change all occurrences of outputting a username to call a function, which is going to be called multiple times.

Either;

  • Write once, call once
  • Call hundreds of times, modify loads of files

The former of those two seems most logical.

Link to comment
Share on other sites

I'm unsure what you mean by "if you wanted to use the raw username field it's then not available." - Do you mean it'll have extra characters in it, and may frick some stuff up? If so, I would be very disappointed if anything used the username as a key/index.

Going through each file is something that's useless to do, if you can change the username is one place, and call the function once - assuming that a username isn't a key/index (as it should be numeric and something that doesn't change, ie: userid)

You said you would need to style it, so that was my counter argument.

In essence, I was offering a solution that only needed a function to be called once, and writes to an array once (which is perfectly fine to do (manipulation of data) assuming this data isn't used as a key/index - which it shouldn't be) instead of going through 200+ files to change all occurrences of outputting a username to call a function, which is going to be called multiple times.

Either;

  • Write once, call once
  • Call hundreds of times, modify loads of files

The former of those two seems most logical.

I don't know about too many people but I have occurrences in my game where you need the username, for example; A user profile (/profile/wrux for example) uses the player's username as apposed to an id.

It makes perfect sense to write unobtrusive code. If you are doing hacks like this, what other things are you doing further down the line? Data should be stored as data and outputted in a certain manner.

Link to comment
Share on other sites

How about modifying the db wrapper fetch_row and fetch_single methods to look for the column `username` and format it there? This way you only edit one file and everywhere the column is used BAMMMM! Formatted.

 

im sure there is probably another way like extending it and using a getter or setter but this is pretty easy to do

Link to comment
Share on other sites

ok most of this you guys said has confuzzled me lol.

I use this in the header.php

if($ir['display_pic']) {
$display = "<img src='{$ir['display_pic']}' width='17' height='17' />"; } 
else { $display = "<img src='images/nopic.png' width='17' height='17' />"; }
if($ir['user_level'] == 1) { $rankcolour = "#ffffff"; }
if($ir['user_level'] == 2) { $rankcolour = "#06519b"; }
if($ir['user_level'] >= 3) { $rankcolour = "#49b536"; }
if($ir['donatordays']) { 
$d = "<img src='donator.gif' alt='Donator: {$ir['donatordays']} Days Left' />"; 
$rankcolour = "red"; }
$user = $ir['username'];
$usernamei = "<font color='$rankcolour'>$display $user $d</font>";

 

and $usernamei in the header file of course which this works well.

Edited by peterisgb
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...