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

Recommended Answers

All 11 Replies

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);

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?

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.

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..

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.

@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
<?
for($a=1;$a<9;$a++){
$x = $a*10;
if($attackerSoldiers >$x){$ASoldierPercent = $a;}
echo $ASoldierPercent;
}
?>

@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.

<?
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;

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 :)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.