簡體   English   中英

如何在 foreach 循環中獲取特定值並在 PHP(codeigniter)中的另一個 foreach 循環中匹配它

[英]How to get a specific value inside a foreach loop and match it in another foreach loop in PHP (codeigniter)

我在 codeigniter 下使用 PHP 有兩 (2) 個 foreach 循環。 他們在 controller 中調用相同的 function。 我想在第一個循環上獲得值“gross_1”,然后將其扣除到第二個循環上的“gross_2”以獲得“final_gross”。

這是我在調用相同的 function 時使用的 Controller(我使用這些沒有問題):

CONTROLLER:
$data['for_first_cutoff'] = $this->MyModel->my_function( $date_start_1, $date_end_1);
$data['for_second_cutoff'] = $this->MyModel->my_function( $date_start_2, $date_end_2 );

MODEL:
public function my_function($getDate_start, $getDate_end){
    $this->db->select('usr.*, att.id, ss.*');
    $this->db->from('usr');
    $this->db->join('att', 'att.id = att.id');
    $this->db->join('ss', 'ss.id = att.comp');
    $this->db->group_by('att.id');
    $this->db->where('att_date >=',$getDate_start);
    $this->db->where('att_date <=',$getDate_end);
    $query  = $this->db->get();

    return $query->result_array();
}

這是視圖(第一個循環)。

<!-- first loop -->
<?php foreach ($for_first_cutoff as $emp_1):?>

<?php 

   $num_of_days_1 = '12'; //auto computed based on "start1" and "end" dates1. 
   $salary_cutoff_1 = $emp_1['salary'];
   $gross_1 = $salary_cutoff_1 * $num_of_days_1; 
?>

   <!-- table here-->
   | <?=$emp_1['usr_fname'];?> | <?=$gross_1;?> |


<?php endforeach;?>

(第二個循環)相同的內容,但我需要將 Gross_1 減去 Gross_2 才能得到 final_gross:

<!-- Second loop -->
<?php foreach ($for_second_cutoff as $emp_2):?>

<?php 
   $num_of_days_2 = '10'; //auto computed based on "start2" and "end" dates2. 
   $salary_cutoff_2 = $emp_2['salary'];
   $gross_2 = $salary_cutoff_2 * $num_of_days_2; 

   $final_gross = $gross_1 - $gross_2;
?>

   <!-- table here-->
  | <?=$emp_2['usr_fname'];?> | <?=$gross_2;?> | <?=$final_gross;?> |


<?php endforeach;?>

HTML 看起來像這樣:

Loop 1
| John Doe | $100 |
| Sarah Doe | $0 |

Loop 2
| John Doe | $50 | $150 |
| Sarah Doe | $2 | $150 | <---- this is wrong. It must be only $2.

我嘗試使用在這個 [post] 中找到的方法( 在這種情況下如何獲得 foreach 循環值之外的值? ),但我不知道如何使用我的結構來實現它。

好的 - 根據評論,您可以嘗試以下操作

創建一個數組,用於保存第一個循環中的數據 - 我用作鍵att.id (應該可能是你的 user_id) - 並從你的第一個循環訪問這個數組,你的 id 來自第二個循環

<!-- first loop -->
<?php 
    $arrGrossData = [];
    foreach ($for_first_cutoff as $emp_1):
?>

<?php 

   $num_of_days_1 = '12'; //auto computed based on "start1" and "end" dates1. 
   $salary_cutoff_1 = $emp_1['salary'];
   $gross_1 = $salary_cutoff_1 * $num_of_days_1; 
   $arrGrossData[$emp_1['att.id']] = $gross_1;
?>

   <!-- table here-->
   | <?=$emp_1['usr_fname'];?> | <?=$gross_1;?> |


<?php endforeach;?>


<!-- Second loop -->
<?php foreach ($for_second_cutoff as $emp_2):?>

<?php 
   $num_of_days_2 = '10'; //auto computed based on "start2" and "end" dates2. 
   $salary_cutoff_2 = $emp_2['salary'];
   $gross_2 = $salary_cutoff_2 * $num_of_days_2; 

   $final_gross = (isset($arrGrossData[$emp_2['att.id']])) ? $arrGrossData[$emp_2['att.id']] - $gross_2 : $gross_2;
?>

   <!-- table here-->
  | <?=$emp_2['usr_fname'];?> | <?=$gross_2;?> | <?=$final_gross;?> |


<?php endforeach;?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM