1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

simple php help needed.

Discussion in 'PHP' started by cgo85, Dec 8, 2004.

  1. #1
    I'm kinda learning php as I go.... need a little help with this one. Pages are dynamic and get information from the following:

    <?php
    if ($state == 'Arizona') { $abbr = "AZ"; } { $misspell = "Arazona"; }
    if ($state == 'California') { $abbr = "CA"; } { $misspell = "Californa"; }
    if ($state == 'Colorado') { $abbr = "CO"; } { $misspell = "Colarado"; }
    if ($state == 'Connecticut') { $abbr = "CT"; } { $misspell = "Conecticut"; }
    if ($state == 'Delaware') { $abbr = "DE"; } { $misspell = "Deleware"; }
    ?>

    What's happening is that the first part of the if then statement is working on the page but the misspell is taking it from the last in the list. For example... An arizona page will correctly use the ($state == 'Arizona') and the { $abbr = "AZ"; } but then it is taking the misspell from { $misspell = "Deleware"; }. It's probably something simple that I'm just missing. Please help.
     
    cgo85, Dec 8, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Your problem has to do with having too many curly brackets...

    To keep it clean, I would do this:
    <?php
    	switch ($state) {
    	case "Arizona":
    		$abbr = 'AZ';
    		$misspell = 'Arazona';
    		break;
    	case "California":
    		$abbr = 'CA';
    		$misspell = 'Californa';
    		break;		
    	case "Colorado":
    		$abbr = 'CO';
    		$misspell = 'Colarado';
    		break;	
    	}
    ?>
    PHP:
     
    digitalpoint, Dec 8, 2004 IP
  3. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks alot Shawn... got it workin with that code.
     
    cgo85, Dec 8, 2004 IP
  4. Owlcroft

    Owlcroft Peon

    Messages:
    645
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Re:

    <?php
        switch ($state) {
        case "Arizona":
            $abbr = 'AZ';
            $misspell = 'Arazona';
            break;
        case "California":
            $abbr = 'CA';
            $misspell = 'Californa';
            break;        
        case "Colorado":
            $abbr = 'CO';
            $misspell = 'Colarado';
            break;
        }
    ?> 
    PHP:
    It is, I'd say, always wise to add a default option to switch blocks for the possibility that an unexpected (or invalid) option is presented:

    <?php
        switch ($state) {
        case "Arizona":
            $abbr = 'AZ';
            $misspell = 'Arazona';
            break;
        case "California":
            $abbr = 'CA';
            $misspell = 'Californa';
            break;        
        case "Colorado":
            $abbr = 'CO';
            $misspell = 'Colarado';
            break;    
        default:
            --put here whatever is necessary to safely handle unexpected inputs--
        }
    ?> 
    PHP:
    Note that no break statement is needed for the last option (though it assuredly is for all the others), though putting one in is harmless.
     
    Owlcroft, Dec 8, 2004 IP
  5. mushroom

    mushroom Peon

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Or an other way
     
    mushroom, Dec 8, 2004 IP
  6. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I tried that #2 before I posted to the forum and it didn't work. I was getting the same result as the code I posted.
     
    cgo85, Dec 9, 2004 IP
  7. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    What exactly are you trying to accomplish? Are you trying to catch a misspelled state that a user might accidently submit to your site through a form? Or are you trying to add common misspellings to your pages so you catch searches done from a search engine with a misspelled state in the search terms?
     
    rvarcher, Dec 9, 2004 IP
  8. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    misspellings on pages.... but don't worry about it, it's already figured out.
     
    cgo85, Dec 10, 2004 IP