Jump to content

[SOLVED] Best way to show selected option in List Box


ashishmat1979

Recommended Posts

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.

 

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.

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>";

}

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

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.

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>";

}

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

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  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.