dongpan3001 2015-07-03 11:26
浏览 53
已采纳

将旧的过程PHP函数转换为Codeigniter中的模型 - 视图 - 控制器(MVC)

I am new to Model-View-Controller and I started coding in Codeigniter. I am basically converting my project into MVC, however, I encountered this function(below) which I would like to split into MVC. I have 100s of functions like this and if I get the best approach to this, I will be able to convert the rest of my functions into MVC myself.

This function contains PHP, Mysql, and HTML everything in one. Like we split queries and HTML separately, I also want to do it using Codeingiter framework. Even if you cannot answer using codeigniter default functions, just tell me how to split.

Here it is :

 $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);
    }

Thanks

  • 写回答

2条回答 默认 最新

  • douchi8503 2015-07-03 13:39
    关注

    Your code is a bit funky and not optimal. You are recalling a sql query and iterating where you dont need to. What I would do to fix it is take advantage of MYSQL's GROUP_CONCAT, then convert it all into MVC using Codeigniter. Here's my approach:

    Model: 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>
    

    Source: http://www.codeigniter.com/userguide3/overview/mvc.html

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元