Failing_Solutions Posted June 24, 2011 Share Posted June 24, 2011 I'm trying to set the select to whatever was posted. In this case I'm using dates. I think I may have my loops wrong. On every post I get the select set to the first date in my date array ($options) I've tried putting the loops in different places but can't get it right. Any help is appreciated. <?php //Getting the Dates $date_query="Select Game_Date from june122011 GROUP BY Game_Date DESC"; $date_results=mysql_query($date_query); $options=""; //setting the select as post $x=""; // check if Post Select is set and set x to post if (isset($_POST['Select']) AND strlen($_POST['Select']>0)) { $x=$_POST['Select']; } //loop through dates while ($row=mysql_fetch_array($date_results)) { $thedays=$row["Game_Date"]; //if date = the post set it as selected if ($x==$thedays) { $x='selected="selected"'; } $options.="<OPTION VALUE=\"$thedays\" $x>".$thedays.'</option>'; } // end getting dates ?> Then the html part: <select name="Select" id="Select" class="select"> <?php echo $options ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/240339-set-selectbox-index-as-post-on-submit-with-php/ Share on other sites More sharing options...
Pikachu2000 Posted June 24, 2011 Share Posted June 24, 2011 Comment out this for the moment: if ($x==$thedays) { $x='selected="selected"'; And put this in it's place: $x = ( !empty($_POST['Select']) && $_POST['Select'] == $thedays ) ? 'selected="selected"' : ''; Should work, unless I've misread something . . . Quote Link to comment https://forums.phpfreaks.com/topic/240339-set-selectbox-index-as-post-on-submit-with-php/#findComment-1234535 Share on other sites More sharing options...
Failing_Solutions Posted June 25, 2011 Author Share Posted June 25, 2011 That certainly did fix it. Thank you very much for that. I don't want to impose but I'm learning PHP as I go, mostly I just keep trying stuff till it works. Can you explain what your code is doing there at the end? I understand the first part just checking if not empty and the values match, but the end part with the ? and : I'm not familiar with sort of functionality. Quote Link to comment https://forums.phpfreaks.com/topic/240339-set-selectbox-index-as-post-on-submit-with-php/#findComment-1234538 Share on other sites More sharing options...
Pikachu2000 Posted June 25, 2011 Share Posted June 25, 2011 That's called ternary syntax, it's basically an abbreviated if/else conditional. It does the same as: if( !empty($_POST['Select']) && $_POST['Select'] == $thedays ) { $x = 'selected="selected"'; } else { $x = ''; See example #2 and #3 HERE for more info. Quote Link to comment https://forums.phpfreaks.com/topic/240339-set-selectbox-index-as-post-on-submit-with-php/#findComment-1234550 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.