5 Replies - 66237 Views - Last Post: 27 February 2012 - 01:55 PM Rate Topic: -----

#1 strykerhorse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 26-October 05

setting a select's selected value

Posted 15 February 2006 - 12:58 AM

Good evening to all...

How would I go about setting a select's value to a value in a database? the current way I am accomplishing it is shown below and is VERY inefficient:


<?
/*this function creates a select with a selected value based on the value in the database*/

function selector_s1($ro_type)//$ro_type is the value pulled from the respective cell based on the user's previously input value
{
  switch($ro_type)
  {
	case 'Industrial Pharmaceutical':

  echo "\n<select name=\"select\" size=\"1\">\n\n";

  echo "<option value=\"Industrial Pharmaceutical\" selected=\"selected\">Industrial Pharmaceutical</option>\n";

            echo "<option value=\"Semiconductor\">Semiconductor</option>\n";

            echo "<option value=\"Municipal\">Municipal</option>\n";

            echo "<option value=\"Beverage\">Beverage</option>\n";

echo "<option value=\"Other\">Other</option>\n";

     echo"</select></td>\n";
          echo "</tr>\n\n";
    break;
	
//etc, etc for the other options of the <select>

  }
}
?>




the outputted HTML:

<select name="select" size="1">

<option value="Industrial Pharmaceutical" selected="selected">Industrial Pharmaceutical</option>
<option value="Semiconductor">Semiconductor</option>
<option value="Municipal">Municipal</option>
<option value="Beverage">Beverage</option>
<option value="Other">Other</option>
</select></td>
</tr>



Any ideas?

thanks for anything,
_NLC

Is This A Good Question/Topic? 0
  • +

Replies To: setting a select's selected value

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: setting a select's selected value

Posted 15 February 2006 - 02:34 AM

What you could do is have each option be an element in an array and then loop through it. If the option is the one desired to have the "selected="selected" then add it.

I didn't include the rest of the html/php stuff. I'll leave that to you to figure out how to integrate it into your code.

$desired_option = 'Semiconductor';
$arr = array('Semiconductor', 'etc', 'and', 'so', 'on');
for($i = 0; $i < count($arr); $i++) {
   $selected = ($arr[$i] == $desired_option) ? 'selected="selected"' : '';
   echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>";
}


Was This Post Helpful? 1

#3 strykerhorse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 26-October 05

Re: setting a select's selected value

Posted 15 February 2006 - 02:57 PM

with a little bit of tweaking, the code works perfectly.

<?
//selector_s4_dis: dynamically selects a value in a select according to what's already in the DB

function selector_s4_dis($dis)
{
	$arr = array('None', 'Free Chlorine', 'Chloramine', 'Chlorine Dioxide', 'Ozone', 'Other (please specify)');
	for($i = 0; $i < count($arr); $i++) 
  {
 	 $selected = ($arr[$i] == $dis) ? 'selected="selected"' : '';
 	 echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>\n";
  }
  //return;
}
  
?>


Thanks, hotsnoj!

_NLC
Was This Post Helpful? 0
  • +
  • -

#4 HerofTime  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 29-July 09

Re: setting a select's selected value

Posted 17 September 2009 - 01:18 AM

Beneficial. Thank you.
Was This Post Helpful? 0
  • +
  • -

#5 iamberryboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-February 12

Re: setting a select's selected value

Posted 27 February 2012 - 10:18 AM

Hey guys,
This works for me and was very helpful, but can someone explain more of the code? I've tried looking around the manual but have come up empty handed!
Thanks,
Kevin
Was This Post Helpful? 0
  • +
  • -

#6 strykerhorse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 26-October 05

Re: setting a select's selected value

Posted 27 February 2012 - 01:55 PM

View Postiamberryboy, on 27 February 2012 - 11:18 AM, said:

Hey guys,
This works for me and was very helpful, but can someone explain more of the code? I've tried looking around the manual but have come up empty handed!
Thanks,
Kevin


Gah, I remember the stressful day I started this thread. Glad that's over and done with!

Take a look here for an explanation and more info about ternary operators...this should help a bit if you're having questions.

Peace!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1