比较关联数组键和复选框的值

时间:2016-10-06 18:07:25

标签: php checkbox cakephp-1.3

我有一个带有活动空间复选框的网络表单。

checkboxes

从我的数据库“facility_event_charges”中的一个表中获取复选框值:

facility event charges

让我说我检查5和6,“视听设备”和“视听技术员”并提交我的表格。

这会将id(s)保存到我对“Event Space”的单独表中 假设我的“事件空间”的ID为63.由于我选中了2个复选框,因此查找表现在有两个条目用于我的ID:

lookup table

一切都很好并且得救了。 现在,假设我需要实际编辑与此空间相关的费用。

当我点击编辑时,我的webform呈现表单字段但是复选框是空的!!为什么?因为它从我的“facility_event_charges”表中获取数据。

我需要将此表数据(数组)与保存在查找表(数组)中的数据进行比较,以确定哪些值是相同的,并在这些复选框中进行检查。

我的编辑空间webform中的所需结果将检查这些结果 checked

我的复选框“收费”数组:

array(6) { 
[1]=> string(9) "Officials" 
[2]=> string(6) "Ushers" 
[3]=> string(19) "Additional Staffing" 
[4]=> string(16) "Special Lighting" 
[5]=> string(22) "Audio Visual Equipment" 
[6]=> string(23) "Audio Visual Technician" 
}

- 我也可以在这里产生费用:

array(6) { 
[0]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "1" ["type"]=> string(9) "Officials" } } 
[1]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "2" ["type"]=> string(6) "Ushers" } } 
[2]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "3" ["type"]=> string(19) "Additional Staffing" } } 
[3]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "4" ["type"]=> string(16) "Special Lighting" } } 
[4]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "5" ["type"]=> string(22) "Audio Visual Equipment" } } 
[5]=> array(1) { ["FacilityEventCharge"]=> array(2) { ["id"]=> string(1) "6" ["type"]=> string(23) "Audio Visual Technician" } } } 

我的查找表数组:

array(2) { 
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } } 
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } } 
} 

- 我的尝试一直没有成功 - 尝试过php array_intersect array_intersect_assoc array_map( '和array_diff_assoc')

我需要遍历复选框计费数组并从查找表数组中找到匹配项,如果匹配,则选中“已选中”复选框。

任何人都有可以测试的工作示例吗? 复选框收费数组是需要匹配“facility_event_charges_id”

3 个答案:

答案 0 :(得分:0)

这有效......

但是它会根据你最终如何构建数组而改变......但是这应该只需要改变if-then语句并且可能需要改变i的值(索引变量)。

<html>
<head></head>
<body>
<form>
<?php

// assume you build this based on a sql query
// you don't need to use the primary key for the
// array index
$charges[0]="Officials";
$charges[1]="Ushers";
$charges[2]="Additional Staffing";
$charges[3]="Special Lighting";
$charges[4]="AV Equipment";
$charges[5]="AV Technician";


// your array of eventcharges
// select facility_event_charges_id from tbl where facility_event_spaces_id='63'
// loop thru the result of teh query make a simple array of your evencharge ids
// gonna hardcode mine out of laziness
// since php is typeless we can use strings to compare to ints
// directly as long as we use == and not ===
// and in_array() (used below) goes with loose comparison too
$ec[]="4";
$ec[]="5";

// now we have an array of our descriptions
// and an array of the already selected choices
// from the db data

for($i=0;$i<count($charges);$i++){
    print('<input type="checkbox" name="charges" value="'.$i.'" ');
    if(in_array($i,$ec)){
        print("checked");
    }
    print(" > ".$charges[$i]."<br />");
}

?>
</form>
</body></html>

答案 1 :(得分:0)

根据您上面给出的数组,您可以像这样比较

例如Lets take $charge array as

array(6) { 
[1]=> string(9) "Officials" 
[2]=> string(6) "Ushers" 
[3]=> string(19) "Additional Staffing" 
[4]=> string(16) "Special Lighting" 
[5]=> string(22) "Audio Visual Equipment" 
[6]=> string(23) "Audio Visual Technician" 
}

$lookup array as

array(2) { 
[0]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "5" } } 
[1]=> array(1) { ["EventCharge"]=> array(1) { ["facility_event_charges_id"]=> string(1) "6" } } 
}

你可以compare both array like below

$charges = array(
    1 => "Officials",
    2 => "Ushers",
    3 => "Additional Staffing",
    4 => "Special Lighting",
    5 => "Audio Visual Equipment",
    6 => "Audio Visual Technician"
);
$lookup = array(
    array("EventCharge" => array(
            "facility_event_charges_id" => "5"
        )
    ), array(
        "EventCharge" => array(
            "facility_event_charges_id" => "6"
        )
    )
);
$lookup = array_column(array_column($lookup, 'EventCharge'), 'facility_event_charges_id');
foreach ($charges as $key => $value) {
    echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $lookup)?"checked":"").'>';
    echo '<label>'.$value.'<label>';
}

修改

如果不支持array_column(),则可以使用如下所示的循环创建数组并测试

$arr=array();
foreach ($lookup as $key => $value) {
    foreach ($value as $k => $v) {
        $arr[]=$v['facility_event_charges_id'];
    }    
}
foreach ($charges as $key => $value) {
    echo '<input type="checkbox" name="charges" value="' . $key . '" '.(in_array($key, $arr)?"checked":"").'>';
    echo '<label>'.$value.'<label>';
}

答案 2 :(得分:0)

您好,您可以使用以下code.i假设您有$ arr = array('5','6');从database.so你可以我们如下

<?php
$arr=array('5'=>'Audio Visual Equipment','6'=>'Audio Visual Technician');
$arr=array_keys($arr);//if you have associated array
?>



<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="ISO-8859-1"> 
</head>
<body>
    <input type="checkbox" name="charges" value="1" <?=in_array('1', $arr)?'checked=true':''?>> Officials<br>
    <input type="checkbox" name="charges" value="2" <?=in_array('2', $arr)?'checked=true':''?>> Ushers<br>
    <input type="checkbox" name="charges" value="3" <?=in_array('3', $arr)?'checked=true':''?>> Additional Staffing <br>
    <input type="checkbox" name="charges" value="4" <?=in_array('4', $arr)?'checked=true':''?>> Special Lighting <br>
    <input type="checkbox" name="charges" value="5" <?=in_array('5', $arr)?'checked=true':''?>> Audio Visual Equipment<br>
    <input type="checkbox" name="charges" value="6" <?=in_array('6', $arr)?'checked=true':''?>> Audio Visual Technician<br>
</body>
</html>