0

Hello peeps!
I am wondering, how can i make this to not require manually writing.

if($attackerSoldiers >10){$ASoldierPercent = "1";}
	if($attackerSoldiers >20){$ASoldierPercent = "2";}
	if($attackerSoldiers >30){$ASoldierPercent = "3";}
	if($attackerSoldiers >40){$ASoldierPercent = "4";}
	if($attackerSoldiers >50){$ASoldierPercent = "5";}
	if($attackerSoldiers >60){$ASoldierPercent = "6";}
	if($attackerSoldiers >70){$ASoldierPercent = "7";}
	if($attackerSoldiers >80){$ASoldierPercent = "8";}
	if($attackerSoldiers >90){$ASoldierPercent = "9";}

For every 10th $attackerSoldiers i want $ASoldierPercent to go up one.
Anyone see a solution?
/cheers

6
Contributors
11
Replies
12
Views
6 Years
Discussion Span
Last Post by Ezzaral
Featured Replies
  • What you have now is: [icode]10 => 0 11 => 1[/icode] I suspect you want: [icode]9 => 0 10 => 1[/icode] Which I'd do like: [code]$ASoliderPercent = floor($attackerSoldiers/10);[/code] Read More

1

What you have now is:

10 => 0
11 => 1

I suspect you want:

9 => 0
10 => 1

Which I'd do like:

$ASoliderPercent = floor($attackerSoldiers/10);
0

Im sorry didnt understand that fully..
What does floor($attackerSoldiers/10); do ? i tried reading some on php.net but dont quite understand the function yet.
How would you put that up in the list i got?

0

The description of floor() is pretty straightforward:

Returns the next lowest integer value by rounding down value if necessary.

It basically gives you integer portion of your number without any of the decimal portion.
So think about that in the context of dividing your number by 10 and how the result compares to the values that you have hard-coded in that block of if statements.

0

Im sorry for even asking about this, Ezzaral, because i couldnt get a single % of what you just said.
I believe when you see a noob-question you shouldn't answer like i was a pro..

0

Are you kidding? I can't possibly explain what floor() does in any clearer terms.

If you want to scold people that try to explain things for you, just forget it. RTFM. I won't trouble you with any further attempts to help.

0

@Sorcher, is English your native language? If not, you're forgiven :)

What floor does, is round any number, but downwards. So:

echo floor(2);     // 2
echo floor(2.5);   // 2
echo floor(2.999); // 2

$x =  9; echo floor($x / 10); // 0
$x = 10; echo floor($x / 10); // 1
$x = 19; echo floor($x / 10); // 1
$x = 20; echo floor($x / 10); // 2

Edited by twiss: n/a

0
<?
for($a=1;$a<9;$a++){
$x = $a*10;
if($attackerSoldiers >$x){$ASoldierPercent = $a;}
echo $ASoldierPercent;
}
?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

@Sorcher, is English your native language? If not, you're forgiven :)

Nah im norwegian, and i sincerely apologize Ezz, was the heat of the moment and i really had little time. Thank you for replying.

0
<?
for($a=1;$a<9;$a++){
$x = $a*10;
if($attackerSoldiers >$x){$ASoldierPercent = $a;}
echo $ASoldierPercent;
}
?>

Didn't + any percent :/
The $victim got 105 soldiers at this moment, and the percent rate still stands on 1

$filename = "../u/txt/userSoldiers/".$victim.".txt";
	$handle = fopen($filename, "r");
	$victimSoldiers = fread($handle, filesize($filename));
	fclose($handle);
	for($a=1;$a<9;$a++){
	$x = $a*10;
	if($victimSoldiers >$x){$VSoldierPercent = $a;}
}
	echo "".$victimSoldiers."<br>";
	echo $VSoldierPercent;
	die;

Edited by Sorcher: n/a

0

Check for loop by huhh7 but testing if the modulo (%) brings zero it is divisible by 10 the do whatever you want, that is

for($i=0; $i<=100; i++){
    if($i%10==0){
        //do whatever it is tenth step
    }
}

Completely untested :)

0

Insensus already posted a good solution for this in the very first reply. All of the looping is completely unnecessary.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.