Jump to content

Apply condition to the output based on the count value?


RON_ron

Recommended Posts

I'm not sure what am I doing wrong?

 

I want to check if match1, match2 or match 3 has "W" and the continues count ($numOfWinsCount) to be maximum of 2. If $numOfWinsCount is more than 2 do not continue with

$matchresult . $matchWcounter = "Qualified for Quarter Finals!";

 

My Code:

$match1 = "W"
$match2 = "W"
$match3 = "L"
$getawayr="Group A";

if($getawayr=="Group A"){
for ( $matchWcounter = 1; $matchWcounter <= 3; $matchWcounter ++) {
$numOfWinsCount = 0;
if($match . $matchWcounter=="W" and $numOfWinsCount <3){
$numOfWinsCount=$numOfWinsCount+1;
$matchresult . $matchWcounter = "Qualified for Quarter Finals!";
}
}
}

you need to use either an array or this...

${'match'.$matchWcounter}

$match . $matchWcounter is appending their values, rather than the variable names.

 

Personally I think an array is more appropriate.

$match = array();
$match[1] = "W"
$match[2] = "W"
$match[3] = "L"
$getawayr="Group A";

if($getawayr=="Group A"){
for ( $matchWcounter = 1; $matchWcounter <= 3; $matchWcounter ++) {
$numOfWinsCount = 0;
if($match[$matchWcounter]=="W" and $numOfWinsCount <3){
$numOfWinsCount=$numOfWinsCount+1;
$matchresult . $matchWcounter = "Qualified for Quarter Finals!";
}
}
}

$match = array('W','W','L');
$getAwayR = "Group A";

if($getAwayR == "Group A"){
    $numWins = 0;
    for($i = 0; $i < count($match); i ++){
        if($match[$i] == "W" && $numWins < 3){
            // whatever else
            $numWins++;
        }
    }
}

First off, $numOfWingsCount will never be greater than 2 because you set it to 0 every iteration. You need to assign it to 0 before the for loop.

 

If you were using an array this would be easier. You could do:

$match = array('W', 'W', 'L');

// inside for loop
if ($match[$matchWcounter] == 'W'

 

Otherwise you have to do something like this:

 // inside for loop
if (${'match' . $matchWcounter} == 'W'

 

I can't test this right now so, try it and see if it works.

$match = array('W','W','L');
$getAwayR = "Group A";

if($getAwayR == "Group A"){
    $numWins = 0;
    for($i = 0; $i < count($match); i ++){
        if($match[$i] == "W" && $numWins < 3){
            // whatever else
            $numWins++;
        }
    }
}

this is a much cleaner solution RON_ron, I'd just change one thing.

$count = count($match);
for($i = 0; $i < $count; i ++){

wow! thanks guys! but something is wrong. It says syntax error, unexpected T_INC, expecting ')'

 

$getAwayR = "Group A";
$matchInArray = array($match1, $match2, $match3, $match4, $match5, $match6, $match7, $match8, $match9);
$matchInArrayResu = array($matchresult1, $matchresult2, $matchresult3, $matchresult4, $matchresult5, $matchresult6, $matchresult7, $matchresult8, $matchresult9);

if($getAwayR == "Group A"){
    $numWins = 0;
    for($i = 0; $i < count($matchInArray); i ++){
        if($matchInArray[$i] == "W" && $numWins < 3){
            $matchInArrayResu[$i] = "Qualified for Quarter Finals!";
            $numWins++;
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.