Page 1 of 2
PHP IF code
Posted: Mon Aug 11, 2014 12:19 pm
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!
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:23 pm
by Celauran
How are you handling the abbreviations? What if they enter something outside of the US? What if they spell it wrong?
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:36 pm
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
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:40 pm
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.
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:43 pm
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.
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:47 pm
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
Re: PHP IF code
Posted: Mon Aug 11, 2014 12:50 pm
by Celauran
array_search()
Or use the array keys as your form values as I posted above.
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:01 pm
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
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:03 pm
by Celauran
I wouldn't define the states array there, no. Put it somewhere that it's going to be accessible and reusable.
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:04 pm
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 ---
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:07 pm
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.
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:08 pm
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.
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:09 pm
by donny
I'm using php 5.2
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:13 pm
by Celauran
That hasn't been supported in three years.
http://php.net/eol.php
Re: PHP IF code
Posted: Mon Aug 11, 2014 1:14 pm
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',
);