ngreenwood6 Posted December 9, 2008 Share Posted December 9, 2008 I have the following code: <tr> <td>School 1</td> <td>:</td> <td> <select name="school_1"> <?php //school_query $school_query = "SELECT * FROM schools"; //get the results $school_results = mysql_query($school_query); while($school_row = mysql_fetch_array($school_results)) { ?> <option selected="<?php echo $row['school']; ?>" value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } ?> </select> </td> </tr> the $row['school'] comes from another query. The problem with this is that it doesn't show the selected option that is in the database. It is just displaying the last value. $row['school'] has a value because I echo'd it. Any help is appreciated Link to comment https://forums.phpfreaks.com/topic/136214-solved-while-help/ Share on other sites More sharing options...
tivrfoa Posted December 9, 2008 Share Posted December 9, 2008 I have the following code: <tr> <td>School 1</td> <td>:</td> <td> <select name="school_1"> <?php //school_query $school_query = "SELECT * FROM schools"; //get the results $school_results = mysql_query($school_query); while($school_row = mysql_fetch_array($school_results)) { ?> <option selected="<?php echo $row['school']; ?>" value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } ?> </select> </td> </tr> the $row['school'] comes from another query. The problem with this is that it doesn't show the selected option that is in the database. It is just displaying the last value. $row['school'] has a value because I echo'd it. Any help is appreciated try this: <option value="<?php echo $row['school']; ?>"><?php echo $row['school']; ?></option> <? while($school_row = mysql_fetch_array($school_results)) { if($row['school'] != $school_row['school']) { ?> <option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710540 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 try this: <option value="<?php echo $row['school']; ?>" selected><?php echo $row['school']; ?></option> <? while($school_row = mysql_fetch_array($school_results)) { if($row['school'] != $school_row['school']) { ?> <option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } } ?> Use code tags next time please. <option value="<?php echo $row['school']; ?>" selected><?php echo $row['school']; ?></option> <?php while($school_row = mysql_fetch_array($school_results)) { if($row['school'] != $school_row['school']) { ?> <option value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } } ?> Link to comment https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710551 Share on other sites More sharing options...
Jibberish Posted December 9, 2008 Share Posted December 9, 2008 If you want the option selected when $row['school'] == $school_row['school'] (I think thats what you asking it to do, ignore me though if its not ) Then try <tr> <td>School 1</td> <td>:</td> <td> <select name="school_1"> <?php //school_query $school_query = "SELECT * FROM schools"; //get the results $school_results = mysql_query($school_query); while($school_row = mysql_fetch_array($school_results)) { ?> <option <?php if( $school_row['school'] == $row['school']) echo "selected" ?> value="<?php echo $school_row['school']; ?>"><?php echo $school_row['school']; ?></option> <?php } ?> </select> </td> </tr> Link to comment https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710554 Share on other sites More sharing options...
ngreenwood6 Posted December 9, 2008 Author Share Posted December 9, 2008 Thank you jibberish you hit what I was looking for. Link to comment https://forums.phpfreaks.com/topic/136214-solved-while-help/#findComment-710561 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.