SunQuest
 
           PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old August 5th, 2004, 09:32 AM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old August 5th, 2004, 09:43 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
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:
 $result mysql_fetch_array(mysql_query("SELECT name FROM subincsource where id=".$_POST['INCOME1N'])
$INCOME1N=$result['name']; 

Reply With Quote
  #3  
Old August 5th, 2004, 01:49 PM
Scot Scot is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 29 Scot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 17 sec
Reputation Power: 0
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>

Reply With Quote
  #4  
Old August 5th, 2004, 02:48 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
I'd say, replace the if statement on line 8 with the following, build forth on my previous snippet:

PHP Code:
if($array['id'] == $_POST['INCOME1N'] ) {
print 
"<option selected value=$array[id]>$array[name]</option>";
} else {
print 
"<option value=$array[id]>$array[name]</option>"


Pretty much the same trick, but can be I'm missing the whole point here.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Extracting Name instead of associated number


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway