1

Instead of having code like the following:

if ( $Boss1 ==1)
            {
                $Boss1_holder = $tick;
            }

            else if ( $Boss1 ==0)
            {
                $Boss1_holder = $cross;
            }



            if ( $Boss2 ==1)
            {
                $Boss2_holder = $tick;
            }
            else if ( $Boss2 ==0)
            {
                $Boss2_holder = $cross;
            }


            if ( $Boss3 ==1)
            {
                $Boss3_holder = $tick;
            }
            else if ( $Boss3 ==0)
            {
                $Boss3_holder = $cross;
            }


            if ( $Boss4 ==1)
            {
                $Boss4_holder = $tick;
            }
            else if ( $Boss4 ==0)
            {
                $Boss4_holder = $cross;
            }

Can i instead do something like this:

                $i = 1;
            while ($i <= 4) 
            {
                if ( $Boss[$i] ==1)
                {
                    $boss_holder[$i] = $tick;
                }

                else if ( $Boss[$i] ==0)
                {
                    $boss_holder[$i] = $cross;
                }

            }

I'm not entirely sure how to do it. At the moment if only have 4 sets of if else statements but my program later would require about 30 of them and i would rather having something like my second block that will reduce the amount of lines required. Am i on the right track. I think the $boss_holder[$i] part is incorrect as [i] would be part of an array? however i dont know how to write this correctly. I have looked on the php manual page but a lot of the examples are some what complex and go above the scope i need.

To clarify if i have explanined this poorly. I am trying to add a value onto the end of the variable $boss_holder that increments with the while loop counter

    $Boss1_holder = '';
$Boss2_holder = '';
$Boss3_holder = '';
$Boss4_holder = '';
$Boss5_holder = '';
$Boss6_holder = '';
$Boss7_holder = '';
$Boss8_holder = '';
$Boss9_holder = '';
$Boss10_holder = '';
$Boss11_holder = '';
$Boss12_holder = '';
$Boss13_holder = '';
$Boss14_holder = '';
$Boss15_holder = '';
$completed1 = 0;
$completed2 = 0;
$completed3 = 0;
$counter = 0;
$tick ='&nbsp;<img src="/styles/subsilver2/theme/images/soap/tick.png">';
$cross ='&nbsp;<img src="/styles/subsilver2/theme/images/soap/cross.png">';

class checks {

private $addthemup1;
private $addthemup2;
private $addthemup3;

public function checkBosses($Boss1, $Boss2, $Boss3, $Boss4, $Boss5, $Boss6, $Boss7, $Boss8, $Boss9, $Boss10, $Boss11, $Boss12, $Boss13, $Boss14, $Boss15)
{
    global  $tick, $cross, $completed1, $completed2, $completed3, $Boss1_holder, $Boss2_holder, $Boss3_holder, $Boss4_holder, $Boss5_holder, $Boss6_holder, $Boss7_holder, $Boss8_holder, $Boss9_holder, $Boss10_holder, $Boss11_holder, $Boss12_holder, $Boss13_holder, $Boss14_holder, $Boss15_holder;

    $addthemup1 = $Boss1 + $Boss2 + $Boss3 + $Boss4 + $Boss5;
    $addthemup2 = $Boss6 + $Boss7 + $Boss8 + $Boss9 + $Boss10;
    $addthemup3 = $Boss11 + $Boss12 + $Boss13 + $Boss14 + $Boss15;

    switch ($addthemup1)
            {
                case "5":
                $completed1 = $tick;
                break;

                case "4":
                $completed1 ='4/5';
                break;  

                case "3":
                $completed1 ='3/5';
                break;  

                case "2":
                $completed1 ='2/5';
                break;

                case "1":
                $completed1 ='1/5';
                break;  

                case "0":
                $completed1 ='0/5';
                break;  
            }
    switch ($addthemup2)
            {
                case "5":
                $completed2 = $tick;
                break;

                case "4":
                $completed2 ='4/5';
                break;  

                case "3":
                $completed2 ='3/5';
                break;  

                case "2":
                $completed2 ='2/5';
                break;

                case "1":
                $completed2 ='1/5';
                break;  

                case "0":
                $completed2 ='0/5';
                break;  
            }
    switch ($addthemup3)
            {
                case "5":
                $completed3 = $tick;
                break;

                case "4":
                $completed3 ='4/5';
                break;  

                case "3":
                $completed3 ='3/5';
                break;  

                case "2":
                $completed3 ='2/5';
                break;

                case "1":
                $completed3 ='1/5';
                break;  

                case "0":
                $completed3 ='0/5';
                break;  
            }




            if ( $Boss1 ==1)
            {
                $Boss1_holder = $tick;
            }

            else if ( $Boss1 ==0)
            {
                $Boss1_holder = $cross;
            }



            if ( $Boss2 ==1)
            {
                $Boss2_holder = $tick;
            }
            else if ( $Boss2 ==0)
            {
                $Boss2_holder = $cross;
            }


            if ( $Boss3 ==1)
            {
                $Boss3_holder = $tick;
            }
            else if ( $Boss3 ==0)
            {
                $Boss3_holder = $cross;
            }


            if ( $Boss4 ==1)
            {
                $Boss4_holder = $tick;
            }
            else if ( $Boss4 ==0)
            {
                $Boss4_holder = $cross;
            }


            if ( $Boss5 ==1)
            {
                $Boss5_holder = $tick;
            }
            else if ( $Boss5 ==0)
            {
                $Boss5_holder = $cross;
            }

            if ( $Boss6 ==1)
            {
                $Boss6_holder = $tick;
            }

            else if ( $Boss6 ==0)
            {
                $Boss6_holder = $cross;
            }

            if ( $Boss7 ==1)
            {
                $Boss7_holder = $tick;
            }
            else if ( $Boss7 ==0)
            {
                $Boss7_holder = $cross;
            }


            if ( $Boss8 ==1)
            {
                $Boss8_holder = $tick;
            }
            else if ( $Boss8 ==0)
            {
                $Boss8_holder = $cross;
            }


            if ( $Boss9 ==1)
            {
                $Boss9_holder = $tick;
            }
            else if ( $Boss9 ==0)
            {
                $Boss9_holder = $cross;
            }


            if ( $Boss10 ==1)
            {
                $Boss10_holder = $tick;
            }
            else if ( $Boss10 ==0)
            {
                $Boss10_holder = $cross;
            }

            if ( $Boss11 ==1)
            {
                $Boss11_holder = $tick;
            }

            else if ( $Boss11 ==0)
            {
                $Boss11_holder = $cross;
            }



            if ( $Boss12 ==1)
            {
                $Boss12_holder = $tick;
            }
            else if ( $Boss12 ==0)
            {
                $Boss12_holder = $cross;
            }


            if ( $Boss13 ==1)
            {
                $Boss13_holder = $tick;
            }
            else if ( $Boss13 ==0)
            {
                $Boss13_holder = $cross;
            }


            if ( $Boss14 ==1)
            {
                $Boss14_holder = $tick;
            }
            else if ( $Boss14 ==0)
            {
                $Boss14_holder = $cross;
            }


            if ( $Boss15 ==1)
            {
                $Boss15_holder = $tick;
            }
            else if ( $Boss15 ==0)
            {
                $Boss15_holder = $cross;
            }


}

}

$sql = 'SELECT * FROM raid_progression';

$result = $db->sql_query($sql);

  while ($row = $db->sql_fetchrow($result))
  {      
            $counter = $counter+1;

            $checker1 = new checks();   
            $checkResult1 = $checker1-> checkBosses($row['kp_em_no_bonethr'], $row['kp_em_no_foreman'], $row['kp_em_no_heavyfab'], $row['kp_em_no_jargsor'], $row['kp_em_no_kragga'],$row['kp_em_ha_bonethr'], $row['kp_em_ha_foreman'], $row['kp_em_ha_heavyfab'], $row['kp_em_ha_jargsor'], $row['kp_em_ha_kragga'],$row['kp_em_kn_bonethr'], $row['kp_em_kn_foreman'], $row['kp_em_kn_heavyfab'], $row['kp_em_kn_jargsor'], $row['kp_em_kn_kragga']);



            $template->assign_block_vars('raid_op', array(
            'VAR0' => $counter,
            'VAR1' => $row['guild_name'],
            'VAR2' => $row['guild_website'],
            'VAR3' => $row['server_name'],
            'VAR4' => $row['server_region'],
            'VAR5' => $row['faction_name'],
            'VAR6' => $completed1,
            'VAR7' => $Boss1_holder,
            'VAR8' => $Boss2_holder,
            'VAR9' => $Boss3_holder,
            'VAR10' => $Boss4_holder,
            'VAR11' => $Boss5_holder,
            'VAR12' => $Boss6_holder,
            'VAR13' => $Boss7_holder,
            'VAR14' => $Boss8_holder,
            'VAR15' => $Boss9_holder,
            'VAR16' => $Boss10_holder,
            'VAR17' => $Boss11_holder,
            'VAR18' => $Boss12_holder,
            'VAR19' => $Boss13_holder,
            'VAR20' => $Boss14_holder,
            'VAR21' => $Boss15_holder,
            'VAR22' => $completed2,
            'VAR23' => $completed3,
            ));






  }

  $db->sql_freeresult($result);
3
  • Will the Bosses only be holding 'ticks' and 'crosses'? Commented Mar 10, 2012 at 17:46
  • Yes. $tick ='&nbsp;<img src="tick.png">'; $cross ='&nbsp;<img src="cross.png">'; Commented Mar 10, 2012 at 17:51
  • I have updated at the bottom my description as it may have been poorly explained Commented Mar 10, 2012 at 17:59

4 Answers 4

3

There's a few ways to do it...

$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
    switch($value){
         case 1: 
            //do something
         break;
         case 2: 
            //something else;
         break;
    }
}

Would probably be how I wrote it.

Edit: If you're looking for clean code, you can do something like this:

bosshandler.php:

function doSomethingWithBoss($value){
    switch($value){
         case 1: 
            return "boss equals 1";
         break;
         case 2: 
            return "boss equals 2";
         break;
    }
}

Your script page:

include bosshandler.php;
$boss = new array(1, 2, 3, 4);
foreach($boss as $value){
    echo doSomethingWithBoss($value);
}

Ok, trying again. I think I see what you're asking now:

You want to assign a variable so that your arrays match up. If you're in $boss[1] you want to assign something to $boss_holder['1'], is that correct? If so, you don't want to use a foreach array, as it can be somewhat unpredictable with arrays and the order in which it parses them.

Instead, let's use a for() loop, like so:

$boss = new Array(1, 2, 3, 4);
$boss_holder = new Array(); //empty
$length = count($boss);
//iterate through boss
for($i = 1; $i < $length; $i++){
    if($boss[$i] == 1){
        $boss_holder[$i] = $tick;
    }elseif($boss[$i] == 2){
        $boss_holder[$i] = $cross;
    }
}

Edit again to reflect your full code:

Use arrays. Refactor your code so that it uses arrays. If for some reason you absolutely cannot or will not use arrays, you can use PHP's variable variables to accomplish what you're looking for. It would look something like this:

var $boss1_holder, $boss2_holder, $boss3_holder;
for($i=1; $i> 4; $i++){
    $variableName = 'boss' . $i . '_holder';
    $$variableName = 1;
}
echo $boss2_holder; //returns 2
Sign up to request clarification or add additional context in comments.

8 Comments

That's how I would do it too.
i'd prefer to keep away from this as i would have alot of case statements and im trying to reduce the code count
I find case statements to be more readable than a ton of if/else blocks. If you want to make your code look cleaner, you can put the code inside a function, and then call the function inside the foreach. I'll edit to give you an example of that.
I have updated at the bottom my description as it may have been poorly explained
@RyanWalkowski Check out my latest answer... i think I understand the question now, let me know if that helps?
|
1

Take a look at this code:

$bosses = new array(1, 2, 1, 1, 2);
$boss_holders = new array()

for ($i = 1; $i <= count($boss); $i++) {
    // if bosses[i] equals 1, we make the current boss_holder equal a $tick
    if($bosses[i] == 1) 
        array_push($boss_holders, $tick);

    // if the current boss doesn't equal 1 then it must equal 2
    // so we add a $cross instead
    else
        array_push($boss_holders, $cross);
}

Please note: The above code is untested and may contain errors, as my PHP's a little (well, a lot) rusty.

What it does is creates a new array of bosses (you can set this value to whatever numbers you have for your bosses), and an empty array called $boss_holders. We then go through the array of bosses and if the current Boss equals 1, then we add a $tick. Otherwise, the current Boss equals 2, so we'll add a $cross.

When the for loop has finished running, what you'll have is an array of boss_holders that reflects the values of the bosses. If boss1 = 1, boss2 = 2, and boss3 = 1, the array boss_holders will look like this: [$tick, $cross, $tick], and so on. When you need to access boss_holder_2's value, for instance, you get that value by accessing the boss_holder array like this: boss_holders[2].

Does that make sense? Is that what you need?

Comments

0

Maybe you can use switch.

From the first example:

if ($i == 0) {
  echo "i equals 0";
} elseif ($i == 1) {
  echo "i equals 1";
} elseif ($i == 2) {
  echo "i equals 2";
}

Is the same like:

switch ($i) {
  case 0:
    echo "i equals 0";
    break;
  case 1:
    echo "i equals 1";
    break;
  case 2:
    echo "i equals 2";
    break;
}

but looks much cleaner.

3 Comments

That isn't what he's doing though.
That's suboptimal. A while loop is probably the best way to go in this case, although I suppose that depends on the OP's response to my comment on his question.
Yeah, i fear my written explanation may have not been entirely clear. I am trying to give the variable $boss_holder a number on the end of it that would go up instead with the while loop
0
Have you tried using variable variables? The first script will print out the variables created, while the second demonstrates that a variable can be defined or determined by modifying the "if" statement.  Maybe this will be helpful for your situation. `

` "; $i++; } ?>

<?php
$i=1;
$boss4_holder=true;

while($i<10){
    $q="boss".$i."_holder";

    if ($$q==true){
         echo "true";
    }
$i++;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.