I’m trying to simplify some code where I have an extremely long switch statement (20+ cases). The only thing I need from the switch is to increment a counter.
The currently working switch is set up like this:
//multidimensional-array is above
while ($responses = mysql_fetch_assoc($surveydata)){
switch($responses['cou_mun']){
case "New York":
$targetCities[0][1]++;
break;
case …
}
}
I have the following multi-dimensional array and for loop:
// [index] 'city name',count,minimum
$targetCities[0]=array('New York City',0,250);
$targetCities[1]=array('Los Angeles',0,250);
// 20 more
$responses = mysql_fetch_assoc($surveydata); //this is taken from the while-loop when I comment out the while() and switch().
for ($i=0; $i < count($targetCities); $i++){
if ($responses['cou_mun'] == $targetCities[$i][0]){$targetCities[$i][1]++;}
else{$nontargetcity++;}
echo "<li><span>" . $targetCities[$i][0] . "</span><span>" . $targetCities[$i][1] . "</span>" . amountUnderMin($targetCities[$i][1],$targetCities[$i][2]) . "<span>" . $targetCities[$i][2] . "</span><span>" . cityMinMet($targetCities[$i][1],$targetCities[$i][2]) . "</span></li>";
}
$responses['cou_mun'] returns a city-name (such as “Los Angeles”).
The only thing that’s not working is the if (increments the counter).
I expect the 2nd dimension of the current $targetCities to be incremented, however when the line is printed/echo’d, it still shows as 0 (yet I can see in the Db that there is more than one “Los Angeles”).
P.S. I made sure the names in the array exactly match (spell+whitespace+case) the name options in the Db.
Thanks!
EDIT: the $nontargetcity counter is returning 21 (the number of cases).
1 Answer