[SOLVED] Pulling data in fields & editing them

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

Post Reply
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

[SOLVED] Pulling data in fields & editing them

Post by jescobar »

I am making a simple form and for some reason when I click to "Edit" a record it displays all the information previously entered in the "text" fields but it does not pull the ones for the drop down boxes.

All information flows very good as far as adding/deleting/editing but if you want to edit the records you have to remember what the previous values were, which is quite important because we use the system to upgrade meaning we have to know what you had so we can give you the next step up.

btw this is being done with php & MySQL.

Thanks in advance!
J.


###########
UPDATE

I am using the

Code: Select all

value="<?php echo $name ?>"
method which like I said earlier works fine for the text fields but not the drop downs.

ty, J.
Last edited by jescobar on Mon Aug 29, 2005 3:56 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you'll have to peek through the options while outputing them to set "selected" on the one matching the record.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

WOW that was quick, well thanks for the reply, so should I just add the value to the options? like...

Code: Select all

<option value="<?php echo $name ?>" selected><?php echo $name ?></option>
Once again, thanks alot
J.
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

You would want to do something like:

Code: Select all

foreach($options as $option) {
    if($option == $value) 
          echo "<option value=\"$option\" selected>$option\n";
    else 
          echo "<option value=\"$option\">$option\n";
}
:)
Last edited by Stewsburntmonkey on Sun Aug 28, 2005 6:56 pm, edited 1 time in total.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

seems logical but I'm rather new so never thought of that, thanks I will update on the outcome
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

yep what those guys said should work fine. gl.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

I am encountering some problems, the code looks like it would pull all the information from the database right? I need something to bring the current value from the database but to also give the remainder options from the code.

something like

Code: Select all

<option value="$current_value_on_database" selected>$current_value_on_database</option> 

 (Note: then values not on the database)

<option value="value">value</option>
<option value="value">value</option>
<option value="value">value</option>
did that explain this better?

Thanks for all the help every1
J.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

Well I must say after hours of trying it was right in front of my eyes all this time.

Aparently the suggestion I made above did the trick when it comes to populating just that one field. Thanks alot fellas!!!
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

This one is more generic and can be applied to any option set you have...
(A little code snippet from my lib adapted to your code)

Suppose you have a set of options of which one is stored in db. So you will code it like this

Code: Select all

//keys are put as value attribute, value part is put as text of option
$Opts = array(1=>"One", 2=>"Two", 3=>"Three", 4=>"Four", 5=>"Five");

//Output the combo-box with given options...
print "<select name='opts'>";
writeOpts($Opts, $selOpt); //$selOpt is the selected option that you ertrieve from db
print "</select>";

///////////////////////
//$aOpts is an array of Options
//$sSelect is the key of selected option

function writeOpts($aOpts, $sSelect = '')
{
  echo "<option value=''".($sSelect==''?' selected':'').">-Select-</option>\r\n"; //comment this if you dont want a default -select- option
  foreach ($aOpts as $val=>$text)
    echo "<option value=''{$val}".($sSelect==$val?' selected':'').">{$text}</option>\r\n";
}
Post Reply