2016-07-22 110 views
0

正如我經歷了幾種解決方案,我沒有得到解決方案。當我去編輯和打開頁面時,我想顯示覆選框的列表,標記爲我在插入時選擇的列表。 這是我的看法網頁..如果在編輯時勾選複選框,則顯示

<div class="form-group"> 
     <label class="control-label col-sm-1" for="courses">Courses Completed</label> 
     <div class=" col-sm-5"> 

     <input type="checkbox" name="select_course[]" id="select_course" value="10" <?php if($row->courses =='10') echo "checked" ;?>>10 
     <input type="checkbox" name="select_course[]" id="select_course" value="12" <?php if($row->courses =='12') echo "checked" ;?>>12 
     <input type="checkbox" name="select_course[]" id="select_course" value="degree" <?php if($row->courses=='degree') echo "checked" ;?>>degree 

     </div> 
    </div> 

通過做這樣沒有複選框被顯示爲插入檢查state.the值就像是一個數組,我的表看起來像這樣

id name  address    sex  courses  places     image   password 
1 nesru   v     male "10"  tamilnadu upld-file1469095130.jpg  l 
3 siraj koonathil house  male  "10,12"  tamilnadu upld-file1469167954.jpg  d 
+1

你可以打印$ row-> –

+0

顯示'print_r($ row-> courses)的輸出;' –

回答

1

按表已張貼看來你是存儲在課程逗號分隔值,您需要在數組轉換,並檢查使用in_array()功能

<div class="form-group"> 
    <label class="control-label col-sm-1" for="courses">Courses Completed</label> 
    <?php $idArray = explode(",",$row->courses); ?> 
    <div class=" col-sm-5"> 
     <input type="checkbox" name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="10" <?php if(in_array('10',$idArray)) echo "checked" ;?>>10 
     <input type="checkbox" name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="12" <?php if(in_array('12',$idArray)) echo "checked" ;?>>12 
     <input type="checkbox" name="select_course[]" id="select_course" rel="<?php echo $row->courses; ?>" value="degree" <?php if(in_array('degree',$idArray)) echo "checked" ;?>>degree 
    </div> 
</div> 
+0

我曾使用json_encode來插入值,所以需要解碼它? –

+0

沒有按照你的表結構它不需要解碼它... –

+0

我已經解碼它得到它...感謝很多..改變像這樣<?php $ idArray = explode(「,」,json_decode($ row- >課程)); ?> –

0

的屬性託運狀態進行檢查=「」

把你的PHP代碼中的引號

<input type="checkbox" name="select_course[]" id="select_course" value="degree" checked="<?php if($row->courses=='degree') echo "checked" ;?>" /> 
+0

我已經給出了這個,但所有的複選框都顯示爲標記爲 –

+0

@MOHAMMED你必須顯示' print_r($ row-> courses);'對我們來說每個人都是明智的猜測。在你的代碼中輸出帽子 –

+0

然後我將不得不同意上面的評論者你需要print_r($ row-> courses)的內容;這聽起來像錯誤是在從數據庫中檢索數據或從 – Danimal

0

你應該把下面的代碼:

<input type="checkbox" name="select_course[]" id="select_course" value="degree" <?php if($row->courses=='degree'): ?> checked="checked" <?php endif; ?> />