Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Functions and Global Variables

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #203159
    Steven
    Participant

    Is there any way to tell a function, “Hey, make all variables inside this function GLOBALS”? I only ask because having $GLOBALS['foo'] all over the place just aesthetically bugs me—and that is probably not a good enough reason to do so—but I’m curious.

    Here’s my function:

    function county($state) {
        switch ($GLOBALS['state']) {
            case "Iowa":            $GLOBALS['county'] = $GLOBALS['ia_county'];
            break;
            case "Minnesota":       $GLOBALS['county'] = $GLOBALS['mn_county'];
            break;
            case "South Dakota":    $GLOBALS['county'] = $GLOBALS['sd_county'];
            break;
            case "Wisconsin":       $GLOBALS['county'] = $GLOBALS['wi_county'];
        }
        return $GLOBALS['county'];
    }
    
    #203162
    Steven
    Participant

    My mind a’sploded a little when I got to the code examples. I’m self-“taught” in PHP, so I’m still trying to grasp the basics… and the basics/best practices of programming in general.

    Let me quickly try to explain exactly what I’m trying to do, so maybe you can expound a little better.

    This is for a WordPress site. I created a custom post type, and created a number of custom fields for it via Advanced Custom Fields Pro plugin. There is a “State” field with four possible values: IA, MN, SD, WI, and four respective county fields: “Iowa County”, “Minnesota County”, etc. These four county fields are populated with the respective counties for that state. Now, in ACF I set it up that way, so I could tell it, if the user has selected X state then display the dropdown for Y county (it made sense to me at the time, maybe I’m wrong in that decision?).

    Anyway… now when I’m in the WP Loop I need to grab their State value, and depending on what that is, grab the value of their corresponding county field. I started with if/else statements, did a switch statement, then tried moving that switch statement to my own custom_functions.php file (which is required inside functions.php) and placed it inside a function, thinking I was really clever… Then I could just call county($state) any time I needed to grab an entry’s county.

    So my original function was a bad idea (thanks!), but I still can’t understand your examples (sorry..)

    #203216
    Steven
    Participant

    The county_map makes sense now. Thanks a bunch!

    Currently $state contains the full name of each state, but as with everything else is subject to change ;-)

    At various times in the development of this project, I wish I were just a little more comfortable with Rails and chose to develop it on that. WordPress can be great, but it can get real annoying (in instances like this!).

    Thanks again for the help.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.