2015-09-29 42 views
0

我有一個foreach循環,將「1,2」格式的文本分割爲「1 2」。比較foreach和複選框的值

我想比較每個分裂值與$ rowCentral ['tuitionAreaID'],當它匹配時,複選框將被選中。我能夠實現這一點,但是,我的所有複選框都是重複的。

<?php 
while($rowCentral=mysql_fetch_array($resultCentral)) 
    { 
    ?> 
    <?php 
    foreach($myArray as $my_Array){ 
    //echo $my_Array.'<br>'; 
    ?> 
    <input type="checkbox" name="centralArea[]" value="<?php echo $rowCentral['tuitionAreaID']?>" <?php if($my_Array == $rowCentral['tuitionAreaID']){echo "checked";}?>> <?php echo $rowCentral['tuitionAreaDesc']?><br> 
    <?php 
    } 
    ?> 
    <?php  
    } 
    ?> 

這是上述代碼的輸出(每行是重複的):

Anson, Tanjong Pagar 
Anson, Tanjong Pagar 
Beach Road, High St, Hill St 
Beach Road, High St, Hill St 
Cairnhill, Newton, Orchard, Scotts Rd 
Cairnhill, Newton, Orchard, Scotts Rd 
Cecil, Chinatown, Marina, People's Park, Raffles Place 
Cecil, Chinatown, Marina, People's Park, Raffles Place 
+3

那麼,是什麼問題? – Amarnasan

+0

可以請你格式化你的代碼 – e4c5

+0

複選框全部複製兩次 –

回答

1

我想你想達到什麼是這個...如果你需要設置你應該檢查您的複選框的選中屬性seperately,並測試,在您的foreach,然後只回聲出複選框一次....當心,那我沒有測試過我的代碼..

<?php 
while($rowCentral=mysql_fetch_array($resultCentral)) 
{  
    $checked = ""; 
    foreach($myArray as $my_Array) 
    { 
     if($my_Array == $rowCentral['tuitionAreaID']) 
     { 
     $checked = "checked"; 
     }   
    } 
    echo "<input type='checkbox' name='centralArea[]' value='".$rowCentral['tuitionAreaID']."' ".$checked.">".$rowCentral['tuitionAreaDesc']."<br>" 
} 
+0

感謝您的幫助。我工作 –