ashishmat1979 Posted June 19, 2007 Share Posted June 19, 2007 I need a way so that we can show selected option in list box without using if condition in loop since some times we have lot of options and we have too much if conditions to check. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted June 19, 2007 Share Posted June 19, 2007 could you explain that again, i'm lost ??? sorry. Regards ACE Quote Link to comment Share on other sites More sharing options...
ashishmat1979 Posted June 19, 2007 Author Share Posted June 19, 2007 Normally we fetch data for listbox from it's master table like for a invoice billing we select client name from listbox filled by client names from client table and store client id in invoice table; now when we edit that invoice or bill we have to show that client name selected in our listbox on form. For this we usually fill names of client from client table by a for loop and show that name selected by checking each id in if condition to match one stored in our invoice. Now if there is a long list of clients then script will took long time since if condition is repeated in for loop. thus needs a more optimistic solution for this. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted June 19, 2007 Share Posted June 19, 2007 An option is if you are editing the invoice that option should not be able to be edited so why not just display the client and not use a listbox. Once your create the link between the invoice and the client that should not change. Quote Link to comment Share on other sites More sharing options...
ashishmat1979 Posted June 19, 2007 Author Share Posted June 19, 2007 No i want a general solution that was just an example to demonstrate. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted June 19, 2007 Share Posted June 19, 2007 Here is a Function I use to Create a listbox also display option that was defined. // Used to create a list of distinct values from field to display as search options // 1 - Working Table Name $tbl // 2 - Field Name $fn // 3 - Field Name for where clause $fnd // 4 - Value $vl Function QSDLA($tbl,$fn,$fnd,$vl) { echo "<td>"; $QSdist = "select Distinct ".$fn.",".$fnd." from ".$tbl." where ".$fnd." != 'Null' ORDER by ".$fnd." ASC"; echo "<select size='1' style='width:200' name='".$fn."'>"; echo "<option echo value=''>Make Selection</option>"; $options=mysql_query($QSdist); while($data = mysql_fetch_array($options)) { echo "<option value='".$data[$fn]."' "; IF ($vl == $data[$fn]) {Echo "selected";} echo ">".$data[$fnd]." - ".$data[$fn]."</option>"; } echo "</select>"; echo "</td>"; } But By Changing the Make Selection to Display the Selection Made you can remove the if clause in the Function // Used to create a list of distinct values from field to display as search options // 1 - Working Table Name $tbl // 2 - Field Name $fn // 3 - Field Name for where clause $fnd // 4 - Value $vl Function QSDLA($tbl,$fn,$fnd,$vl) { echo "<td>"; $QSdist = "select Distinct ".$fn.",".$fnd." from ".$tbl." where ".$fnd." != 'Null' ORDER by ".$fnd." ASC"; echo "<select size='1' style='width:200' name='".$fn."'>"; echo "<option echo value='".$vl."'>".$vl."</option>"; $options=mysql_query($QSdist); while($data = mysql_fetch_array($options)) { echo "<option value='".$data[$fn]."'>".$data[$fnd]." - ".$data[$fn]."</option>"; } echo "</select>"; echo "</td>"; } Quote Link to comment Share on other sites More sharing options...
ashishmat1979 Posted June 20, 2007 Author Share Posted June 20, 2007 Hi jvrothjr, Actually i need to show one option selected but don't want to include if condition like u used in while loop to show selected item. Since as no. of options increases this will take extra resource to check each option Quote Link to comment Share on other sites More sharing options...
AndyB Posted June 20, 2007 Share Posted June 20, 2007 Best way? Can you suggest any way to show a selected item without a conditional test in a loop? If you are getting the selectable options from a database, then jvrothjr's solution is fine. The 'extra resources' are likely immeasurably small. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted June 20, 2007 Share Posted June 20, 2007 This second option does not use an if statement just defaults the first value to the defined value thus that value will show up twice in the list. Meaning in stead of and if clause just display the defined value as the default then create the list box below it. // Used to create a list of distinct values from field to display as search options // 1 - Working Table Name $tbl // 2 - Field Name $fn // 3 - Field Name for where clause $fnd // 4 - Value $vl Function QSDLA($tbl,$fn,$fnd,$vl) { echo "<td>"; $QSdist = "select Distinct ".$fn.",".$fnd." from ".$tbl." where ".$fnd." != 'Null' ORDER by ".$fnd." ASC"; echo "<select size='1' style='width:200' name='".$fn."'>"; echo "<option echo value='".$vl."'>".$vl."</option>"; $options=mysql_query($QSdist); while($data = mysql_fetch_array($options)) { echo "<option value='".$data[$fn]."'>".$data[$fnd]." - ".$data[$fn]."</option>"; } echo "</select>"; echo "</td>"; } Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted June 20, 2007 Share Posted June 20, 2007 Opps there is one thing if your storing the ID and not the Description displayed // Used to create a list of distinct values from field to display as search options // 1 - Working Table Name $tbl // 2 - Field Name $fn // 3 - Field Name for where clause $fnd // 4 - Value $vl Function QSDLA($tbl,$fn,$fnd,$vl) { echo "<td>"; $QSdist = "select Distinct ".$fn.",".$fnd." from ".$tbl." where ".$fnd." != 'Null' ORDER by ".$fnd." ASC"; echo "<select size='1' style='width:200' name='".$fn."'>"; $QSdist_a = "select ".$fn.",".$fnd." from ".$tbl." where ".$fn." = '".$vl."'"; $options_a=mysql_query($QSdist_a); while($data_a = mysql_fetch_array($options_a)) { echo "<option value='".$data_a[$fn]."'>".$data_a[$fnd]." - ".$data_a[$fn]."</option>"; } $options=mysql_query($QSdist); while($data = mysql_fetch_array($options)) { echo "<option value='".$data[$fn]."'>".$data[$fnd]." - ".$data[$fn]."</option>"; } echo "</select>"; echo "</td>"; } The red text will query for the information for that record and display it as the first selection Quote Link to comment Share on other sites More sharing options...
ashishmat1979 Posted June 21, 2007 Author Share Posted June 21, 2007 Finally found a way: I set the particular array index that is my selected value with value "selected" and repeated that array for all option values, when i reached that value it added "selected" in that option. <select name="isalutation_id" > <? $salutations = Get_Details($obj,'salutation_mast','','isalutation_id'); $selArrVal[$row[0]['isalutation_id']] = "selected"; for($i=0;$i<count($salutations);$i++){ ?><option value="<?=$salutations[$i]['isalutation_id']?>" <?=$selArrVal[$salutations[$i]['isalutation_id']]?> ><?=$salutations[$i]['vsalutation']?></option><? } ?> </select> Thanks for your all support 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.