Jump to content

Set a form dropdown list 'selected' item to the value in a database


fj1200

Recommended Posts

Hi - I am building a form to update records in a database but struggling a bit with one secondary but vitally important area.  I wrote the data entry for using drop-down lists where the value is not the same as the list entry - for example, I have material widths where they are represented in the database as: 1/2 = 0, 3/4 = 1 and full width = 2.  There's 10 of these.  I have a machine number where A = 101, B=102, D=103, E=104 - this is how they are represented in the manufacturers DB and I'm following their naming for continuity and reporting. 

 

I want to update these on the fly, so if, for example machine D is entered in the database, how do I set the "selected = ..." value in the drop-down on the form to reflect the database entry?

 

eg.

<Select>
<option  Value = \"101\">A</option>
<option Value = \"201\">B</option>
<option selected = \"selected\" Value = \"301\">D</option>  <--- like this...
<option  Value = \"401\">E</option>
</select>

 

I only need to update maybe a few items - maybe we change the scheduled time, or put a job on a different machine, or the order changes - that kind of thing.  I don't want them to have to re-enter the whole job so there's a fair number of these to do on the update page.

 

Been tearing my hair out and could do with some advice.

Link to comment
Share on other sites

OK - I've found a way to do it using arrays from another site (lost the link now) but it's a bit long-winded:

 

 

  $MACHINE= $pbupdate[4];
		$MODE = $pbupdate[5];

		$_machine = array(
		101=> "A",
		102=> "B",
		103=> "D",
		104=> "E",
		);

		If ($MACHINE == "101")  $PRESS = ("A");				
		If ($MACHINE == "201")  $PRESS = ("B");
		If ($MACHINE == "301")  $PRESS = ("D");
		If ($MACHINE == "401")  $PRESS = ("E");

	echo "<tr><td>Machine :</td><td>$PRESS</td><td>";

		echo "<SELECT name=fldr>";
		foreach ($_machine as $key => $value)
		{
		$SELECTED = "";		
		If ($value == $PRESS)  $SELECTED = ("SELECTED");
		echo '<OPTION '.$SELECTED.' value="'.$key.'">'.$value.'</option>';
		}
	echo "</select></td></tr>";

 

It's a bit laborious for one field but it works, the only problem I have with it is I have 18 fields to update from drop-down boxes and 4 text fields.  Going to be a long file.

 

If anyone has a simpler or more elegant solution I'd be glad to hear it.

Link to comment
Share on other sites

$types = array('A' => 101, 'B' => 102, 'D' => 103, 'E' => 104);
foreach ($types as $type => $code) {
   echo '<option value="$code"';
   if ($MACHINE == $code) {
      echo ' selected="selected"';
   }
   echo '>'.$type.'</option>';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.