|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Extracting Name instead of associated number
The answer I am looking for is probably right under my nose, but I have gotten some good advice from this forum so I thought I would pick your collective brains. The problem.....
I have a form which calculates income. The form asks the user to select the type of income from a select list. Each income type in the list is associated with a corresponding id like so....... <select name='INCOME1N'> <?php $result = mysql_fetch_array(mysql_query("SELECT INCOME1N FROM _eligibility WHERE CLIENTNUM = '$CLIENTNUM'")); $user_val = $result['INCOME1N']; $query = mysql_query("SELECT * FROM subincsource"); while($array = mysql_fetch_array($query)) { if($array['id'] == $user_val) { print "<option selected value=$array[id]>$array[name]</option>"; } else { print "<option value=$array[id]>$array[name]</option>"; } } ?> </select> The names of the income types and their associated number are stored in a MySQL database. When the user clicks the Calculate button on the form I want the form to calculate totals and redisplay the item selected by the user in the list using Post to restore those values. Like so.............. switch ($_POST['submit']) { // if calculate => add case 'Calculate': $ASSETT=($_POST['ASSET1A']+$_POST['ASSET2A']+$_POST['ASSET3A']+$_POST['ASSET4A']+$_POST['ASSET5A']); $INCOMET = (($_POST['INCOME1A']*(52))+($_POST['INCOME2A']*(52))+($_POST['INCOME3A']*(52))+($_POST['INCOME4A']*(52))+($_POST['INCOME5A']*(52))+($_POST['INCOME6A']*(52))+($_POST['INCOME1AM']*(12))+($_POST['INCOME2AM']*(12))+($_POST['INCOME3AM']*(12))+($_POST['INCOME4AM']*(12))+($_POST['INCOME5AM']*(12))+($_POST['INCOME6AM']*(12))+$_POST['INCOME1AY']+$_POST['INCOME2AY']+$_POST['INCOME3AY']+$_POST['INCOME4AY']+$_POST['INCOME5AY']+$_POST['INCOME6AY']); $INCOME1A=$_POST['INCOME1A']; $INCOME2A=$_POST['INCOME2A']; $INCOME3A=$_POST['INCOME3A']; $INCOME4A=$_POST['INCOME4A']; $INCOME5A=$_POST['INCOME5A']; $INCOME6A=$_POST['INCOME6A']; $INCOME1AM=$_POST['INCOME1AM']; $INCOME2AM=$_POST['INCOME2AM']; $INCOME3AM=$_POST['INCOME3AM']; $INCOME4AM=$_POST['INCOME4AM']; $INCOME5AM=$_POST['INCOME5AM']; $INCOME6AM=$_POST['INCOME6AM']; $INCOME1AY=$_POST['INCOME1AY']; $INCOME2AY=$_POST['INCOME2AY']; $INCOME3AY=$_POST['INCOME3AY']; $INCOME4AY=$_POST['INCOME4AY']; $INCOME5AY=$_POST['INCOME5AY']; $INCOME6AY=$_POST['INCOME6AY']; $ASSET1A=$_POST['ASSET1A']; $ASSET2A=$_POST['ASSET2A']; $ASSET3A=$_POST['ASSET3A']; $ASSET4A=$_POST['ASSET4A']; $ASSET5A=$_POST['ASSET5A']; $INCOME1N=$_POST['INCOME1N']; echo $INCOME1N; ////////////////////////////end of code//////////////////////// Echo $INCOME1N returns the value associated with the type of income not the name itself. If user selected Employment as their choice and the id of Employment is "6" echo $INCOME1N above returns 6! My problem is that the select box values returned are the corresponding numbers not the names of the items selected therefore the user's selections do not repost to the form. Probably my question is clear as mud huh! I know the solution is simple but I have stared at it too long to be objective. Any and all responses are greatly appreciated. Have an Awesome Day! |
|
#2
|
||||
|
||||
|
Not really clear what your problem is, but if it's that the select box returns an Id instead of the namestring, you should alter the <option> in the HTML form, so that the item in the 'value' field is identical to the text between <option> and </option>.
An alternative I prefer (more secure) is to read the array again on the next page, with the indexes from the form, like this: PHP Code:
|
|
#3
|
|||
|
|||
|
Worked Great But!
Itsacon
Thanks so much for the snippet. It works great. One little snag though. I can't figure out how to get the value back into my select box to display to the user which selection they made before posting the form. Any help would be appreciated. My select box code is as follows............. <select name='INCOME1N'> <?php $result = mysql_fetch_array(mysql_query("SELECT INCOME1N FROM _eligibility WHERE CLIENTNUM = '$CLIENTNUM'")); $user_val = $result['INCOME1N']; $query = mysql_query("SELECT * FROM subincsource"); while($array = mysql_fetch_array($query)) { if($array['id'] == $user_val ) { print "<option selected value=$array[id]>$array[name]</option>"; } else { print "<option value=$array[id]>$array[name]</option>"; } } ?> </select> |
|
#4
|
||||
|
||||
|
I'd say, replace the if statement on line 8 with the following, build forth on my previous snippet:
PHP Code:
Pretty much the same trick, but can be I'm missing the whole point here. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Extracting Name instead of associated number |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|