...

View Full Version : slected selected



pphpnewbie
01-25-2010, 04:08 PM
This code is in the update form but somehow it does not work. Would appreciate any help.


<td nowrap="nowrap" align="right">Month:</td>
<td>
<?
//selector_s4_dis: dynamically selects a value in a select according to what's already in the DB

function selector_s4_dis($dis)
{
$arr = array('January', 'February', 'March', 'April', 'May', 'June' 'July', 'August', 'September', 'October', 'November', 'December');
for($i = 0; $i < count($arr); $i++)
{
$selected = ($arr[$i] == $dis) ? 'selected="selected"' : '';
echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>\n";
}
//return;
}

?>

<select input type="text" name="month" id="month">
<option value="January" selected="selected">January</option>
<option value="February" >February</option>
<option value="March" >March</option>
<option value="April" >April</option>
<option value="May">May</option>
<option value="June" >June</option>
<option value="July" >July</option>
<option value="August" >August</option>
<option value="September" >September</option>
<option value="October" >October</option>
<option value="November" >November</option>
<option value="December" >December</option>
</select></td>

mlseim
01-25-2010, 04:13 PM
What you have there is a PHP function.
It has to be called from someplace ... in a script somewhere.

What you're showing us means nothing.

pphpnewbie
01-25-2010, 04:27 PM
Somehow I tried different methods they just cannot work. Please help, thanks.


<td>
<select input type="text" name="month" id="month">
<? for($i=0;$i<11;$i++) { ?>
<option value="<?=$i?>" <? if($i==$selected) echo 'selected="selected"';?>><?=$i?></option>
<? }//end for ?>

<option value="January" selected="selected">January</option>
<option value="February" >February</option>
<option value="March" >March</option>
<option value="April" >April</option>
<option value="May">May</option>
<option value="June" >June</option>
<option value="July" >July</option>
<option value="August" >August</option>
<option value="September" >September</option>
<option value="October" >October</option>
<option value="November" >November</option>
<option value="December" >December</option>
</select></td>

angst
01-25-2010, 04:41 PM
try something like this;



$arr = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

$dis = "May";

echo "<select>\n";
foreach($arr As $Month){
if($dis == $Month){
echo "<option value='" . $Month . "' SELECTED>" . $Month . "</option>\n";
} else {
echo "<option value='" . $Month . "'>" . $Month . "</option>\n";
}

}
echo "</select>";

pphpnewbie
01-25-2010, 05:06 PM
Thanks. I am really clueless of where to put those code.

angst
01-25-2010, 05:21 PM
i've updated your script:



<td nowrap="nowrap" align="right">Month:</td>
<td>
<?
$arr = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

echo "<select>\n";
foreach($arr As $Month){
if($dis == $Month){
echo "<option value='" . $Month . "' SELECTED>" . $Month . "</option>\n";
} else {
echo "<option value='" . $Month . "'>" . $Month . "</option>\n";
}

}
echo "</select>";

?>
</td>


that should work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum