【问题标题】:PHP Loop with smarty带有 smarty 的 PHP 循环
【发布时间】:2014-03-07 12:27:39
【问题描述】:

这是我用 smarty 编写的 PHP 代码。

<p>
    <label for="dob">{$smarty.const.LBL_DATE_OF_BIRTH}</label>
    <span>
        <select class="left" id="bday" name="bday" style="width:60px;" >
            <option value="">- {$smarty.const.LBL_DAY} -</option>{$days}
        </select>
    </span>
</p>

我的 PHP 代码是

for($bd=1;$bd<=31;$bd++)
{
    $bdkey=($bd<10)?"0".$bd:$bd;
    $selected='';
    if($bdkey==date("d",strtotime($data['dob']))) $selected ='selected';
    $days.="<option value=".$bdkey." $selected >".$bdkey."</option>";
}

$smarty->assign('days',$days); 

这个值被插入到数据库中。但它没有选择值。就像,如果我选择 15 并保存,它会插入 DB。但不显示选定的值。提前致谢。

【问题讨论】:

  • 尝试使用:$selected=' selected="selected" ;`
  • @Oscar Pérez 不,这行不通
  • 您的if($bdkey==date("d",strtotime($data['dob']))) 语句可能返回错误。把每张支票打印出来看看,为什么得不到预期的结果。

标签: php mysql smarty


【解决方案1】:

用 Smarty 的方式来做,Smarty 具有出色的功能html_options 来创建 html 选择字段。

PHP:

$days = array("- ".LBL_DAY." -");
for($bd=1;$bd<=31;$bd++) {
    $days[] = ($bd<10)?"0".$bd:$bd;
}

$smarty->assign("days", $days);
$day = date("d",strtotime($data['dob']));
$smarty->assign("dob", $day);

聪明的:

<p>
    <label for="dob">{$smarty.const.LBL_DATE_OF_BIRTH}</label>
    <span>
        {html_options values=$days output=$days selected=$dob class="left" id="bday" name="bday" style="width:60px;"}
    </span>
</p>

这将使用 PHP 中的数组和数据并创建完整的选择元素,包括所有选项,当前存储的选项已被选中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多