fife Posted July 12, 2012 Share Posted July 12, 2012 Im having trouble writing this code as im not very good at breaking in an out of an echo. Here is the code. It has an error somewhere in it. Ithink its something to do with the if statement as it was fine until i added that. echo "<option value=\"{$a[$id]}\" " .if($row_rs_doc['network']==$a['id']){echo"selected";} . ">$a[$field]</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/ Share on other sites More sharing options...
hakimserwa Posted July 12, 2012 Share Posted July 12, 2012 try this it should work but next time provide us with the error massage. echo "<option value=\"{$a[$id]}\" "; if($row_rs_doc['network']==$a['id']){ echo"selected"; } echo ">". $a[$field]. "</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1361015 Share on other sites More sharing options...
fife Posted July 12, 2012 Author Share Posted July 12, 2012 cool thanks but that brings back an error too. Here is the view source <select name="networks" id="networks"> <option value="24" <option value="33" <option value="14" <option value="23" <option value="35" <option value="19" <option value="7" <option value="2" <option value="6" <option value="34" <option value="32" <option value="12" <option value="1" <option value="31" <option value="25" <option value="15" <option value="4" <option value="8" <option value="28" <option value="22" <option value="13" <option value="30" <option value="26" <option value="27" <option value="16" <option value="9" <option value="21" <option value="20" <option value="10" <option value="11" <option value="3" <option value="29" <option value="36" Notice: Undefined variable: row_rs_doc in /home/sites/members/edit-documents.php on line 254 selected></option> </select> Basically this is the code that works echo "<option value=\"{$a[$id]}\">$a[$field]</option>"; I just want to edit it so that if $row_rs_doc['network'] == the option id echo the selected as this row is from an edit form Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1361018 Share on other sites More sharing options...
hakimserwa Posted July 12, 2012 Share Posted July 12, 2012 i see its saying you have un defined valuable "row_rs_doc" which means you are making use of this valuable before setting it to anything. show me just the errror that it gives you,. Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1361021 Share on other sites More sharing options...
fife Posted July 12, 2012 Author Share Posted July 12, 2012 Notice: Undefined variable: row_rs_doc in /home/sites/members/edit-documents.php on line 254 is the error i get. The thing is though if i echo $row_rs_doc['network'] into the page i get a value so I know its there. further more if I echo <option value=\"{$a[$id]}\">$a[$field]</option> I get <option value="33">Asset Management Plan</option> So i know both of them are there. If i echo echo "<option value=\"{$a[$id]}\" "; if($row_rs_doc['network']==$a['id']){ echo"selected"; } echo ">". $a[$field]. "</option>"; then i get <option value="24" <option value="33" etc with that error at the end. There are no other errors on the page. Is it not something to do with the syntax? To be honest im just guessing an its driving me insane Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1361027 Share on other sites More sharing options...
Tarential Posted July 12, 2012 Share Posted July 12, 2012 It may be set in some iterations of the loop and not set in others. Try something like this: echo "<option value=\"{$a[$id]}\" "; if(isset($row_rs_doc['network']) && $row_rs_doc['network']==$a['id']){ echo"selected"; } echo ">". $a[$field]. "</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1361081 Share on other sites More sharing options...
fife Posted July 17, 2012 Author Share Posted July 17, 2012 Ok I think that's nearly it. There are at least no php errors flagging up but there is an error. First ill show the code then Ill explain the problem. thanks to everyone that helped 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/265557-break-in-an-out-of-code/#findComment-1362163 Share on other sites More sharing options...
fife Posted July 17, 2012 Author Share Posted July 17, 2012 Ive just noticed in the code they are all coming back as selected!! Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1362165 Share on other sites More sharing options...
fife Posted July 17, 2012 Author Share Posted July 17, 2012 ok scratch the last 2 posts.. just relised im asking a second question. The echo has been solved by Tarential so I will ask the second bit as a new question. thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/265557-break-in-an-out-of-code/#findComment-1362172 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.