AdRock Posted October 18, 2008 Share Posted October 18, 2008 I have a form I use for editing a record and in there I have a select list. I want the select list to be filled with 3 values I want the value in the database to be the selected option in the select list but it's not working properly Any help? This is a cut down version of the page with the important parts of the code $db = & new MySQL($host,$dbUser,$dbPass,$dbName); $get_id = check_input($_GET['id']); $sql = "SELECT id, image, alternate, cat FROM image WHERE id=$get_id LIMIT 1"; $result = $db->query($sql); while ($row = $result->fetch()) { $id = $row['id']; $image = $row['image']; $alternate = $row['alternate']; $category = $row['cat']; } $cats = array("Jack", "Honeylands", "Events"); and the form <p><label class="imagelabel" for="category">Category: </label> <select name="gallery" size="1"> <?php foreach($cats as $cat) { if($cat == $row['cat']) { echo '<option selected value="'.ucwords($category).'">'.ucwords($category).'</option>'; } else { echo '<option value="'.ucwords($cat).'">'.ucwords($cat).'</option>'; } } ?> </select></p> I have got it adding options for each of the array items but not selecting the one in the database Link to comment https://forums.phpfreaks.com/topic/129011-solved-get-database-value-into-option-selected/ Share on other sites More sharing options...
AndyB Posted October 18, 2008 Share Posted October 18, 2008 Hvae you checked the generated HTML (view source) to see if your logic is working? The correct syntax for option selected is: <option value='something' selected='selected'> .... etc Link to comment https://forums.phpfreaks.com/topic/129011-solved-get-database-value-into-option-selected/#findComment-668821 Share on other sites More sharing options...
AdRock Posted October 19, 2008 Author Share Posted October 19, 2008 I checked that and it makes no difference. Could it be becuase of this if($cat == $row['cat']) { Link to comment https://forums.phpfreaks.com/topic/129011-solved-get-database-value-into-option-selected/#findComment-669245 Share on other sites More sharing options...
AndyB Posted October 19, 2008 Share Posted October 19, 2008 Have you checked the generated HTML (view source) to see if your logic is working? Link to comment https://forums.phpfreaks.com/topic/129011-solved-get-database-value-into-option-selected/#findComment-669257 Share on other sites More sharing options...
.josh Posted October 19, 2008 Share Posted October 19, 2008 I'm leaning toward what Andy is saying, as well. But, failing that... == is case sensitive. Perhaps you have for instance jack instead of Jack in your db? Link to comment https://forums.phpfreaks.com/topic/129011-solved-get-database-value-into-option-selected/#findComment-669259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.