PHP IF code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

i used the code for 5.2 and it still doesn't work.

<select name="state" >
<?php
$states = array(
'CA' => 'California',
'FL' => 'Florida',
'NY' => 'New York',
);
?>
<option value="">---</option>
<?php foreach ($states as $abbr => $name): ?>
<option value="<?= $abbr; ?>"><?= $name; ?></option>
<?php endforeach; ?>
</select>
thanks a lot for helping me.. i tried to upgrade to 5.5 in my cpanel but i get internal server error so i reverted back to 5.2
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

there isn't a way i can just set a variable on my process page

$fullstatename = florida
$abbreviation = FL

thats all i want to come out of it
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

donny wrote:it still doesn't work.
That's not really giving me much to work with. Is error reporting turned on? What error(s) are you seeing? What's happening? The more specific you can be, the better.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

how do i turn on error reporting? in my cpanel i have it turned on if thats how it is turned on.

the form option field doesn't list any states

Code: Select all

<select name="state" >
<?php
$states = array(
        'CA' => 'California',
        'FL' => 'Florida',
        'NY' => 'New York',
);
?>
        <option value="">---</option>
        <?php foreach ($states as $abbr => $name): ?>
        <option value="<?= $abbr; ?>"><?= $name; ?></option>
        <?php endforeach; ?>
</select>
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

basically i just want to have my form option select a state... and on my next page i want it to be in 2 variables $fullstatename $stateabbreviation

thats all
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

Code: Select all

error_reporting(-1);
You don't really want to be doing that on a live site, mind you. Do you not have some sort of dev/staging environment? Trying to guess what errors you're encountering on a server you don't control running a badly outdated version of PHP is going to be a challenge.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

my site isn't live yet.. there isnt a way i can set the abbreviation variable on the processing page like if $state = California then $abbreviation = CA

something along those lines?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

That's exactly what we've been trying to do. Using an array seems both the simplest and most easily extensible. Sure, you could set up a bunch of conditionals

Code: Select all

if ($_POST['state'] == 'California') {
    $state_abbr = 'CA';
} else if ($_POST['state'] == 'New York') {
 $state_abbr = 'NY';
}
// etc
But that's not terribly efficient and it's ignoring the larger problem of why iterating over an array isn't working.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

it works for me.

thanks a lot for helping
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

Code: Select all

if ($_POST['state'] == 'California') {
    $state_abbr = 'CA';
} else if ($_POST['state'] == 'New York') {
 $state_abbr = 'NY';
}
// etc
how can i add more states to this?
also is it possible to run another version of this script with different fields on the same process page?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

Just keep adding else if statements. Like I mentioned above, though, that's really solving the wrong problem.
Post Reply