Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6583719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:26:46+00:00 2026-05-25T16:26:46+00:00

I’m trying to simplify some code where I have an extremely long switch statement

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:26:46+00:00Added an answer on May 25, 2026 at 4:26 pm
    $surveydata = mysql_query( … );
    $targetCities[0]=array('New York',0,250);
    $targetCities[1]=array('Los Angeles',0,250);
    //etc
    //etc
    //etc
    $respondentCities=array();
    
    while ($responses = mysql_fetch_assoc($surveydata)) {
        array_push($respondentCities,$responses['cou_mun']);
    } //i'm sorting the values in $respondentCities because i need them later
    
    //function amountUnderMin(){…}
    //function cityMinMet(){…}
    
    for ($i=0; $i < count($targetCities); $i++){
        foreach($respondentCities as $respondentCity){
            if($respondentCity==$targetCities[$i][0]){$targetCities[$i][1]++;break;}
        }
        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>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have a text area in my form which accepts all possible characters from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.