动态检查复选框

时间:2010-05-07 10:09:07

标签: php

alt text嗨,我已经处理了一段时间了,需要你的帮助。好吧,我有一个数组$ arrayAmenities,它包含基于从数据库中提取的内容的以下数据的组合:

Air Conditioned
Bar
Brunch
Party Room
Tea Room
Terrace
Valet

我希望应用程序根据数组中包含的数据动态检查以下一组复选框。 使用我的代码,只根据数组中包含的第一个数据检查一个复选框。

你能说出我错过了什么吗?谢谢你回答。

代码:

//get amenities one by one in order to set the checkboxes
        $arrayAmenities = explode(',', $rest_amenities );

        $i=0;
        while(count($arrayAmenities) > $i)
        {
            var_dump($arrayAmenities[$i]);
            switch($arrayAmenities[$i])
            {
                case 'Air Conditioned':
                    $checkedAir = 'checked=true';
                    break;

                case 'Bar':
                    $checkedBar = 'checked=true';
                    break;

                case 'Brunch':
                    $checkedBru = 'checked=true';
                    break;

                case 'Party Room';
                    $checkedPar = 'checked=true';
                    break;
            }


            $i+=1;
        }

    }

复选框

<table cellpadding="0" cellspacing="0" style="font-size:10px">
                        <tr>
                            <td style="border-top:1px solid #CCC;border-right:1px solid #CCC;border-left:1px solid #CCC; padding-left:5px   ">Air Conditioned <input type="checkbox" name="air_cond" <?php print $checkedAir;?> value="Air Conditioned"></td>
                            <td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC;">Bar <input type="checkbox" name="bar" value="Bar" <?php print $checkedBar;?>></td>
                            <td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; ">Brunch <input type="checkbox" name="brunch" value="Brunch" <?php print $checkedBru;?>></td>                          
                        </tr>

                        <tr>
                            <td style="border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC; border-left:1px solid #CCC; padding-left:5px">Party Room <input <?php print $checkedPar;?> type="checkbox" name="party_room" value="Party Room" ></td>
                            <td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC;">Tea Room <input type="checkbox" name="tea_room" value="Tea Room" ></td>
                            <td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC;">Terrace <input type="checkbox" name="terrace" value="Terrace"></td>                         
                        </tr>

                        <tr>
                            <td colspan="3" style="border-bottom:1px solid #CCC; border-left:1px solid #CCC; border-right:1px solid #CCC; padding-left:5px">Valet <input type="checkbox" name="valet" value="Valet" ></td>
                        </tr>
                    </table>

4 个答案:

答案 0 :(得分:1)

使用:

 'checked="checked"';

而不是:

 'checked=true';

答案 1 :(得分:1)

尝试一个简短的方法,会为你节省很多时间

<tr>
    <td your styles>

    Valet 
      <input type="checkbox" name="valet" value="Valet" 
      <? echo ((in_array("Valet", $arrayAmenities)  )?"selected=\"selected\"":"") ?> 
      >

    </td>
</tr>

为每个礼仪重复

答案 2 :(得分:0)

像这样使用,

 // use striaght like this, here don't use the explode function


        $i=0;
 while(count($rest_amenities) > $i)
        {
switch($rest_amenities[$i])
            {
                case 'Air Conditioned':
                    $checkedAir = 'checked=checked';
                    break;

                case 'Bar':
                    $checkedBar = 'checked=checked';
                    break;

                case 'Brunch':
                    $checkedBru = 'checked=checked';
                    break;

                case 'Party Room';
                    $checkedPar = 'checked=checked';
                    break;
            }

        $i+=1;
    }

现在检查一下。

答案 3 :(得分:0)

谢谢大家,感谢您的建议,我成功完成了这项工作: 我对我的代码进行了以下更改,它工作正常: 首先,我取消了一段时间并切换并执行以下操作:

$arrayAmenities = explode(',', $rest_amenities );
        $elt = implode(',', $arrayAmenities);

至于复选框,我只需将以下内容设置为checked = true:

<?php strStr($elt, "Air Conditioned")?print"checked=true":print "";?>

我重复了上面的所有复选框,它完美无缺。谢谢大家的建议,这对我帮助很大。