jkatcherny Posted December 9, 2009 Share Posted December 9, 2009 Hey all, I'm using the code below to try to do 2 things 1 - populate data from a database, which is working fine 2 - choose the default selected value based on a session variable $_SESSION['requiredcomposerid'] - this is where the hang up is. I added in some code to the $compOptions variable and now I'm stuck. Anyone have any thoughts? Thank you. J - - - - - $compSql="SELECT composerid, compFirst, compLast FROM Composer_Info"; $compResult=mysql_query($compSql); $compOptions=""; while ($row=mysql_fetch_array($compResult)) { $composerid=$row['composerid']; $compFirst=$row['compFirst']; $compLast=$row['compLast']; $compOptions.="<OPTION VALUE='$composerid'".if($_SESSION['requiredcomposerid'] == $composerid)."selected = 'selected'>".$compFirst." ".$compLast; // before I added the 'if' statement this was fine, here is where I'm trying to get the selected to work within a session } Link to comment https://forums.phpfreaks.com/topic/184525-selected-variable-value-from-populated-drop-down/ Share on other sites More sharing options...
JAY6390 Posted December 9, 2009 Share Posted December 9, 2009 have you used session_start() ? also, your html is poor in the last line. change it to the following $compSql = "SELECT composerid, compFirst, compLast FROM Composer_Info"; $compResult = mysql_query($compSql); $compOptions = ""; while ($row = mysql_fetch_array($compResult)) { $composerid = $row['composerid']; $compFirst = $row['compFirst']; $compLast = $row['compLast']; $selected = ($_SESSION['requiredcomposerid'] == $composerid) ? ' selected="selected"' : ''; $compOptions .= '<option value="'.$composerid.'"'.$selected.'>'.$compFirst.' '.$compLast.'>'; } Link to comment https://forums.phpfreaks.com/topic/184525-selected-variable-value-from-populated-drop-down/#findComment-974140 Share on other sites More sharing options...
jkatcherny Posted December 9, 2009 Author Share Posted December 9, 2009 Hey Jay, thank you. Yes, I do have session_start() - sorry I didn't include all the code. What I had was this: session_start(); include 'config.php'; include 'opendb.php'; // Populates COMPOSER list $compSql="SELECT composerid, compFirst, compLast FROM Composer_Info"; $compResult=mysql_query($compSql); $compOptions=""; while ($row=mysql_fetch_array($compResult)) { $composerid=$row['composerid']; $compFirst=$row['compFirst']; $compLast=$row['compLast']; $compOptions.="<OPTION VALUE='$composerid'>".$compFirst." ".$compLast; } That populated the drop down menu based on what I had in my database, which works great. Now what I'm trying to do is, if the user needs to go back and change something (for which I already have a page coded), I want whatever they chose from the drop down menu previously, to be selected when they go back to make their changes. This is what I was trying to achieve by the 'if' statement I had but I'm not sure of the exact syntax. Does that make any sense? Thank you again for you help! J Link to comment https://forums.phpfreaks.com/topic/184525-selected-variable-value-from-populated-drop-down/#findComment-974161 Share on other sites More sharing options...
jkatcherny Posted December 9, 2009 Author Share Posted December 9, 2009 Sorry Jay, that actually worked great! Thank you. Link to comment https://forums.phpfreaks.com/topic/184525-selected-variable-value-from-populated-drop-down/#findComment-974164 Share on other sites More sharing options...
JAY6390 Posted December 9, 2009 Share Posted December 9, 2009 Cool, no problem Link to comment https://forums.phpfreaks.com/topic/184525-selected-variable-value-from-populated-drop-down/#findComment-974173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.