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

PHP IF code

Post by donny »

Hello,

I have a form page that inputs data into a database.

I need help writing a code that will enter data in another field of my database depending on what was inputed into the form.

For example, my form field is state. If they enter Florida I want to be able to enter Florida (original form value) and then in another field of my database I want to add FL for abbreviation of the state they chose. I need these to be in 2 separate fields of my database.

Can somebody help me?

Thank you very much!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

How are you handling the abbreviations? What if they enter something outside of the US? What if they spell it wrong?
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

the form field is a option field. i don't know how I'm going to handle the abbreviations thats what i need to figure out. theres only 5 states that i am using my business is only in the tri-state area.

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

Re: PHP IF code

Post by Celauran »

The simplest solution may be just an array.

Code: Select all

$states = [
    'FL' => 'Florida',
    'GA' => 'Georgia',
];
You could use key/value to create the select list and lookup the submitted value to get the full name again. Then again, using that approach, I can't think of a reason you'd need to store both.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

For the form

Code: Select all

<select name="state">
	<option value="">---</option>
	<?php foreach ($states as $abbr => $name): ?>
	<option value="<?= $abbr; ?>"><?= $name; ?></option>
	<?php endforeach; ?>
</select>
and to process the submitted form.

Code: Select all

if (isset($_POST['state'])) {
	$state_full_name = $states[$_POST['state']];
}
Something along those lines, at any rate.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

so say my form field for the states posts data to the next page and lets say the form field for states is $_POST['state'] how can i set the abbreviation variable ?

for example

state selected is california ($state)
form gets submitted
on the next page i want to be able to echo $state = california and $abbreviation = CA

thanks a lot
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

array_search()
Or use the array keys as your form values as I posted above.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

Code: Select all

<select name="state" >
<?
$states = [
    'NY' => 'NEW YORK',
    'FL' => 'FLORIDA',
];
?>      <option value="">---</option>
        <?php foreach ($states as $abbr => $name): ?>
        <option value="<?= $abbr; ?>"><?= $name; ?></option>
        <?php endforeach; ?>
</select>
is this how the form code should look?
thanks a lot for helping
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

I wouldn't define the states array there, no. Put it somewhere that it's going to be accessible and reusable.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

so your saying use include so i can easily edit them?
i have the code set up like i just posted now and the form doesn't list anything under the option field only ---
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

What version of PHP? Short array syntax is only supported after 5.4. Also, you're using a short opening tag, which you shouldn't.

Code: Select all

<?
$states = [
    'NY' => 'NEW YORK',
    'FL' => 'FLORIDA',
];
?> 
should be

Code: Select all

<?php
$states = [
    'NY' => 'NEW YORK',
    'FL' => 'FLORIDA',
];
?> 
Finally, check that you have error reporting turned on.
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:so your saying use include so i can easily edit them?
Depends on your application architecture. Maybe it makes sense in a global config, maybe it's model-specific.
donny
Forum Contributor
Posts: 179
Joined: Mon Aug 11, 2014 11:18 am

Re: PHP IF code

Post by donny »

I'm using php 5.2
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 hasn't been supported in three years. http://php.net/eol.php
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP IF code

Post by Celauran »

If you absolutely must use 5.2, you'll need to use the following syntax:

Code: Select all

$states = array(
	'CA' => 'California',
	'FL' => 'Florida',
	'NY' => 'New York',
);
Post Reply