开发者

Check Checkboxes dynamically

开发者 https://www.devze.com 2022-12-29 18:18 出处:网络
Hi, ive been dealing with this for some time now and need your help. well I have an array $arrayAmenities which contains a combination of the following data based on what is fetched from the database:

Check Checkboxes dynamically

Hi, ive been dealing with this for some time now and need your help. well I have an array $arrayAmenities which contains a combination of the following data based on what is fetched from the database:

Air Conditioned
Bar
Brunch
Party Room
Tea Room
Terrace
Valet

I would like the application to dynamically check the following set of checkboxes based on the data contained in the array. With my code only one checkbox is checked based on the first data contained in the array.

Can you please tell what Im missing? Thanks for answering.

code:

//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]);
            s开发者_运维问答witch($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;
        }

    }

checkboxes

<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>


Use:

 'checked="checked"';

instead of:

 'checked=true';


Try a short approach, will save you a lot of com

<tr>
    <td your styles>

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

    </td>
</tr>

repeat for each Amenity


Use like this,

 // 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;
    }

Now check this.


Thanks you all, thank to your advices I managed to get it done: I made the following changes to my code and it work fine: First I took off the while and switch and did the following:

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

As for the checkboxes, i just set the following as checked=true:

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

I repeated the step above for all the checkboxes and it works perfectly. Thank you all for your suggestion which helped me a lot.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号