SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to show selected option in List Box

    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.
    PHP Functions Class/Php Stuff: php-classes-functions.blogspot.com
    SMS World: sms-treasure.blogspot.com
    Finance Business India: financebusinessindia.blogspot.com
    Retail Market India: retailmarketindia.blogspot.com

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure whether it is possible to make an option selected in the list box without using if or any other types of conditional statements in PHP. But if you have taken the options from the database table or from any of the arrays, then you don't have to make any more than one if statement for selection.

    If possible to do so without using conditions, it would be helpful to me too.

    Thanks.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still waiting for a solution
    PHP Functions Class/Php Stuff: php-classes-functions.blogspot.com
    SMS World: sms-treasure.blogspot.com
    Finance Business India: financebusinessindia.blogspot.com
    Retail Market India: retailmarketindia.blogspot.com

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you post your array or data to be shown in the list box? So that we could discuss here further clearly. But i dont think that you can do it without using any type of conditions. From where you data comes for populating the list?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why can't you do this:
    PHP Code:
    $selected = isset($_POST['posted_value']) ? $_POST['posted_value'] : 'default';
    foreach (
    $options as $option)
    {
      print 
    '<option value="'.$option.'"';
      if (
    $option == $selected) print ' selected="selected"';
      print 
    '>' $option '</option>';


  6. #6
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    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.

    Code:
    <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
    PHP Functions Class/Php Stuff: php-classes-functions.blogspot.com
    SMS World: sms-treasure.blogspot.com
    Finance Business India: financebusinessindia.blogspot.com
    Retail Market India: retailmarketindia.blogspot.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •