Forum Moderators: coopster

Message Too Old, No Replies

Quick and dirty method to "SELECT" the appropriate drop down

is there a "best practice"

         

Clark

2:54 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Problem:

Html page with static drop down boxes go to a dynamically created page pulled from a db. Need to redisplay the form with the previously selected dropdown displayed as selected.

One trick I've used frequently, is to display it like this:

HTML FORM:
<form action=mypage.php>
<select name=selbox size=1>
<option value=>Select one</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>

user selects 3:

PHP FORM:
<form action=mypage.php>
<select name=selbox size=1>
<option value=$selbox SELECTED>$selbox</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>

which results in a dropdown box looking like:

<form action=mypage.php>
<select name=selbox size=1>
<option value=3 SELECTED>3</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>

The upside is, the value of 3 gets displayed. Downside is that 3 is repeated twice and you risk confusing the user.

While this works, I'd prefer to have something like

<form action=mypage.php>
<select name=selbox size=1>
<option value=1 $1selected>1</option>
<option value=2 $2selected>2</option>
<option value=3 $3selected>3</option>
<option value=4 $4selected>4</option>
</select>
</form>

But with 50 choices in 10 dropdowns, that is a lot of typing. Not the programmers way.

I've solved this problem in other ways for other apps with some complicated code. What I'm wondering, has anyone solved the issue in an elegant, simple, quick and dirty way?

Clark

3:28 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A renewed search on the internet yielded a good solution. Rather than

<form action=mypage.php>
<select name=selbox size=1>
<option value=1 $1selected>1</option>
<option value=2 $2selected>2</option>
<option value=3 $3selected>3</option>
<option value=4 $4selected>4</option>
</select>
</form>

do

<form action=mypage.php>
<select name=selbox size=1>
<option $selectedbox[1] value=1>1</option>
<option $selectedbox[2] value=2>2</option>
<option $selectedbox[3] value=3>3</option>
<option $selectedbox[4] value=4>4</option>
</select>
</form>

Then in the php page

$selectedbox[$selbox]="SELECTED";

That should do the trick.

hakre

5:38 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but that leads you to a lot of warnings or accesses on array entries which do not exist. in case i need it quick and dirty, i create an array for all values, then loop though it and output selected as needed:

<form action="mypage.php"> 
<select name="selbox" size="1">
<option value="">Select one</option>
<? foreach(array(1,2,3,4) AS $value) {?>
<option value="<?=$value?>"<?=($selbox==$value)?' selected="selected"':''?>><?=$value?></option>
<? }?>
</select>
</form>

and that's it.

Clark

6:54 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Interesting. You're using some function I'm not familiar with. You somehow replaced the "if" with = signs? And used a colon that I don't know what it means?

Clark

8:17 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Should also note I'm using a templating system so I can't use php code in the template part.

hakre

12:26 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hmm, i thought i posted an answer already, now in short:

array() [php.net] is not a function, it's a language construct to create an array.

the other thingy is not another form of if, it's an expression [php.net]:

There is one more expression that may seem odd if you haven't seen it in other languages, the ternary conditional operator:

<?php
$first? $second : $third
?>

If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.

Clark

12:45 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I appreciate the clarification. What I meant to add that was confusing me was the = sign, e.g.
="<?=$value?>"

(the second = above no the first)

jatar_k

12:49 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that is php shorthand

<?=$value?> is the same as writing <? echo $value;?>

[php.net...]

echo() also has a shortcut syntax, where you can immediately follow the opening tag with an equals sign. This short syntax only works with the short_open_tag configuration setting enabled.

Clark

5:15 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very helpful guys. Now I got it all sorted out :)