我对数组循环有问题。
我有2张桌子。员工和销售。像这样的表结构。
car=> select id,fullname,dealer_id from sc_employee where dealer_id=1; id | fullname | dealer_id ----+-------------------+----------- 1 | James Butt | 1 2 | Josephine Darakjy | 1 3 | Art Venere | 1 4 | Lenna Paprocki | 1 (4 rows) car=> select * from sc_recsales where dealer_senderid='831081551'; id | vehicle_typeid | spk_num | vin_num | retail_date | bpk_num | sc_id | notes | price | dealer_senderid -----+----------------+----------+------------------+-------------+----------+-------+-------+-----------+----------------- 515 | 120 | CAR00003 | H4TB2H26CC000002 | 2015-12-26 | CAR00003 | 3 | | 500000000 | 831081551 516 | 120 | CAR00004 | H4TB2H26CC000003 | 2015-12-26 | CAR00004 | 4 | | 500000000 | 831081551 518 | 120 | CAR00006 | H4TB2H26CC000005 | 2015-12-26 | CAR00006 | 2 | | 500000000 | 831081551 519 | 120 | CAR00007 | H4TB2H26CC000006 | 2015-12-26 | CAR00007 | 3 | | 500000000 | 831081551 520 | 120 | CAR00008 | H4TB2H26CC000007 | 2015-12-26 | CAR00008 | 4 | | 500000000 | 831081551 521 | 120 | CAR00009 | H4TB2H26CC000008 | 2015-12-26 | CAR00009 | 1 | | 500000000 | 831081551 522 | 120 | CAR00010 | H4TB2H26CC000009 | 2015-12-26 | CAR00010 | 1 | | 500000000 | 831081551 524 | 120 | CAR00012 | H4TB2H26CC000011 | 2015-12-26 | CAR00012 | 1 | | 500000000 | 831081551 525 | 120 | CAR00013 | H4TB2H26CC000012 | 2015-12-26 | CAR00013 | 1 | | 500000000 | 831081551 526 | 120 | CAR00014 | H4TB2H26CC000013 | 2015-12-26 | CAR00014 | 2 | | 500000000 | 831081551 527 | 120 | CAR00015 | H4TB2H26CC000014 | 2015-12-26 | CAR00015 | 3 | | 500000000 | 831081551 528 | 120 | CAR00016 | H4TB2H26CC000015 | 2015-12-26 | CAR00016 | 4 | | 500000000 | 831081551 530 | 120 | CAR00018 | H4XZ2D26CC000002 | 2015-12-26 | CAR00018 | 2 | | 500000000 | 831081551 531 | 120 | CAR00019 | H4XZ2D26CC000003 | 2015-12-26 | CAR00019 | 2 | | 500000000 | 831081551 532 | 120 | CAR00020 | H4XZ2D26CC000004 | 2015-12-26 | CAR00020 | 2 | | 500000000 | 831081551 533 | 120 | CAR00021 | H4XZ2D26CC000005 | 2015-12-26 | CAR00021 | 2 | | 500000000 | 831081551 535 | 120 | CAR00023 | H4XZ2D26CC000007 | 2015-12-26 | CAR00023 | 1 | | 500000000 | 831081551 536 | 120 | CAR00024 | H4XZ2D26CC000008 | 2015-12-26 | CAR00024 | 1 | | 500000000 | 831081551 537 | 120 | CAR00025 | H4XZ2D26CC000009 | 2016-01-15 | CAR00025 | 1 | | 500000000 | 831081551 538 | 120 | CAR00026 | H4AA5D26CC000001 | 2016-01-15 | CAR00026 | 2 | | 500000000 | 831081551 539 | 120 | CAR00027 | H4AA5D26CC000002 | 2016-01-15 | CAR00027 | 2 | | 500000000 | 831081551 541 | 120 | CAR00029 | H4AA5D26CC000004 | 2016-01-15 | CAR00029 | 1 | | 500000000 | 831081551 542 | 120 | CAR00030 | H4AA5D26CC000005 | 2016-01-15 | CAR00030 | 2 | | 500000000 | 831081551 543 | 120 | CAR00031 | H4AA5D26CC000006 | 2015-11-05 | CAR00031 | 3 | | 500000000 | 831081551 544 | 120 | CAR00032 | H4AA5D26CC000007 | 2015-11-05 | CAR00032 | 4 | | 500000000 | 831081551 545 | 120 | CAR00033 | H4AA5D26CC000008 | 2015-11-05 | CAR00033 | 1 | | 500000000 | 831081551 547 | 120 | CAR00035 | H4AA5D26CC000010 | 2015-11-05 | CAR00035 | 3 | | 500000000 | 831081551 514 | 120 | CAR00002 | H4TB2H26CC000001 | 2015-12-26 | CAR00002 | 2 | | 500000000 | 831081551 513 | 120 | CAR00001 | H4TB2H26CC000000 | 2015-12-26 | CAR00001 | 1 | | 500000000 | 831081551 517 | 120 | CAR00005 | H4TB2H26CC000004 | 2015-12-26 | CAR00005 | 1 | | 500000000 | 831081551 523 | 120 | CAR00011 | H4TB2H26CC000010 | 2015-12-26 | CAR00011 | 1 | | 500000000 | 831081551 529 | 120 | CAR00017 | H4XZ2D26CC000001 | 2015-12-26 | CAR00017 | 1 | | 500000000 | 831081551 534 | 120 | CAR00022 | H4XZ2D26CC000006 | 2015-12-26 | CAR00022 | 2 | | 500000000 | 831081551 540 | 120 | CAR00028 | H4AA5D26CC000003 | 2016-01-15 | CAR00028 | 1 | | 500000000 | 831081551 546 | 120 | CAR00034 | H4AA5D26CC000009 | 2015-11-05 | CAR00034 | 2 | | 500000000 | 831081551
in modelmodels/ScRecsales.php
public static function getTest2($dealer) {
$salesCount = ScRecsales::find()
->select(['*'])
->where("dealer_senderid ='$dealer'")
->all();
return $salesCount;
}
public static function getStaff($dealer) {
$staff = ScEmployee::find()
->select(['*'])
->where('dealer_id = '.$dealer)
->all();
return $staff;
}
and in my controller
public function actionTest() {
// $dealer = $_GET['dealer_id'];
$dealer = 831081551;
$sales = ScRecsales::getTest2($dealer);
$salesman = ScRecsales::getStaff(1);
return $this->render('report', [
'sales' => $sales,
'salesman'=>$salesman
]);
}
and in my view
<?php
$temp = array();
$year = array('01', '02', '03', '04', '05', '06', '07', '08' , '09', '10', '11', '12');
foreach ($salesman as $key => $man) {
foreach ($sales as $sl => $value) {
if ($key == $value['sc_id']) {
$retail_date = $value['retail_date'];
$timestamp = strtotime($retail_date);
$month = date("m", $timestamp);
if (!empty($temp[$key][$month])) {
$temp[$key][$month]++;
} else {
$temp[$key][$month] = 1;
}
}
}
}
$finalRes = array();
foreach ($temp as $sales => $man) {
foreach ($year as $key => $val) {
$finalRes[$sales][$val] = (!empty($man[$val])) ? $man[$val] : 0;
}
}
//die(print_r($finalRes));
echo '<table width="100%" border="1">';
echo '<tr>';
echo '<th> </th>';
echo '<th>January</th>';
echo '<th>February</th>';
echo '<th>March</th>';
echo '<th>April</th>';
echo '<th>May</th>';
echo '<th>June</th>';
echo '<th>July</th>';
echo '<th>August</th>';
echo '<th>September</th>';
echo '<th>October</th>';
echo '<th>November</th>';
echo '<th>December</th>';
echo '</tr>';
foreach ($finalRes as $key => $val) {
//die(print_r($salesman[$key]));
//print_r($salesman[$key]);
echo '<tr>';
echo ' <td>'.$salesman[$key]['fullname'].'</td>';
foreach ($val as $v) {
echo '<td>'.$v.'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
一切正常,但第一个数组的员工未打印在报表上。
最佳答案
在您看来。你可以改变这个
if ($man['id'] == $value['sc_id']) {
$retail_date = $value['retail_date'];
$timestamp = strtotime($retail_date);
$month = date("m", $timestamp);
if (!empty($temp[$key][$month])) {
$temp[$key][$month]++;
} else {
$temp[$key][$month] = 1;
}
}
关于php - YII报告销售缺失阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36588504/