0

!想用像这样的复选框打印当前月份日期是 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>';  
}


?>
4

2 回答 2

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 (nr of arrayitems) 次) * $no_of_days。例子:

  • 如果数组 $attendancelist 中有 1 项且 $no_of_days 为 1,则将显示 ONE (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

?>
于 2013-03-22T08:12:15.540 回答
0

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

以下示例在当前月份的 DAY 16 下输出两个复选框。第一个被选中(value=1),另一个没有(value=0),day21 显示一个选中的复选框(value=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

?>
于 2013-03-23T22:20:45.660 回答