我有一个表包含公司中所有员工(hr_employees)。除此表之外,另一个表格包含所有开始日期和结束日期,以及它们具有的合同类型(hr_employees_status)。使用我得到的数据,我写了一个1或0(1永久基础,0其他)到excel电子表格,根据契约类型确定颜色编码(1 =黑色,0 =蓝色,绿色,灰色或红色),每月(从2005年5月至现在)。我如何通过case子句来确定哪些单元格应该到哪里,查看hr_employee_status表中的当前状态。循环不写入正确的值
它循环良好,并且它应该正确地执行99%的操作。唯一的问题是,只要员工从临时合同移到永久基础上,它有时不会将正确的值写入单元格。
我已经将代码稍微改为只从数据库中提取月份和年份(并将日期设置为01),出于希望,因为月和年是写入单元格所需的全部内容,它会在正确的单元格中写入正确的数据。但无济于事。
我不知道作什么岗位代码,所以这里是确定合同的类型,并将其写入到Excel工作表的循环:
switch ($dates['status']) {
case '0':
$color = 'blue';
$cellcontent="0";
break;
case '1':
$color = 23;
$cellcontent="0";
break;
case '2':
$color = 'black';
$cellcontent="1";
break;
case '3':
$color = 'green';
$cellcontent="0";
break;
case '4':
$color = 'red';
if(mysql_num_rows($query) > 2)
$cellcontent = "0";
else {
$cellcontent="1";
}
break;
}
if($s['state'] == '4')
$color = 'red';
$format_content =& $workbook->addFormat(array('align' => 'center', 'size' => 11, 'numformat' => '@', 'fontfamily' => 'Calibri', 'left' => 1, 'bottom' => 1, 'right' => 1, 'color' => $color));
for($f = $start_at; $f <= $start_at+$count; $f++) {
$totalmonths = $totalmonths + $cellcontent;
$worksheet->write($k,15+$f,$cellcontent,$format_content);
}
$date
是结果从DB好评。 $start_at
是员工以合同类型开始的日期(从而确定要标记哪个月)。 $count
是开始日期和结束日期(月数)之间的差额。
我想知道为什么它不是从正式的日期开始,从临时合同转换为永久合同。
如果需要其他信息,请告诉我。
编辑1: - 响应从DaveRandom和奥列格反馈
@DaveRandom: hr_employees
看起来是这样的:
------------------------------------------
|employee_id|name|surname|otherinfo|state|
|1 |Foo |Bar | ******* | 1 |
|2 |Ben |Smith | ******* | 1 |
------------------------------------------
hr_employees_status
看起来是这样的:
------------------------------------------
|employee_id|from_date |to_date |status|
|1 |2006-07-12|2009-08-11| 0 |
|1 |2009-08-12|0000-00-00| 1 |
|2 |2009-07-01|0000-00-00| 1 |
------------------------------------------
而且那么Excel表格将输出
Name Surname Start Date *dates* June-06 July06 ->- July09 Aug09 Sep09
Foo Bar 2006-07-12 *empty* 0 ->- 0 1 1
Ben Smith 2009-07-01 *empty* *empty* ->- 1 1 1
@Oleg
$start_at
如下:round(((substr($dates['from_date'],0,4) * 12) + substr($dates['from_date'],5,2)) - ((substr($start_date,0,4) * 12) + substr($start_date,5,2))) + 1; // $dates is a loop through the hr_employee_status table
见上文。
$dates
是通过hr_employee_status表的循环。我100%确定那个状态在那里。请解释类型杂耍?所有的状态值都是varchar(并且不要问我为什么,现在看它,这似乎是一件愚蠢的事情......)
可以显示来自DB的示例数据和当前/想要的电子表格输出吗? – DaveRandom
*“有时不会将正确的值写入单元格”*它代之以什么?此外,您的日志中的任何错误/警告? – Oleg
类型杂耍基本上是自动变量类型转换。例如,将一个字符串与一个整数进行比较 - 该字符串会自动转换为整数。因此,在PHP中:'var_dump(0 ==“a”); // true'。 – Oleg