mpempas Posted June 4, 2009 Share Posted June 4, 2009 Hello, Ive found this code by google <select name="user_position" id="user_position" class='textbox'> <?php foreach($user_position as $key => $value) { if($_SESSION['user_position'] < $key) { $selected = ''; if($key == $row['user_position']) { $selected = ' selected="selected"'; } echo('<option value="'.$key.'"'.$selected.'>'.$value.'</option>'); } } ?> </select> </td> But i cant make it work, i want im going to edit a user profile to 'auto' select the existing choice i have on the database .. Any help please? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/ Share on other sites More sharing options...
taquitosensei Posted June 4, 2009 Share Posted June 4, 2009 There's a few things. You don't need the if($_SESSION['user_position'] < $key You don't need the parenthesis around your echo. And if you're going to concatenate the variables/ html for output your quotes aren't right. <select name="user_position" id="user_position" class='textbox'> <select name="user_position" id="user_position" class='textbox'> <?php foreach($user_position as $key => $value) { $selected = ''; if($key == $row['user_position']) { $selected = "selected='selected'"; echo "<option value='".$key."'".$selected.">".$value."</option>"; } } ?> </select> </td> Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849195 Share on other sites More sharing options...
mpempas Posted June 4, 2009 Author Share Posted June 4, 2009 $user_position = array ( 1 => 'test drop-down 1', 2 => 'test drop-down 2', 3 => 'test drop-down 3' ); Edided code: <select name="user_position" id="user_position" class='textbox'> <select name="user_position" id="user_position" class='textbox'> <?php foreach($user_position as $key => $value) { $selected = ''; if($key == $row['user_position']) { $selected = "selected='selected'"; echo "<option value='".$key."'".$selected.">".$value."</option>"; } } ?> </select> </td> If i use the above fixed code its not even has a drop down list.. Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849220 Share on other sites More sharing options...
taquitosensei Posted June 4, 2009 Share Posted June 4, 2009 Because I screwed up and had a bracket out of place try this foreach($user_position as $key => $value) { $selected = ''; if($key == $row['user_position']) { $selected = "selected='selected'"; } echo "<option value='".$key."'".$selected.">".$value."</option>"; } also that tells me that $key never equals $row['user_position'] otherwise you would have had a single option. The default one. Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849374 Share on other sites More sharing options...
mpempas Posted June 4, 2009 Author Share Posted June 4, 2009 again no luck .. its display the first in the list not the corrent is in the database Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849380 Share on other sites More sharing options...
taquitosensei Posted June 4, 2009 Share Posted June 4, 2009 try this. Copy and paste the results. <!--<select name="user_position" id="user_position" class='textbox'>--> <?php foreach($user_position as $key => $value) { echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>" $selected = ''; if($key == $row['user_position']) { $selected = ' selected="selected"'; } <!--echo '<option value='".$key."'".$selected.">".$value."</option>-->"; } ?> <!--</select>--> </td> Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849477 Share on other sites More sharing options...
mpempas Posted June 5, 2009 Author Share Posted June 5, 2009 Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' (line 52) <!--<select name="user_position" id="user_position" class='textbox'>--> <?php foreach($user_position as $key => $value) { echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>" $selected = ''; <-------------------------------- if($key == $row['user_position']) { $selected = ' selected="selected"'; } <!--echo '<option value='".$key."".$selected.">".$value."</option>-->"; } ?> <!--</select>--> </td> Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849953 Share on other sites More sharing options...
Hybride Posted June 5, 2009 Share Posted June 5, 2009 Change echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>" to echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>"; You were missing a semi-colon at the end. Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-849993 Share on other sites More sharing options...
mpempas Posted June 5, 2009 Author Share Posted June 5, 2009 Parse error: syntax error, unexpected '<' in (line 56) <!--<select name="user_position" id="user_position" class='textbox'>--> <?php foreach($user_position as $key => $value) { echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>"; $selected = ''; if($key == $row['user_position']) { $selected = ' selected="selected"'; } <!--echo '<option value='".$key."".$selected.">".$value."</option>-->"; <----------------------------- } ?> <!--</select>--> </td> Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-850007 Share on other sites More sharing options...
taquitosensei Posted June 5, 2009 Share Posted June 5, 2009 I guess I wasn't at the top of my game when I posted that. :-\ <?php foreach($user_position as $key => $value) { echo "key: ".$key.", value: ".$value.", user_position:".$row['user_position']."<br>"; $selected = ''; if($key == $row['user_position']) { $selected = ' selected="selected"'; } // echo '<option value='".$key."'".$selected.">".$value."</option>"; } ?> there was also a single apostrophe missing after $key in the same line. Quote Link to comment https://forums.phpfreaks.com/topic/160910-editing-drop-down-list-with-php-mysql/#findComment-850156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.