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条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动