gobbles Posted January 18, 2007 Share Posted January 18, 2007 Hey All,Im just trying to cut down on thousands of lines of code at the moment and hopefully someone could help me.I have a drop down box and the "selected" value needs to be selected by a dynamic value that gets entered into the database, this is currently how im doing it:[code]<?php if($modify_row[21] == "0") { print "<option value=\" \"></option>\n"; print "<option value=\"1\">Active</option>\n"; print "<option value=\"0\" selected>In-Active</option>\n"; } elseif ($modify_row[21] == "1") { print "<option value=\" \"></option>\n"; print "<option value=\"1\" selected>Active</option>\n"; print "<option value=\"0\">In-Active</option>\n"; }?> [/code]At the moment i have over 100 "options" and i think it would be a major waist of code doing it this way, can someone help me out please.Its fairly straight forward, basically the option that is automatically selected needs to be the one coming from the database $modify_row[21].Thanks All Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 You might consider making a function so you don't have the same thing posted over and over. Here is a simpler version. Waist is around your tummy, waste is a verb more appropriate.[code]for($i=1; $i<=100; $i++){ printStuff($modify_row[$i]);}function printStuff($value){ print '<option value=" "></option> <option value="1"'; if(!$value){ print ' selected '; } print '>Active</option> <option value="0"'; if($value){ print ' selected '; } print '>In-Active</option>';}[/code] Quote Link to comment Share on other sites More sharing options...
fiat Posted January 18, 2007 Share Posted January 18, 2007 try this[code]<?php if($modify_row[21] == "0") { $selected = "selected";} print "<option value=\" \" $selected></option>\n"; print "<option value=\"1\" $selected>Active</option>\n"; print "<option value=\"0\" $selected>In-Active</option>\n";[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 fiat, that will mark either none or all selected. Quote Link to comment Share on other sites More sharing options...
gobbles Posted January 18, 2007 Author Share Posted January 18, 2007 What if it isnt numbers ... say the options are contact methods, Phone, Fax, Email, Mail, Car etc etc? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 Erm, you could make an array of the options, and use foreach, or call the function several times.[code]$options = array('Phone', 'Fax');foreach($options AS $o){ printStuff($_POST[$o]);}[/code]Or just [code]printStuff($_POST['Phone']);printStuff($_POST['Fax']);[/code]Without seeing the code that's all I can suggest. You'll obviously have to modify it to suit you. 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.