Page 1 of 1

A Shorter Way?

Posted: Wed Nov 12, 2014 2:51 pm
by Epiales
Is there a way to shorten this so I don't have to write it everytime before each else?

The first code right below this is what I have to repeat.....the next code is the actual code (battle), and the last is the forms it uses to display the results.

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>

Code: Select all

<script>
    setTimeout('document.weapons.life.value = "<?php echo $playerHp; ?>";', 0);
</script>

<?php 
    $player_accuracy = mt_rand($user_accuracy, 100);
    $player_atkdam = mt_rand(0,$user_strength);
    $player_critical = mt_rand(0,$user_strength)*2;

if($player_accuracy > 80 && $player_critical>=$user_strength){

    $outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    </script>

<?php

}elseif($player_accuracy < 80 || $player_accuracy > 80) {

    $outputList .= 'Your attack hits and ' . $arena_char_name . ' suffers ' . $player_atkdam . ' health'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    </script>

<?php

}else{

    $outputList .= 'Your attacked missed!  You really suck !'; ?>
    
    <script>
    setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
    </script>

<?php

    }

Code: Select all

<form name="status">
<input type="text" readonly="true"  name="msgbx" size="80" maxlength="80" onFocus="FocusOut()" onKeyDown="javascript: alert('Don\'t do that!');" style="background-color:#575957; border:none">
</form>

<form name="status1">
<input type="text" readonly="true"  name="msgbx1" size="80" maxlength="80" onFocus="FocusOut()" onKeyDown="javascript: alert('Don\'t do that!');" style="background-color:#575957; border:none">
</form>

Re: A Shorter Way?

Posted: Thu Nov 13, 2014 8:27 am
by MikuzA
Hello,

Since you are using an IF sentence, you could do it like this.

Code: Select all

IF ( CRIT ) {
$Output = 'Crit';
}
ELSEIF ( HIT ) {
$Output = 'Hit';
}
ELSE{
$Output ='Miss';
}
?>
<script>js_here[echo $Output]</script>
<?PHP
(And notice the above is not real code :D)

What I am saying is that during the IF-ELSE process, there is only one way out. and after that you can display the generic stuff using the happening inside the IF.

Re: A Shorter Way?

Posted: Fri Nov 14, 2014 1:44 am
by Jackolantern
This code doesn't really have a point:

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>
The PHP variable $outputlist is only going to be evaluated once on the server, and each time the timer goes off, it will be overwriting with the same value. Basically, you will be putting a literal constant into status.msgbx every 2 seconds.

Re: A Shorter Way?

Posted: Fri Nov 14, 2014 1:48 am
by Epiales
Jackolantern wrote:This code doesn't really have a point:

Code: Select all

	$outputList .= 'Your critical attack hits and ' . $arena_char_name . ' suffers ' . $player_critical . ' health'; ?>
	
	<script>
	setTimeout('document.status.msgbx.value = "<?php echo $outputList; ?>";', 2000);
	</script>
The PHP variable $outputlist is only going to be evaluated once on the server, and each time the timer goes off, it will be overwriting with the same value. Basically, you will be putting a literal constant into status.msgbx every 2 seconds.
I went a different route with this in my other threads. I got rid of the output and such. Thx :)