如何在复选框中显示数据

时间:2012-01-28 04:24:59

标签: php zend-framework

我的输出是这样的: - 当我print_r ($aResultData)

Array ( [0] => Array ( [parent] => SK336 [child] => CP ) [1] => Array ( [parent] => SK336 [child] => PES ) [2] => Array ( [parent] => SK995 [child] => MS ) [3] => Array ( [parent] => SK995 [child] => LSW ) [4] => Array ( [parent] => SK995 [child] => GES ) [5] => Array ( [parent] => SK995 [child] => RSW ) )

现在我想将这些数据显示在php中的复选框中。

            parent  child
checkbox    SK336   CP
checkbox    SK336   PES
checkbox    SK995   MS
checkbox    SK995   LSW
checkbox    SK995   GES
checkbox    SK995   RSW

和提交按钮

并在提交后复选框(这取决于我选择的是什么),最后它存储到另一个数据库我可以做什么?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

$parents = array();
$childs = array();
foreach ($aResultData as $aResultDataValue) {
      $parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
      $childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}

foreach ($parents as $parent) {
     echo '<div>';
     echo '<div><input type="checkbox" name="parents[]" value="'.$parent.'" id="parents_'.$parent.'"/>
     <label for="parents_'.$parent.'">'.$parent.'</label></div>';
     foreach ($childs[$parent] as $child) {
            echo '<div style="margin-left:15px;"><input type="checkbox" name="childs[]" value="'.$child.'" id="childs_'.$child.'"/>
            <label for="childs_'.$child.'">'.$child.'</label></div>';
     }
    echo '</div>';
}

希望这有帮助

修改

如果要检查所有子复选框,如果选择了父级,则需要c =使用jQuery并且还必须更改PHP代码:

PHP代码:

$parents = array();
$childs = array();
foreach ($aResultData as $aResultDataValue) {
     $parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
     $childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}

foreach ($parents as $parent) {
     echo '<div>';
     $parent_value = "'$parent'";
     echo '<div><input type="checkbox" name="parents[]" value="'.$parent.'" id="'.$parent.'" class="parentCheck"/>
     <label for="parents_'.$parent.'">'.$parent.'</label></div>';
     foreach ($childs[$parent] as $child) {
           echo '<div style="margin-left:15px;"><input type="checkbox" name="childs[]" value="'.$child.'" id="childs_'.$child.'" class="child_'.$parent.'"/>
           <label for="childs_'.$child.'">'.$child.'</label></div>';
     }
     echo '</div>';
}

JS CODE:

jQuery(document).ready(function(){
     // add multiple select / deselect functionality
    jQuery(".parentCheck").click(function () {
       var childId = jQuery(this).attr('id');
       jQuery('.child_'+childId).attr('checked', this.checked);
    });
});

新编辑:

如果您想在取消选中子项时取消选中父级,并在选中所有子级时检查父级,则可以使用此命令:

PHP代码:

$parents = array();
$childs = array();
foreach ($aResultData as $aResultDataValue) {
     $parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
    $childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}

foreach ($parents as $parent) {
     echo '<div>';
     $parent_value = "'$parent'";
     echo '<div><input type="checkbox" name="parents[]" value="'.$parent.'" id="'.$parent.'" class="parentCheck"/>
     <label for="parents_'.$parent.'">'.$parent.'</label></div>';
     foreach ($childs[$parent] as $child) {
           $child_value = "'$child'";
               echo '<div style="margin-left:15px;"><input type="checkbox" name="childs[]" value="'.$child.'" id="childs_'.$child.'" class="child_'.$parent.'" onclick="checkParent('.$parent_value.','.$child_value.');"/>
              <label for="childs_'.$child.'">'.$child.'</label></div>';
     }
     echo '</div>';
}

JS CODE:

jQuery(document).ready(function(){
      // add multiple select / deselect functionality
      jQuery(".parentCheck").click(function () {
           var childId = jQuery(this).attr('id');
           jQuery('.child_'+childId).attr('checked', this.checked);
      });
 });
 function checkParent(parentId,childId) {
    if(jQuery(".child_"+parentId).length == $(".child_"+parentId+":checked").length) {
           $('#'+parentId).attr("checked", "checked");
       } else {
           $('#'+parentId).removeAttr("checked");
       }
 }

这件事有效:)