fife Posted July 17, 2012 Share Posted July 17, 2012 First ill show the code then Ill explain the problem. thanks to everyone that helped with my last post I now have this for my function function createoptions($table , $id , $field) { $sql = "select * FROM $table ORDER BY $field"; $res = mysql_query($sql) or die(mysql_error()); while ($a = mysql_fetch_assoc($res)){ echo "<option value=\"{$a[$id]}\" "; if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){ echo"selected";} echo ">". $a[$field]. "</option>"; } } // and in the form I have this <select name="networks" id="networks"> <?php createoptions("networks", "idnetworks", "netname"); ?> </select> This is an edit page. I have a query that pulls the data down. If i echo out $row_rs_doc['network'] I get a number so I know that has a value. However when I view the page the item that is selected as selected is the last item in the list. The number for $row_rs_doc['network'] on the loaded page is 1 yet the selected item is ...... <select name="networks" id="networks"> <option value="24" selected>Admissions</option><option value="33" selected>Asset Management Plan</option><option value="14" selected>Capital</option><option value="23" selected>Child Trust Rep</option><option value="35" selected>Childrens Centre Rep</option><option value="19" selected>Childrens University</option><option value="7" selected>Conference</option><option value="2" selected>Core</option><option value="6" selected>CPD</option><option value="34" selected>Early Years</option><option value="32" selected>Emergency Planning</option><option value="12" selected>Forthright</option><option value="1" selected>General</option><option value="31" selected>Healthy Schools</option><option value="25" selected>High Cost</option><option value="15" selected>Home</option><option value="4" selected>Horizon</option><option value="8" selected>ICT</option><option value="28" selected>In Year Fair Access</option><option value="22" selected>LSCB</option><option value="13" selected>MADCOS</option><option value="30" selected>NCSL Rep</option><option value="26" selected>NW PHA Link</option><option value="27" selected>SACRE</option><option value="16" selected>SAPH Doc Store</option><option value="9" selected>Schools Forum</option><option value="21" selected>SEN Review</option><option value="20" selected>Social Care</option><option value="10" selected>Southport</option><option value="11" selected>Stanley</option><option value="3" selected>Strand</option><option value="29" selected>Support Services</option><option value="36" selected>Traded Services Evalution</option> </select> All i want is to show the correctly selected item which should be "general" as the value there is 1 thanks Quote Link to comment https://forums.phpfreaks.com/topic/265830-select-list-issues-with-selected/ Share on other sites More sharing options...
Ksuil Posted July 17, 2012 Share Posted July 17, 2012 Out of curiousity, why does this segment use $a[$id]; when right below it you compare $a['id']? while ($a = mysql_fetch_assoc($res)){ echo "<option value=\"{$a[$id]}\" "; if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){ echo"selected";} Quote Link to comment https://forums.phpfreaks.com/topic/265830-select-list-issues-with-selected/#findComment-1362180 Share on other sites More sharing options...
fife Posted July 17, 2012 Author Share Posted July 17, 2012 good question an im not sure. I've changed it though and now its doesnt work at all. Now its just showing the first item in the list with nothing set as selected. while ($a = mysql_fetch_assoc($res)){ echo "<option value=\"{$a[$id]}\" "; if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a[id]){ echo"selected";} Quote Link to comment https://forums.phpfreaks.com/topic/265830-select-list-issues-with-selected/#findComment-1362234 Share on other sites More sharing options...
Ksuil Posted July 17, 2012 Share Posted July 17, 2012 Try this. while ($a = mysql_fetch_assoc($res)){ echo "<option value=\"{$a['id']}\" "; if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){ echo"selected";} Quote Link to comment https://forums.phpfreaks.com/topic/265830-select-list-issues-with-selected/#findComment-1362261 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.