Sam San
Sam San

Reputation: 6903

html select option SELECTED

I have in my php

$sel = "
    <option> one </option>
    <option> two </option>
    <option> thre </option>
    <option> four </option>
";

let say I have an inline URL = site.php?sel=one

if I didn't saved those options in a variable, I can do it this way to make one of the option be SELECTED where value is equal to $_GET[sel]

<option <?php if($_GET[sel] == 'one') echo"selected"; ?> > one </option>
<option <?php if($_GET[sel] == 'two') echo"selected"; ?> > two </option>
<option <?php if($_GET[sel] == 'three') echo"selected"; ?> > three </option>
<option <?php if($_GET[sel] == 'four') echo"selected"; ?> > four </option>

but the problem is, I need to save those options in a variable because I have a lot of options and I need to call that variable many times.

Is there a way to make that option be selected where value = $_GET[sel] ?

Upvotes: 15

Views: 189590

Answers (7)

Franklin CI
Franklin CI

Reputation: 171

                                          <?php 

                                            $options = array( 'DG', 'DD1', 'DD2', 'DD3', 'SPAG', 'AG' );
                                            $options_title = array( 'Admin', 'Superviseur Direction 1', 'Superviseur Direction 2', 'Superviseur Direction 3', 'Super-Agent', 'Agent' );

                                            $output = '';
                                            for( $i=0; $i<count($options); $i++ ) 
                                            {


                                              if ($get_users_data["permission"] == $options[$i]) {

                                                echo "<option value=$options[$i] selected> $options_title[$i]</option>";

                                              }else{

                                                echo "<option value=$options[$i] > $options_title[$i]</option>";

                                              }


                                            }



                                         ?>

                                        </select>

Upvotes: 0

tradesouthwest
tradesouthwest

Reputation: 176

You could create a function and then add it to each option field to print the selected="selected".

Example

<option value="PHP" ' . tsw_selected('PHP', $row['anchor']) . '>PHP/PDO/SQL[+]</option>
<option value="HTML" ' . tsw_selected('HTML', $row['anchor']) . '>HTML/CSS</option>

Then your fun:

function tsw_selected( $opt, $val )
{
$sel = '';
$opt = ('' != $opt ) ? $opt : 'OTHER';
if( $opt == $val ) $sel = true;
if ( $sel ) {
    return 'selected="selected"'; } else { return ''; }
}

This will echo the select="selected" per true and echo '' (nothing) for false. You could set $sel = false, but it does not matter. Booloean checks on empty as false.

Upvotes: 1

Muhammad Fahad
Muhammad Fahad

Reputation: 1412

This is simple example by using ternary operator to set selected=selected

<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $key => $value) { ?>
  <option value="<?php echo $key;?>" <?php echo ($key ==  '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>

Upvotes: 8

Sirko
Sirko

Reputation: 74086

Just use the array of options, to see, which option is currently selected.

$options = array( 'one', 'two', 'three' );

$output = '';
for( $i=0; $i<count($options); $i++ ) {
  $output .= '<option ' 
             . ( $_GET['sel'] == $options[$i] ? 'selected="selected"' : '' ) . '>' 
             . $options[$i] 
             . '</option>';
}

Sidenote: I would define a value to be some kind of id for each element, else you may run into problems, when two options have the same string representation.

Upvotes: 23

Mohit Mehta
Mohit Mehta

Reputation: 1285

foreach($array as $value=>$name)
{
    if($value == $_GET['sel'])
    {
         echo "<option selected='selected' value='".$value."'>".$name."</option>";
    }
    else
    {
         echo "<option value='".$value."'>".$name."</option>";
    }
}

Upvotes: 6

TRiG
TRiG

Reputation: 10643

foreach ($array as $value => $name) {
     echo '<option value="' . htmlentities($value) . '"' . (($_GET['sel'] === $value) ? ' selected="selected"') . '>' . htmlentities($name) . '</option>';
}

This is fairly neat, and, I think, self-explanatory.

Upvotes: 0

Rob
Rob

Reputation: 3574

You're missing quotes for $_GET['sel'] - fixing this might help solving your issue sooner :)

Upvotes: 0

Related Questions