Jumpy09 Posted May 12, 2010 Share Posted May 12, 2010 First, to my knowledge this is a code thing. It's all php based, the sql side is ready just needs a particular place to go. Function: function dropdown( $name, array $options, $selected=null ) { /*** begin the select ***/ $dropdown = '<select name="'.$name.'" id="'.$name.'">'."\n"; $selected = $selected; /*** loop over the options ***/ foreach( $options as $key=>$option ) { /*** assign a selected value ***/ $select = $selected==$key ? ' selected' : null; /*** add each option to the dropdown ***/ $dropdown .= '<option value="'.$option.'"'.$select.'>'.$option.'</option>'."\n"; } /*** close the select ***/ $dropdown .= '</select>'."\n"; /*** and return the completed dropdown ***/ return $dropdown; } Actual Use of Said Function: <tr><td>Smoke:</td><td><?php $name = 'smoke'; $options = array( 'Prefer not to say', 'Yes', 'No' ); $selected = 0; echo dropdown( $name, $options, $selected ); ?></td></tr> Since the array displays to the $selected part as 0 - 2 I can't put Selected = $databaseitem. Prefer not to say = 0 Yes = 1 No = 2 How can I alter the Selected zone to choose words instead of numbers? So I could for instance put $selected = "Prefer Not To Say"; Any ideas? Yesterday or the Day before, I thought I had the Answer! Today I realized I didn't. Without the function I can add an option above where it would pull the options by the SQL Database. For example: <tr><td>Topic:</td><td><select name="topic"><option value="<?php echo $topic3; ?>"><? echo $topic3; ?></option> <?php $q = "SELECT topicid, topicname FROM news_topics ORDER BY topicname"; $results = mysql_query($q); while($row = mysql_fetch_array($results)) { echo "<option value=\"".$row['topicid']."\">".$row['topicname']."\n "; } ?></select></td></tr> That unfortunately shows two of the same option, but it works! If anyone has any fixes to make it smooth, I wouldn't mind em. Link to comment https://forums.phpfreaks.com/topic/201442-drop-down-select-box-setting-on-the-right-selection-from-mysql/ Share on other sites More sharing options...
Jumpy09 Posted May 12, 2010 Author Share Posted May 12, 2010 <tr><td>Smoke:</td><td><?php $name = 'smoke'; $options = array( "$smoke", 'Prefer not to say', 'Yes', 'No' ); $selected = 0; echo dropdown( $name, $options, $selected ); ?></td></tr> Just In case anyone wanted to know the answer! I just did what I did for the other one, seems like the only real solution, at least according to easiest method. Link to comment https://forums.phpfreaks.com/topic/201442-drop-down-select-box-setting-on-the-right-selection-from-mysql/#findComment-1057010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.