gpt4 book ai didi

php - 在 Codeigniter 中将旧的程序化 PHP 函数转换为模型- View - Controller (MVC)

转载 作者:可可西里 更新时间:2023-11-01 08:45:06 25 4
gpt4 key购买 nike

我是模型- View - Controller 的新手,我开始在 Codeigniter 中编码。我基本上是将我的项目转换为 MVC,但是,我遇到了这个函数(如下),我想将其拆分为 MVC。我有 100 个这样的函数,如果我找到最好的方法,我将能够自己将其余函数转换为 MVC。

此功能集 PHP、Mysql 和 HTML 于一身。就像我们分别拆分查询和 HTML 一样,我也想使用 Codeingiter 框架来完成。即使您不能使用 codeigniter 默认函数回答,也请告诉我如何拆分。

这里是:

 $fetch_projections = mysql_query("SELECT issue_id, emp_name, employeeId, sum(actualHoursPerDay) as ss FROM day_projections WHERE date = '$today' GROUP BY employeeId ORDER BY emp_name ASC");
while ($r = mysql_fetch_array($fetch_projections)) {
$maes_array[] = $r['issue_id'];
$all_maes_for_emp = implode($maes_array);
// echo $r['emp_name'] $r['ss'].'<br/>';

$split_up_query = mysql_query("SELECT issue_id, actualHoursPerDay FROM day_projections WHERE date = '$today' AND emp_name = '" . $r['emp_name'] . "'");
while ($t = mysql_fetch_array($split_up_query)) {
$kk[] = $t['issue_id'] . ' = ' . $t['actualHoursPerDay'] . ' hrs';
}
$pp = implode(', ', $kk);
$cap = round((((8 - $r['ss']) / 8) * 100), 2);
echo '<tr><td>' . $r['emp_name'] . '</td><td>' . $cap . '%</td><td>' . $r['ss'] . ' hrs</td><td>' . $pp . '</td></tr>';
unset($maes_array);
unset($kk);
}

谢谢

最佳答案

您的代码有点古怪而且不是最优的。您正在记忆一个 sql 查询并在不需要的地方迭代。我要修复它的方法是利用 MYSQL 的 GROUP_CONCAT,然后使用 Codeigniter 将其全部转换为 MVC。这是我的方法:

模型: application\models\My_model.php

The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.

class My_model extends CI_MODEL{

function fetch_projections($today){
$this->db->select("emp_name, sum(actualHoursPerDay) as ss, GROUP_CONCAT( issue_id,'=',actualHoursPerDay,'hrs' SEPARATOR ';') as pp");
$this->db->from("day_projections");
$this->db->where("date" , $today);
$this->db->group_by("employeeId");
$this->db->order_by("emp_name" , "asc");
$query = $this->db->get();
return $query->result();
}

}

Controller : application\controllers\My_controller.php

The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

class My_controller extends CI_Controller {

function calculate() {
$today = "0000-00-00"; // or whatever code you have to come up for "today"
$this->load->model("My_model");
$projections_results = $this->My_model->fetch_projections($today);
if ($projections_results) {
foreach ($projections_results as $projection) {
$projection->cap = round((((8 - $projection->ss) / 8) * 100), 2);
}
}
$view_data["results"] = $projections_results;
$this->load->view("my_view", $view_data);
}

}

View : application\views\my_view.php

The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".

<table>
<?php foreach ($results as $res) { ?>
<tr>
<td><?= $res->emp_name ?></td>
<td><?= $res->cap ?>%</td>
<td><?= $res->ss ?>hrs</td>
<td><?= $res->pp ?></td>
</tr>
<?php } ?>
</table>

来源:http://www.codeigniter.com/userguide3/overview/mvc.html

希望这对您有所帮助。

关于php - 在 Codeigniter 中将旧的程序化 PHP 函数转换为模型- View - Controller (MVC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31205736/

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