Dinamik onay kutularını işaretleyin

4 Cevap

alt text Merhaba, bir süredir bu ile uğraşan ve sizin yardımınıza ihtiyacımız ive. iyi ben veritabanından getirilen ne dayalı aşağıdaki verileri bir arada içeren bir dizi $ arrayAmenities var:

Air Conditioned
Bar
Brunch
Party Room
Tea Room
Terrace
Valet

I would like the application to dynamically check the following set of onay kutularını 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.

Eğer Im eksik ne söyleyebilir misiniz? Cevap için teşekkürler.

kodu:

//get amenities one by one in order to set the onay kutularını
        $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;
        }

    }

onay kutularını

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

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

Onay kutularını gelince, ben sadece = true teslim olarak aşağıdakileri ayarlayın:

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

Ben tüm onay kutularını için yukarıdaki adım tekrarlanır ve mükemmel çalışıyor. Bana çok yardımcı Öneriniz için teşekkür ederiz.

Kullanın:

 'checked="checked"';

yerine:

 'checked=true';

Kısa bir yaklaşım deneyin, size com çok kazandıracak

<tr>
    <td your styles>

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

    </td>
</tr>

Her İmkan için tekrarlayın

Bu gibi kullanın,

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

Şimdi bu kontrol edin.