Jump to content

Setting Select boxes After creation


Valrus

Recommended Posts

Hi, I'm looking for a method in which I can set the selected option of drop-down select box (a <Select><Option> group) through scripting. I don't see any way to do it, maybe I'm just missing something.

 

What I'm trying to accomplish is I'm pulling a bunch of data from a database and then populating a page for editing. Which is relatively easy except part of the data is represented by option groups in select boxes which I would need to set based on the value of the pulled data.

 

Thanks for any help you can give me.

Link to comment
https://forums.phpfreaks.com/topic/151794-setting-select-boxes-after-creation/
Share on other sites

Here is a script I use, hopefully this is what you are talking about...

 

<?php
//Database query and putting data into an array
$query = mysql_query("SELECT * FROM whatever");
while ($dbarray = mysql_fetch_assoc($query)) {
    $whatever[] = $dbarray['whatever'];
}
?>
//then put this in your html form to dispay the data for Select boxes

<?php echo '<SELECT name=whatever>';
foreach ($whatever as $key => $value) {
    echo '<OPTION value=' . $value . '> ' . $value . '';
}
echo '</select>'; ?>

here is a basic example.  here i am comparing the array value to a database entry. you can also compare to a $_POST['name'] form entry

 

<?php
$array = ('Tom','Jack','John','Steve');
echo "<SELECT name=\"test\">";
$row = mysql_fetch_array($query)
foreach($array as $name) {
   if($name == $row['name']) {
      echo "<OPTION selected=\"selected\">".$name."</OPTION>";
   }
   else {
      echo "<OPTION>".$name."<OPTION>";
   }
}
?>

Thank you, but that's not quite what I'm hoping to achieve. Perhaps I can illustrate what I'd like to do.

 


<HTML>
<?php
selectionFunction() {
         Some code here to select the proper selection option.
}
?>
		<select style="formSelect" name="ranking" id="ranking">
      			<optgroup label="Ranking" />
        			<option value="" />
				<OPTION VALUE="1">1 - First Choice</OPTION>
				<OPTION VALUE="2">2</OPTION>
				<OPTION VALUE="3">3</OPTION>
				<OPTION VALUE="4">4</OPTION>
				<OPTION VALUE="5">5</OPTION>
				<OPTION VALUE="6">6</OPTION>
				<OPTION VALUE="7">7</OPTION>
				<OPTION VALUE="8">8</OPTION>
				<OPTION VALUE="9">9</OPTION>
				<OPTION VALUE="10">10</OPTION>
				<OPTION VALUE="-1">Banned</OPTION>
      		</select>
<?php
selectionFunction();
?>
</HTML>

 

Obviously stripped down a bit, but hopefully it illustrates what I want. The reason I want to do this is so I can re-use a whole bunch of code for add/edit/view pages for this data. Otherwise I'm gonna have to re-do a whole bunch of stuff.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.