unixbro Posted March 6, 2008 Share Posted March 6, 2008 <?php $i = 1; while ($i <= 100) { //Execute the statements below if this is TRUE echo $i; //Print the value of $i echo "<OPTION>$i</OPTION>"; //print a space $i++; //Increment value of $i by 1 } ?> the above is supposed to print a select box with options from 1 to 100 My problem is when it reaches 50 I want it to print <OPTION selected>50</OPTION> all other variables I would like to keep as <OPTION>$i</OPTION> where $i will be 1 to 100 not sure if this makes sense. Thanks in advance Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 6, 2008 Share Posted March 6, 2008 I am not a php expert but this should be it shouldnt it? <?php $i = 1; while ($i <= 100) { //Execute the statements below if this is TRUE echo $i; //Print the value of $i if ($i == 50) { echo "<OPTION selected>50</OPTION>"; } else { echo "<OPTION>$i</OPTION>"; //print a space } $i++; //Increment value of $i by 1 } ?> Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 Another way to crack that nut. <?php // In case something in your code changes this $to_select = 50; for ($i = 1; $i <= 100; $i++) { echo $i; //Print the value of $i $selected = $i == $to_select ? ' selected="selected"' : ''; echo "<option{$selected}>{$i}</option>"; } ?> Quote Link to comment Share on other sites More sharing options...
unixbro Posted March 6, 2008 Author Share Posted March 6, 2008 none if the selections above are printing anything dmcabes printed 2 selections 50 and $i more learning involved Quote Link to comment Share on other sites More sharing options...
fnairb Posted March 6, 2008 Share Posted March 6, 2008 I ran this before I posted it and it spit out all the <option> tags. Did the code in the original post work with the exception of setting 50 to selected? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted March 6, 2008 Share Posted March 6, 2008 Not sure what you are meaning in your last reply, but I just tested this: <?php echo "<select name=\"test\">"; $i = 1; while ($i <= 100) { //Execute the statements below if this is TRUE echo "The value is: ".$i."<br />"; //Print the value of $i if ($i == 50) { echo "<OPTION selected>50</OPTION>"; } else { echo "<OPTION>$i</OPTION>"; //print a space } $i++; //Increment value of $i by 1 } echo "</select>"; ?> And it worked perfectly, obviously it does not do the "The value is X" line as it is in the middle of a select box, so has no place to echo that line to, but it did create a select box with 100 options and the 50 as the default selected option. Quote Link to comment Share on other sites More sharing options...
unixbro Posted March 6, 2008 Author Share Posted March 6, 2008 I ran this before I posted it and it spit out all the <option> tags. Did the code in the original post work with the exception of setting 50 to selected? No I guess I posted to early ... my bad ... haste makes waste Quote Link to comment Share on other sites More sharing options...
unixbro Posted March 6, 2008 Author Share Posted March 6, 2008 MY BAD ... sorry all it's working now ... dang fat fingers ... Thanks to those who helped Quote Link to comment 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.