douren6874 2014-04-07 03:35
浏览 26
已采纳

从阵列动态检查复选框

Given Data in a varchar mysql field 1,3,6

I have 7 checkboxes for SUN MON TUE WED THU FRI SAT I have stored the days that need to be checked but I want to display all non checked and checked boxes

So for my example output will be SUN(checked) MON TUE(checked) WED THU FRI(checked) SAT These 7 words represent checkboxes. assumed the data is a varchar mysql field 1,3,6

My code works partially because it will only show the first day that needs to be checked but not the other ones. It does However display the non-checked checkboxes which is good.

Here is my code: BTW sorry if the formatting is weird! I am new here.

 <? php
  $RecurringDays = explode(",", $recur['weekday']);
 for ($i = 1; $i <= count($RecurringDays) - 1; $i++) {
     if ($RecurringDays[$i - 1] == "1") {
         echo "<td><input type='checkbox' name='0' id='0' checked='checked' />";
         echo "<label for='0'>Sun </label></td>";
         continue;
     } else {
         echo "<td><input type='checkbox' name='0' id='0' />";
         echo "<label for='0'>Sun </label></td>";
     }
     if ($RecurringDays[$i - 1] == "2") {
         echo "<td><input type='checkbox' name='1' id='1' checked='checked' />";
         echo "<label for='0'>Mon </label></td>";
         continue;
     } else {
         echo "<td><input type='checkbox' name='1' id='1'  />";
         echo "<label for='0'>Mon </label></td>";
     }
     if ($RecurringDays[$i - 1] == "3") {
         echo "<td><input type='checkbox' name='2' id='2' checked='checked' />";
         echo "<label for='0'>Tue </label></td>";
         continue;
     } else {
         echo "<td><input type='checkbox' name='2' id='2' />";
         echo "<label for='0'>Tue </label></td>";
     }
     if ($RecurringDays[$i - 1] == "4") {
         echo "<td><input type='checkbox' name='3' id='3' checked='checked' />";
         echo "<label for='0'>Wed </label></td>";
         continue;
     } else {
         echo "<td><input type='checkbox' name='3' id='3' />";
         echo "<label for='0'>Wed </label></td>";
     }
  • 写回答

1条回答 默认 最新

  • duanjianxiu9400 2014-04-07 03:48
    关注

    Your code will echo only the days in the varchar, as per your code (as they are in the for loop). I recommend the following edit.

    <?php
    //The days in the database
    //$recurringDays = explode(",", $recur['weekday']); //expected an array(1,3,6) 
    $recurringDays = array(1,3,6) ;
    //The days in a the week    
    $days = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
    for($i = 1; $i < 7; $i++ ){
        if(in_array($i,$recurringDays)){
         echo "<td><input type='checkbox' name='0' id='".$i."' checked='checked' />";
             } else {
                 echo "<td><input type='checkbox' name='0' id='".$i."' />";             
             }
         echo "<label for='".$i."'>".$days[$i]." </label></td>";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法