Danimal {l Wrote}:a condition checks if the bonus type is "hp", "mp" (or any other I´ll add in the future);
As I've said, if you want to reference a value later, then just put it in a variable. For example,
- {l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, 1 * $level)?>
Here I've moved "rand(0, 5)" up and gave a name to it. Then you can use this $bonustype variable in the array index as well as in expressions for the conditionals.
Danimal {l Wrote}:2 extra hp or mp is useless on a lv5 item, so the condition would check first if any of those bonus types have been output (hp, mp...) leaving the other types untouched(mental, defense...) but if hp, mp... are detected the numerical value (bonus=hp,2) is multiplied with random value of 3 to 7, so "2 * rand(3,7)" it would then print something like "bonus=hp,8" (2*4).
For example:
- {l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, $level) * ($bonustype < 2 ? rand(3, 7) : 1)?>
Or you could reference more $bonustype with logical and (&&) and logical or (||) operators. For example
- {l Code}: {l Select All Code}
...
$bonustype = rand(0, 5);
?>
bonus=<?=["hp","mp","mental","offense","defense","absorb_min"][$bonustype]?>,<?=rand(1, $level) * ($bonustype == 0 || $bonustype == 1 ? rand(3, 7) : 1)?>
Which means bonus type is hp or bonus type is mp, then mupltiply by a random value.
Danimal {l Wrote}:From what little I remember about programming a switch would be best?
That depends how many kind of output you want. If two different kind is enough, then trinary or "if" block is the best (here you can count an empty output as one option). Looks like this:
- {l Code}: {l Select All Code}
<? if(expression) { ?>
things to output if expression is true
<? } else { ?>
things to output if expression is false
<? } ?>
If the second option is empty, then you can simply omit the "else" part, like this:
- {l Code}: {l Select All Code}
<? if(expression) { ?>
things to output if expression is true
<? } ?>
For a switch, the scheme is similar, but as you've said it, multiple outputs can be defined based on the value of the given expression.
- {l Code}: {l Select All Code}
<? switch(expression) {
case (v1): ?>things to print if value is v1
<? case (v2): ?>things to print if value is v2
<? case (v3): ?>things to print if value is v3
<? case (v4): ?>things to print if value is v4
<?
...
} ?>
Danimal {l Wrote}:Then no doubt more complex but nicer looking, the names could be generated by expanding the switch loop to house all possible bonus types.
I don't know if this is more readable, or nicer looking, but definitely a solution and useful if you want different output per the expression's value. For example:
- {l Code}: {l Select All Code}
<? switch($bonustype) {
case 0: ?>bonus=hp,<?=rand(1,2)?>
<? case 1: ?>bonus=mp,<?=rand(3,4)?>
<? case 2: ?>bonus=mental,<?=rand(5,6)?>
<? case 3: ?>bonus=defense,<?=rand(7,8)?>
<? } ?>
Danimal {l Wrote}:Variables declaration, i guess this goes right after header and not here?
Yes. Go to the end of the first line, go back two characters and press an Enter. So
- {l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){?>
should became
- {l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
?>
Now define your variables after the first line, moving the "?>" line down.
- {l Code}: {l Select All Code}
<?php foreach($_SERVER['argv'] as $k=>$v)if($k>0){$a=explode("=",$v)[0];$$a=explode("=",$v)[1];}for($id=$startid;$id<=$endid;$id++){
$bonustype = rand(3,7); // here comes a variable definition.
?>
On the left side of "=" you name the variable (all variables must be prefixed by dollar sign), and on the right side use the same expression that you've used in the template. Finally close the line with a semicolon ";" and an Enter.
Danimal {l Wrote}:$traits #stores how many bonus traits the item will have, needs to take it from console input
For that, no need to define a variable, just add "traits=N" on the command line, and the $traits variable will be created for you.
The rest should look like this (note: I haven't checked this code, just wrote from memory to demonstrate syntax for you. Might contain errors)
- {l Code}: {l Select All Code}
for($i = 1; $i <= $traits; $i++) { // iterate traits time, $i will be 1 .. $traits
$bonus=["hp","mp","mental","offense","defense","absorb_min"][rand(0,5)]; // no need for <??> here, you're not in output mode
$rand_value= rand(1, 1 * $level);
switch ($bonus) {
case "offense": // $bonus is a string, so you need to use quotes here
?>bonus=offense,<?=rand_value?>
<? // use ?>things to print<?, and newline might be important here as you need that in the output too
And
- {l Code}: {l Select All Code}
if i=1 and i<3
makes no point, because if "i" is 1, then it is also less than 3, but just for demonstation
- {l Code}: {l Select All Code}
if ($i == 1 && $i < 3) {
Danimal {l Wrote}:Im sorry if it made your eyes bleed, but this the best i can express what want it to be.
Don't worry, and I hope I could help. A few rules to keep in mind:
- variables are always prefixed by "$"
- strings must be in quotes in programming mode
- to output something, switch to output mode with "?>" and switch back to programming mode with "<?"
- one equal sign "=" assigns a variable, two "==" checks if it's equal to (if you're not declaring a variable at the top, then remember to always use double "==").
Cheers,
bzt