Php复选框重复

时间:2013-03-20 10:40:58

标签: php

!想打印当前月份的日期,复选框就像这是3月,我从1-03-2013开始现在复选框[],2-03-2013现在[]直到月底,31个日期和31个复选框,现在我有数组中复选框的值$ isActive = $ attendancelis ['EmployeeAttendance'] ['is_active'] ;.我希望复选框中的$ isActive值都是全部。以下是print_r($attendancelis);的输出:

Array ( [EmployeeAttendance] => Array ( [id] => 5 [employee_id] => 8 [is_active] => 1 [date] => 2013-03-21 11:15:17 [day_month_year] => 2323123 ) ) Array ( [EmployeeAttendance] => Array ( [id] => 3 [employee_id] => 8 [is_active] => 1 [date] => 2013-03-12 17:47:03 [day_month_year] => 23213213 ) )
Array ( [EmployeeAttendance] => Array ( [id] => 0 [employee_id] => 8 [is_active] => 1 [date] => 2013-03-16 13:11:58 [day_month_year] => 324234324 ) ) -->

这是我的代码:

<?php

$nrDaysCurrentMonth = date("t");    
for($dayNr = 1; $dayNr <= $nrDaysCurrentMonth; $dayNr++)
{
echo'<div style="width:300px;float:left;">';
echo  date(''.$dayNr.'-m-Y'); 
echo '<input name="frmEmployeeAttendance[]" type="checkbox" value="0" />'; 
foreach($attendanceList as $attendanceLis)
{
$isActivee = $attendanceLis['EmployeeAttendance']['date'];
$isActiveYM = strtotime(date('d-m-Y', strtotime($isActivee)));
$isActiveYMD = date('j', strtotime($isActivee)); 

if($dayNr==$isActiveYMD) {
$isActive = $attendanceLis['EmployeeAttendance']['is_active'];

if ($isActive){ 
echo '<input name="frmEmployeeAttendance[]" type="checkbox" checked=="checked" value="' . $isActive . '" />'; 
}
else { 
echo '<input name="frmEmployeeAttendance[]" type="checkbox" value="' . $isActive . '" />';
}       
}


}

echo '<br/>';
echo'</div>';  
}


?>

2 个答案:

答案 0 :(得分:0)

foreach($attendancelist as $attendancelis) {
}
如果你喜欢

将遍历数组$ attendancelist两次

Array ( [EmployeeAttendance] => Array ( [id] => 5 [employee_id] => 8 [is_active] => 1 [date] => 2013-02-21 11:15:17 [day_month_year] => 2323123 ) ) Array ( [EmployeeAttendance] => Array ( [id] => 3 [employee_id] => 8 [is_active] => 1 [date] => 2013-03-12 17:47:03 [day_month_year] => 23213213 ) )
Array ( [EmployeeAttendance] => Array ( [id] => 1 [employee_id] => 8 [is_active] => 1 [date] => 2013-03-16 13:11:58 [day_month_year] => 324234324 ) ) -->

foreach($ attendancelist as $ attendancelis){ } 将在行上方迭代两次。

循环

for($i = 1; $i <= $no_of_days; $i++)
{
}
如果$ no_of_days为1,

将进行1次迭代。

如果你将上述循环结合起来......

    foreach($attendancelist as $attendancelis) {

    for($i = 1; $i <= $no_of_days; $i++)
    {
    //checkbox stuff here
    }

    }

将迭代=($ attendancelist(nite of arrayitems)次)* $ no_of_days。 示例:

  • 如果您在数组$ attendancelist中有1项且$ no_of_days为1, 然后将显示一个(1 * 1)复选框
  • 如果您在阵列$ attendancelist中有2个项目且$ no_of_days为1,那么将显示两个(2 * 1)复选框
  • 如果您在数组$ attendancelist中有2个项目且$ no_of_days为2,则会显示四个(2 * 2)个复选框

您还应该使用格式化技巧! :-)我希望这会有所帮助...

<强>更新

<?php
$no_of_days = 10;

$attendancelist = array('a','b', 'c');

foreach($attendancelist as $attendancelis)
{


        for($i = 1; $i <= $no_of_days; $i++) {

                    ?>
                <div style="width:300px;float:left;">
                    INACTIVE<input type="checkbox" value="0" name="isactive"  />
                <?php echo $start = date('' . $i . '-m-Y'); ?>
                    <br/><br/><br/>         
                </div>
        <?php
        }

}
?>

上面的代码将迭代第一次循环3次。 (a,b和c),并且对于每次迭代,它将显示10天的复选框。

再次更新: - 如果您只想为每个日期显示一些值...... ......就像这样......

<?php
$no_of_days = 10;
$attendancelist = array('a', 'b', 'c');

$outputCheckbox = array();

//Go through all days
for($i = 1; $i <= $no_of_days; $i++)
{

    //Show one checkbox for every date.
    foreach($attendancelist as $attendancelis)
    {
        $isActive = true; //Dummy

        if ($outputCheckbox[$i] == false) {

            if ($isActive) {
                echo '<input type="checbox" value="active"><br />';
            }
            else {
                echo '<input type="checbox" value="inactive"><br />';
            }
            $outputCheckbox[$i] = true;

        }
    }
    //--attendanceList loop

}
//-- days loop

?>

答案 1 :(得分:0)

啊哈,现在我觉得我真的明白你的问题! :-)它只用了大约两天左右;-) lol

以下示例在第16天输出当前月份的两个复选框。检查第一个(值为1),另一个不是(值为0),第21天显示复选复选框(值= 1)。我没有包括格式与div和东西,但我想你会管理! : - )

<?php

//Example data
$attendanceList = array();
$attendanceList[0]['EmployeeAttendance'] = array('id' => 5, 'employee_id' => 8, 'is_active' => 1, 'date' => '2013-02-21 11:15:17', 'day_month_year' => 2323123);
$attendanceList[1]['EmployeeAttendance'] = array('id' => 3, 'employee_id' => 8, 'is_active' => 1, 'date' => '2013-03-16 17:47:03', 'day_month_year' => 23213213);
$attendanceList[2]['EmployeeAttendance'] = array('id' => 1, 'employee_id' => 8, 'is_active' => 0, 'date' => '2013-03-16 13:11:58', 'day_month_year' => 324234324);


//Go through all days for current month
$nrDaysCurrentMonth = date("t");    
for($dayNr = 1; $dayNr <= $nrDaysCurrentMonth; $dayNr++)
{
    echo 'Day: ' . $dayNr . '<br />';       //Show nr of current date once

    //Go through attendancelist and check where nr of dates match. 
    //Where they match, show checkboxes with active or not.
    foreach($attendanceList as $attendanceLis)
    {

        //Get this iterations date and get the number of day and put it into $isActiveYMD
        $isActivee = $attendanceLis['EmployeeAttendance']['date'];
        $isActiveYM = strtotime(date('d-m-Y', strtotime($isActivee)));
        $isActiveYMD = date('j', strtotime($isActivee)); //Day of month without leading zeros

        //Compare day of month with days loop value ($dayNr). If date from 
        if($dayNr==$isActiveYMD) {

            $isActive = $attendanceLis['EmployeeAttendance']['is_active'];

            if ($isActive) {
                echo '<input name="frmEmployeeAttendance[]" type="checkbox" checked=="checked" value="' . $isActive . '" /><br />'; 
            }
            else {
                echo '<input name="frmEmployeeAttendance[]" type="checkbox" value="' . $isActive . '" /><br />';
            }       
        }
        //--compare day of month...
    }
    //--attendanceList loop
    echo '<hr />';
}
//-- days loop

?>