Jump to content
MakeWebGames

Function help


Recommended Posts

Hey,

I have a function that converts my money into gold, silver and copper. (code below) I was wondering if there is a way to make it so if there is 0 copper and\or silver it'd make it not show it. (IE: 200gold 300 silver 000copper make it look like this, 200gold 300 silver) Thanks for the help in advance :)

function gold_format($money)

{

$counter=strlen($money);

if ($counter <= 3)

{

$moneydone="

$money<img src='images/Copper.png'/>";

return $moneydone;

}

if ($counter <= 6)

{

$copper= substr($money, -3);

$silver= substr($money, 0, -3);

$moneydone="

$silver<img src='images/Silver.png'/>

$copper<img src='images/Copper.png'/>";

return $moneydone;

}

else

{

$copper= substr($money, -3);

$mid=($counter-6);

$silver= substr($money, $mid, -3);

$gold=substr($money, 0, $mid);

$moneydone="

$gold<img src='images/Gold.png'/>

$silver<img src='images/Silver.png'/>

$copper<img src='images/Copper.png'/>";

return $moneydone;

}

}

Link to comment
Share on other sites

  • 1 month later...
I don't even understand the damn function!

Why do you have gold, silver and bronze all using a single field?

I'm confused as to what is happening here and why you are splitting it 3 times.

For simplicity, if nothing else, use 3 fields!?

He's assuming three digits in the string, i.e. 100gold, and thus only collecting the values of them, i.e. 100.

Flawed in some ways, but it does work if the integer value is above 100 and below 999.

@OP:

$int_value = preg_replace('[a-zA-z]', '', $string);
if (intval($int_value) == 0) {
   // zero, e.g. not valid.
} else 
  // valid
Link to comment
Share on other sites

Hey,

I have a function that converts my money into gold, silver and copper. (code below) I was wondering if there is a way to make it so if there is 0 copper and\or silver it'd make it not show it. (IE: 200gold 300 silver 000copper make it look like this, 200gold 300 silver) Thanks for the help in advance :)

function gold_format($money)

{

$counter=strlen($money);

if ($counter <= 3)

{

$moneydone="

$money<img src='images/Copper.png'/>";

return $moneydone;

}

if ($counter <= 6)

{

$copper= substr($money, -3);

$silver= substr($money, 0, -3);

$moneydone="

$silver<img src='images/Silver.png'/>

$copper<img src='images/Copper.png'/>";

return $moneydone;

}

else

{

$copper= substr($money, -3);

$mid=($counter-6);

$silver= substr($money, $mid, -3);

$gold=substr($money, 0, $mid);

$moneydone="

$gold<img src='images/Gold.png'/>

$silver<img src='images/Silver.png'/>

$copper<img src='images/Copper.png'/>";

return $moneydone;

}

}

I know what you mean. But i don't know how to code except il give you a psuedo code.

If copper = "000" then

copper = nothing

else if silver = "000" then

silver = nothing

else if gold = "000" then

gold = nothing

end if

xD ? any help there :P

Link to comment
Share on other sites

I know what you mean. But i don't know how to code except il give you a psuedo code.

If copper = "000" then

copper = nothing

else if silver = "000" then

silver = nothing

else if gold = "000" then

gold = nothing

end if

xD ? any help there :P

That would parse as valid VB6.0 with a little tweak. Just sayin'.

Link to comment
Share on other sites

function gold_format($money) { 
   $copper = '000';
   $silver = '000';
   $gold = '000';

   $counter = strlen($money);
   if ($counter <= 3) {
       $copper = $money;
   }
   if ($counter <= 6) {
       $copper = substr($money, -3);
       $silver = substr($money, 0, -3);
   }
   if ($counter <= 9) {
       $copper = substr($money, -3);
       $silver = substr($money, ($counter - 6), -3);
       $gold = substr($money, 0,($counter - 6));
   }

   return
   (($copper == '000') ? '' : $copper.' C<img src="" title="" />').
   (($silver == '000') ? '' : $silver.' S<img src="" title="" />').
   (($gold == '000') ? '' : $gold.' G<img src="" title="" />');

}

Still a little sketchy on why you are doing it like this (equally as sketchy as why you expect the param back to front compared to your code), however this should provide the result you're after.

Link to comment
Share on other sites

Not bad.

Here's my 5 minute attempt:

<?php
class metal_amounts
{
   function __construct($money)
   {
       $this->_money  = strrev($money);
       $this->_strlen = strlen($this->_money);
       $this->_copper = substr($this->_money, 0, 3);
       $this->_silver = (strlen(substr($this->_money, 3, 3)) > 0) ? substr($this->_money, 3, 3) : 0 ; 
       $this->_gold   = (strlen(substr($this->_money, 6))    > 0) ? substr($this->_money, 6)    : 0 ;
   }
   function display_copper()
   {
       return strrev($this->_copper);
   }
   function display_silver()
   {
       return strrev($this->_silver);
   }
   function display_gold()
   {
       return strrev($this->_gold);
   }
}

$money = 1234567890;
$metal = new metal_amounts($money);
echo '<p>Copper: ' . $metal->display_copper() . '</p>';
echo '<p>Silver: ' . $metal->display_silver() . '</p>';
echo '<p>Gold: '   . $metal->display_gold() . '</p>';

/*
OUTPUTS:

Copper: 890

Silver: 567

Gold: 1234
*/

Considering the output is used in reverse order -- seems logical to use strrev() to start, sort from there and then strrev() back out of it.

Smexy;

The even more logical, other than not doing it this way, would be to expect the input to be in the same order as the output. :|

Link to comment
Share on other sites

My game does not use $ for the currency. It uses gold, silver, and copper. 1000 copper = 1 silver 1000 silver = 1 gold

Here is my fixed function now.

function gold_format($money)

{

$counter=strlen($money);

if ($counter <= 3)

{

$moneydone="$money<img src='images/money/Copper.png'/>";

return $moneydone;

}

if ($counter <= 6)

{

$copper= substr($money, -3);

$silver= substr($money, 0, -3);

$copper=ltrim($copper, 0);

if ($copper != "")

{

$moneydone="$silver<img src='images/money/Silver.png'/>$copper<img src='images/money/Copper.png'/>";

}

else {

$moneydone="$silver<img src='images/money/Silver.png'/>";

}

return $moneydone;

}

else

{

$copper= substr($money, -3);

$mid=($counter-6);

$silver= substr($money, $mid, -3);

$gold=substr($money, 0, $mid);

$copper=ltrim($copper, 0);

$silver=ltrim($silver, 0);

if ($copper != "")

{

if ($silver != "")

{

$moneydone="$gold<img src='images/money/Gold.png'/>$silver<img src='images/money/Silver.png'/>$copper<img src='images/money/Copper.png'/>";

}

else {

$moneydone="$gold<img src='images/money/Gold.png'/>$copper<img src='images/money/Copper.png'/>";

}

}

else {

$moneydone="$gold<img src='images/money/Gold.png'/>";

}

return $moneydone;

}

}

Link to comment
Share on other sites

Why go through so much?

 

function valueFormat($gold = 0, $siver = 0, $copper = 0)	{
$return = '';
$return .= ($gold) ? number_format($gold).' G <img src="images/Gold.png" /> ' : '';
$return .= ($silver) ? number_format($silver).' S <img src="images/Silver.png" /> ' : '';
$return .= ($copper) ? number_format($copper).' C <img src="images/Copper.png" />' : '';
return $return;
}

 

How to use it?

valueFormat($gold, $silver, $copper)

Or change it to how u want it to output!

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.

 Share

×
×
  • Create New...