Page 1 of 1

[SOLVED] Pulling data in fields & editing them

Posted: Sun Aug 28, 2005 4:11 pm
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.

Posted: Sun Aug 28, 2005 4:22 pm
by feyd
you'll have to peek through the options while outputing them to set "selected" on the one matching the record.

Posted: Sun Aug 28, 2005 4:53 pm
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.

Posted: Sun Aug 28, 2005 5:12 pm
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";
}
:)

Posted: Sun Aug 28, 2005 5:47 pm
by jescobar
seems logical but I'm rather new so never thought of that, thanks I will update on the outcome

Posted: Sun Aug 28, 2005 6:26 pm
by jayshields
yep what those guys said should work fine. gl.

Posted: Mon Aug 29, 2005 3:35 am
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.

Posted: Mon Aug 29, 2005 3:55 am
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!!!

Posted: Mon Aug 29, 2005 4:10 am
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";
}