gpt4 book ai didi

php - 循环不写正确的值

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:50 26 4
gpt4 key购买 nike

我有一个表,其中包含一家公司的所有员工 (hr_employees)。除了这个表之外,还有另一个表,其中包含他们的所有开始日期和结束日期,以及他们拥有的契约(Contract)类型 (hr_employees_status)。

根据我获得的数据,我将 1 或 0(1 个基于永久性数据,0 个其他数据)写入 Excel 电子表格,颜色编码由契约(Contract)类型决定(1 = 黑色,0 = 蓝色、绿色、灰色或红色),每个月(从 2005 年 5 月至今)。我如何通过 case 子句确定哪个单元格中的内容,查看 hr_employee_status 表中的当前状态。

它循环正常,并正确执行了 99% 的操作。唯一的问题是,一旦员工从临时契约(Contract)转为永久契约(Contract),它有时不会将正确的值写入单元格。

我稍微更改了代码以仅从数据库中提取月份和年份(并将日期设置为 01),出于希望,因为月份和年份是写入单元格所需的全部内容,它会在正确的单元格中写入正确的数据。但无济于事。

我不知道要发布什么代码,所以这里是确定契约(Contract)类型并将其写入 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 是从数据库收到的结果。$start_at 是员工开始使用契约(Contract)类型的日期(因此确定要标记的月份)。$count 是开始日期和结束日期(月数)之间的差值。

我想知道为什么在从临时契约(Contract)转换为永久契约(Contract)时没有在正确的日期开始。

如果需要任何其他信息,请告诉我。

编辑 1: - 回应 DaveRandom 和 Oleg 的反馈

@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

@奥列格

  1. $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
  2. 见上文。 $dates 是遍历 hr_employee_status 表的循环。我 100% 确定状态在那里。

  3. 请解释类型杂耍? status 中的所有值都是 varchar(不要问我为什么,现在看来,这似乎是一件愚蠢的事情......)

最佳答案

我们遗漏了您代码的一些关键部分,所以在您提供更多详细信息之前(例如,您能否向我们展示您定义 $start_at 的位置?)我只能猜测...

  1. 您问“为什么它没有在正确的日期开始”,但您从未提供定义 $start_at 的代码。它可能对我们有用。
  2. 为什么要切换$dates['status']?您确定它在 $dates 中吗?
  3. switch 语句在类型转换后比较值...

Example from php.net :

switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}

这可能是您的情况,因为 $dates['status'] 的类型不正确导致您的 switch 语句 永远不会达到永久契约(Contract)状态 1 使其停止在 case '0':.

尝试使用 var_dump() 查看您在代码中操作的变量的确切类型和值。

关于php - 循环不写正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10681455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com