面向初学者的重构
Refactoring for beginners
所以我已经建立了一个系统,现在我正在整理。我正在使用 Codeigniter MVC 框架和 PHP Storm,在我看来我有大量的数学知识。见下文。
foreach ($records as $row) :
$join_date = $row->start_date;
$date1 = new DateTime('now');
$date2 = new DateTime($join_date);
$p = $row->start_amount;
$i = $row->interest;
$c = 12; // compound frequency set to monthly
$n = ((int)$date1->diff($date2)->format("%m")) / 12;
$r = $row->monthly_deposits;
$x = $i / $c;
$y = pow((1 + $x), ($n * $c));
$Total_balance = $p * $y + ($r * (1 + $x) * ($y - 1) / $x);
$remain = 365 - $date1->diff($date2)->format("%a days");
$Total_Deposits = ($row->monthly_deposits * (int)$date1->diff($date2)->format("%m")) + $row->start_amount;
$Total_Int = $Total_balance - $Total_Deposits;
$originalDate = $row->start_date;
$newDate = date("jS \of F Y", strtotime($originalDate));
// Add field values to get row total
$rowTotal = $Total_balance;
// Add row total to grand total
$grandTotal += $rowTotal;
?>
我需要它以便我可以在我的代码中调用变量并且它需要在循环中。
执行此操作的最佳方法是什么,表明我将数学放入模型中,然后将 this>model>modelname
之类的东西放入循环中?
谢谢大家,代码工作正常,只是不确定保持代码整洁的最佳方法。
视图基本上是为了显示您已经在控制器中计算的内容。将此作为一般规则(这并不意味着您不能根据需要在模型或视图上计算任何内容)。
为了保持代码的简洁和有意义,我将执行以下操作:
型号
class Random_model extends CI_Model {
function get_records() {
// access the database and return the rows you need
}
}
控制器
function whatever() {
$data = array(
'first_result' => '',
'second_result' => ''
);
$this->load->model('random_model');
$records = $this->random_model->get_records();
foreach ($records as $row) {
// do here the huge chunk of math and
// put in $data the results you need to display
}
$this->load->view('myview', $data)
}
查看
<div> <?php echo $first_result; ?> </div>
<div> <?php echo $second_result; ?> </div>
所以我已经建立了一个系统,现在我正在整理。我正在使用 Codeigniter MVC 框架和 PHP Storm,在我看来我有大量的数学知识。见下文。
foreach ($records as $row) :
$join_date = $row->start_date;
$date1 = new DateTime('now');
$date2 = new DateTime($join_date);
$p = $row->start_amount;
$i = $row->interest;
$c = 12; // compound frequency set to monthly
$n = ((int)$date1->diff($date2)->format("%m")) / 12;
$r = $row->monthly_deposits;
$x = $i / $c;
$y = pow((1 + $x), ($n * $c));
$Total_balance = $p * $y + ($r * (1 + $x) * ($y - 1) / $x);
$remain = 365 - $date1->diff($date2)->format("%a days");
$Total_Deposits = ($row->monthly_deposits * (int)$date1->diff($date2)->format("%m")) + $row->start_amount;
$Total_Int = $Total_balance - $Total_Deposits;
$originalDate = $row->start_date;
$newDate = date("jS \of F Y", strtotime($originalDate));
// Add field values to get row total
$rowTotal = $Total_balance;
// Add row total to grand total
$grandTotal += $rowTotal;
?>
我需要它以便我可以在我的代码中调用变量并且它需要在循环中。
执行此操作的最佳方法是什么,表明我将数学放入模型中,然后将 this>model>modelname
之类的东西放入循环中?
谢谢大家,代码工作正常,只是不确定保持代码整洁的最佳方法。
视图基本上是为了显示您已经在控制器中计算的内容。将此作为一般规则(这并不意味着您不能根据需要在模型或视图上计算任何内容)。
为了保持代码的简洁和有意义,我将执行以下操作:
型号
class Random_model extends CI_Model {
function get_records() {
// access the database and return the rows you need
}
}
控制器
function whatever() {
$data = array(
'first_result' => '',
'second_result' => ''
);
$this->load->model('random_model');
$records = $this->random_model->get_records();
foreach ($records as $row) {
// do here the huge chunk of math and
// put in $data the results you need to display
}
$this->load->view('myview', $data)
}
查看
<div> <?php echo $first_result; ?> </div>
<div> <?php echo $second_result; ?> </div>