Jump to content

Drop down menu and database values


ReeceSayer

Recommended Posts

Hi all,

 

I'm just wondering if there's an easier way of doing what accomplishing the following:

 

I have a value in my database which represents a selection in a drop down menu, i want to read it from the database and have it automatically selected depending on the stored data.

 

I have the following working but just wondered if there was an easier way to get the same result:

 

<?php
//database connection

$query = "SELECT id
		  FROM	 `tablename`
		  WHERE	 username='$username'";
$result = mysql_query($query);
$row = mysql_fetch_object($result);

$id = $row->id;
$id = (int)$id;
?>

<select name="id">
			<option value="">Select your option...</option>
			<option value="1" <?php if (($id - 1) === 0) { echo 'selected="selected"'; }?>>Selection 1</option>
			<option value="2"<?php if (($id - 2) === 0) { echo 'selected="selected"'; }?>>Selection 2</option>
</select>

 

Sorry if it's not very clear, i'll explain best i can if anyone can help.

 

Thanks

Link to comment
Share on other sites

You could store your option value/text pairs in an array, and then loop the array to build the list of <option> tags. Example:

 

$options = array(1=> 'Option1', 'Option2', 'Option3', 'Option4');

echo <select name=\"field_name\">\n";
foreach( $options as $k => $v ) {
     $selected = $id == $k ? 'selected="selected"' : '';
     echo "<option value=\"k\" $selected>" . htmlentities($v) . "</option>\n";
}
echo "</select>\n";

Link to comment
Share on other sites

Thats a real bad example there.

 

1) is your selection boxes always hardcoded, and you are selecting if from a value?

Learn to use an array, and loops

$opts=array(
  array('','Select your option...'),
  array('1','Selection 1'),
  array('2','Selection 2'));
echo '<select name="id">'.PHP_EOL;
foreach($opts as $opt)
  echo '  <option value="'. $opt[0] .'"'.  ($id==$opt[0]?$' selected':'') .'>'. $opt[1] .'</option>'.PHP_EOL;
echo '</select>'.PHP_EOL;

Link to comment
Share on other sites

Hi,

 

Thanks for the quick replies. I thought it was bad which is why i thought i'd post on here to find a better way of doing it but it's the only immediate way i could think of getting the results.

 

I've just had a quick look and it looks like both of your solutions would work.

 

Also yes the dropdown is always hardcoded.

 

I'll try and implement your solutions and see which works best.

 

Thanks a lot!

Link to comment
Share on other sites

They both are relatively the same.

Concept use an array to build the option list, Pikachu uses the keys as identifiers I went as an all array solution.

the selection selector is also the same, just placement is different

about only thing is there is a bug in pikachu's code, actually a typo

"<option value=\"k\"

should be

"<option value=\"$k\"

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.